text
stringlengths 0
828
|
---|
if len(self._addresses) < 2:
|
# there can't be a tag boundary if there's only 1 or 0 characters
|
return frozenset()
|
# creating 'parent_sets' mapping, where the first item in tuple
|
# is the address of character and the second is set
|
# of character's parent HTML elements
|
parent_sets = []
|
# meanwhile we are creatingalso a set of common parents so we can
|
# put them away later on (we're not interested in them as
|
# they're only some global wrappers)
|
common_parents = set()
|
for addr in self._addresses:
|
parents = set()
|
if addr.attr == 'text':
|
parents.add(addr.element)
|
parents.update(addr.element.iterancestors())
|
parent_sets.append((addr, parents))
|
if not common_parents:
|
common_parents = parents
|
else:
|
common_parents &= parents
|
# constructing final set of involved tags
|
involved_tags = set()
|
prev_addr = None
|
for addr, parents in parent_sets:
|
parents = parents - common_parents
|
involved_tags.update(p.tag for p in parents)
|
# hidden tags - sometimes there are tags without text which
|
# can hide between characters, but they actually break textflow
|
is_tail_of_hidden = (
|
prev_addr and
|
addr.attr == 'tail' and
|
prev_addr.element != addr.element
|
)
|
if is_tail_of_hidden:
|
involved_tags.add(addr.element)
|
prev_addr = addr
|
return frozenset(involved_tags)"
|
4646,"def _parse(self, html):
|
""""""Parse given string as HTML and return it's etree representation.""""""
|
if self._has_body_re.search(html):
|
tree = lxml.html.document_fromstring(html).find('.//body')
|
self.has_body = True
|
else:
|
tree = lxml.html.fragment_fromstring(html,
|
create_parent=self._root_tag)
|
if tree.tag != self._root_tag:
|
# ensure the root element exists even if not really needed,
|
# so the tree has always the same structure
|
root = lxml.html.HtmlElement()
|
root.tag = self._root_tag
|
root.append(tree)
|
return root
|
return tree"
|
4647,"def _iter_texts(self, tree):
|
""""""Iterates over texts in given HTML tree.""""""
|
skip = (
|
not isinstance(tree, lxml.html.HtmlElement) # comments, etc.
|
or tree.tag in self.skipped_tags
|
)
|
if not skip:
|
if tree.text:
|
yield Text(tree.text, tree, 'text')
|
for child in tree:
|
for text in self._iter_texts(child):
|
yield text
|
if tree.tail:
|
yield Text(tree.tail, tree, 'tail')"
|
4648,"def _analyze_tree(self, tree):
|
""""""Analyze given tree and create mapping of indexes to character
|
addresses.
|
""""""
|
addresses = []
|
for text in self._iter_texts(tree):
|
for i, char in enumerate(text.content):
|
if char in whitespace:
|
char = ' '
|
addresses.append(CharAddress(char, text.element, text.attr, i))
|
# remove leading and trailing whitespace
|
while addresses and addresses[0].char == ' ':
|
del addresses[0]
|
while addresses and addresses[-1].char == ' ':
|
del addresses[-1]
|
return addresses"
|
4649,"def _validate_index(self, index):
|
""""""Validates given index, eventually raises errors.""""""
|
if isinstance(index, slice):
|
if index.step and index.step != 1:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.