Pull the prose content out of asciidoctor's HTML output.
| 98 | |
| 99 | |
| 100 | class TextExtractor(HTMLParser): |
| 101 | """Pull the prose content out of asciidoctor's HTML output.""" |
| 102 | |
| 103 | def __init__(self): |
| 104 | super().__init__() |
| 105 | self._chunks = [] |
| 106 | self._skip_depth = 0 |
| 107 | self._skip_is_block = False |
| 108 | self._label_depth = 0 |
| 109 | self._element_skip_depth = 0 |
| 110 | # Track open tags whose skip mode we entered, so we know when to |
| 111 | # leave it. Each entry is the tag name pushed on enter. |
| 112 | self._element_skip_stack = [] |
| 113 | |
| 114 | def handle_starttag(self, tag, attrs): |
| 115 | # Whole-element skip (table of contents, code listings, footer). |
| 116 | if self._element_skip_depth > 0: |
| 117 | self._element_skip_depth += 1 |
| 118 | self._element_skip_stack.append(tag) |
| 119 | return |
| 120 | if _attrs_match_skip(tag, attrs): |
| 121 | self._chunks.append("\n\n") |
| 122 | self._element_skip_depth = 1 |
| 123 | self._element_skip_stack = [tag] |
| 124 | return |
| 125 | if tag in SKIP_TAGS: |
| 126 | if self._skip_depth == 0 and self._label_depth == 0: |
| 127 | self._skip_is_block = tag in BLOCK_SKIP_TAGS |
| 128 | if self._skip_is_block: |
| 129 | self._chunks.append("\n\n") |
| 130 | else: |
| 131 | self._chunks.append(INLINE_SKIP_PLACEHOLDER) |
| 132 | self._skip_depth += 1 |
| 133 | return |
| 134 | if tag in LABEL_TAGS: |
| 135 | if self._label_depth == 0: |
| 136 | self._chunks.append("\n\n") |
| 137 | self._label_depth += 1 |
| 138 | return |
| 139 | if tag in ("p", "li", "dd", "div"): |
| 140 | self._chunks.append("\n\n") |
| 141 | |
| 142 | def handle_endtag(self, tag): |
| 143 | if self._element_skip_depth > 0: |
| 144 | self._element_skip_depth -= 1 |
| 145 | if self._element_skip_stack: |
| 146 | self._element_skip_stack.pop() |
| 147 | if self._element_skip_depth == 0: |
| 148 | self._chunks.append("\n\n") |
| 149 | return |
| 150 | if tag in SKIP_TAGS and self._skip_depth > 0: |
| 151 | self._skip_depth -= 1 |
| 152 | if self._skip_depth == 0 and self._label_depth == 0: |
| 153 | if self._skip_is_block: |
| 154 | self._chunks.append("\n\n") |
| 155 | self._skip_is_block = False |
| 156 | return |
| 157 | if tag in LABEL_TAGS: |