Return True if ``text`` is likely markup.
(text)
| 77 | |
| 78 | |
| 79 | def is_markup_text(text): |
| 80 | """ |
| 81 | Return True if ``text`` is likely markup. |
| 82 | """ |
| 83 | if text.startswith("<"): |
| 84 | return True |
| 85 | |
| 86 | # count whitespaces |
| 87 | no_spaces = "".join(text.split()) |
| 88 | |
| 89 | # count opening and closing tags_count |
| 90 | counts = Counter(c for c in no_spaces if c in "<>") |
| 91 | |
| 92 | if not all(c in counts for c in "<>"): |
| 93 | return False |
| 94 | |
| 95 | if not all(counts.values()): |
| 96 | return False |
| 97 | |
| 98 | # ~ 5 percent of tag <> markers measn we have tags |
| 99 | has_tags = sum(counts.values()) / len(no_spaces) > 0.05 |
| 100 | |
| 101 | # check if we have some significant proportion of tag-like characters |
| 102 | open_close = counts[">"] / counts["<"] |
| 103 | # ratio of open to close tags should approach 1: accept a 20% drift |
| 104 | balanced = abs(1 - open_close) < 0.2 |
| 105 | return has_tags and balanced |
| 106 | |
| 107 | |
| 108 | def is_kept_tag( |
no test coverage detected