2017-03-03 19:01:52 +01:00
|
|
|
from typing import Dict, List, Optional, Set
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
import re
|
2017-01-06 15:11:15 +01:00
|
|
|
from collections import defaultdict
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
from .template_parser import (
|
|
|
|
tokenize,
|
2016-09-12 00:30:10 +02:00
|
|
|
Token,
|
2016-09-11 20:23:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class HtmlBranchesException(Exception):
|
|
|
|
# TODO: Have callers pass in line numbers.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class HtmlTreeBranch:
|
2016-09-11 20:23:29 +02:00
|
|
|
"""
|
|
|
|
For <p><div id='yo'>bla<span class='bar'></span></div></p>, store a
|
|
|
|
representation of the tags all the way down to the leaf, which would
|
|
|
|
conceptually be something like "p div(#yo) span(.bar)".
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, tags, fn):
|
2017-10-05 08:05:41 +02:00
|
|
|
# type: (List['TagInfo'], Optional[str]) -> None
|
2016-09-11 20:23:29 +02:00
|
|
|
self.tags = tags
|
|
|
|
self.fn = fn
|
|
|
|
self.line = tags[-1].token.line
|
|
|
|
|
|
|
|
self.words = set() # type: Set[str]
|
|
|
|
for tag in tags:
|
|
|
|
for word in tag.words:
|
|
|
|
self.words.add(word)
|
|
|
|
|
|
|
|
def staircase_text(self):
|
|
|
|
# type: () -> str
|
|
|
|
"""
|
|
|
|
produces representation of a node in staircase-like format:
|
|
|
|
|
|
|
|
html
|
|
|
|
body.main-section
|
|
|
|
p#intro
|
|
|
|
|
|
|
|
"""
|
|
|
|
res = '\n'
|
|
|
|
indent = ' ' * 4
|
|
|
|
for t in self.tags:
|
|
|
|
res += indent + t.text() + '\n'
|
|
|
|
indent += ' ' * 4
|
|
|
|
return res
|
|
|
|
|
|
|
|
def text(self):
|
|
|
|
# type: () -> str
|
|
|
|
"""
|
|
|
|
produces one-line representation of branch:
|
|
|
|
|
|
|
|
html body.main-section p#intro
|
|
|
|
"""
|
|
|
|
return ' '.join(t.text() for t in self.tags)
|
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class Node:
|
2017-08-09 20:44:32 +02:00
|
|
|
def __init__(self, token, parent): # FIXME parent parameter is not used!
|
|
|
|
# type: (Token, Optional[Node]) -> None
|
2016-09-11 20:23:29 +02:00
|
|
|
self.token = token
|
|
|
|
self.children = [] # type: List[Node]
|
|
|
|
self.parent = None # type: Optional[Node]
|
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class TagInfo:
|
2016-09-11 20:23:29 +02:00
|
|
|
def __init__(self, tag, classes, ids, token):
|
|
|
|
# type: (str, List[str], List[str], Token) -> None
|
|
|
|
self.tag = tag
|
|
|
|
self.classes = classes
|
|
|
|
self.ids = ids
|
|
|
|
self.token = token
|
|
|
|
self.words = \
|
|
|
|
[self.tag] + \
|
|
|
|
['.' + s for s in classes] + \
|
|
|
|
['#' + s for s in ids]
|
|
|
|
|
|
|
|
def text(self):
|
|
|
|
# type: () -> str
|
|
|
|
s = self.tag
|
|
|
|
if self.classes:
|
|
|
|
s += '.' + '.'.join(self.classes)
|
|
|
|
if self.ids:
|
|
|
|
s += '#' + '#'.join(self.ids)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def get_tag_info(token):
|
|
|
|
# type: (Token) -> TagInfo
|
|
|
|
s = token.s
|
|
|
|
tag = token.tag
|
|
|
|
classes = [] # type: List[str]
|
|
|
|
ids = [] # type: List[str]
|
|
|
|
|
|
|
|
searches = [
|
|
|
|
(classes, ' class="(.*?)"'),
|
|
|
|
(classes, " class='(.*?)'"),
|
|
|
|
(ids, ' id="(.*?)"'),
|
|
|
|
(ids, " id='(.*?)'"),
|
|
|
|
]
|
|
|
|
|
|
|
|
for lst, regex in searches:
|
|
|
|
m = re.search(regex, s)
|
|
|
|
if m:
|
|
|
|
for g in m.groups():
|
2017-01-06 15:11:15 +01:00
|
|
|
lst += split_for_id_and_class(g)
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
return TagInfo(tag=tag, classes=classes, ids=ids, token=token)
|
|
|
|
|
|
|
|
|
2017-01-06 15:11:15 +01:00
|
|
|
def split_for_id_and_class(element):
|
|
|
|
# type: (str) -> List[str]
|
|
|
|
# Here we split a given string which is expected to contain id or class
|
|
|
|
# attributes from HTML tags. This also takes care of template variables
|
|
|
|
# in string during splitting process. For eg. 'red black {{ a|b|c }}'
|
|
|
|
# is split as ['red', 'black', '{{ a|b|c }}']
|
2017-05-07 16:54:55 +02:00
|
|
|
outside_braces = True # type: bool
|
2017-01-06 15:11:15 +01:00
|
|
|
lst = []
|
|
|
|
s = ''
|
|
|
|
|
|
|
|
for ch in element:
|
|
|
|
if ch == '{':
|
|
|
|
outside_braces = False
|
|
|
|
if ch == '}':
|
|
|
|
outside_braces = True
|
|
|
|
if ch == ' ' and outside_braces:
|
|
|
|
if not s == '':
|
|
|
|
lst.append(s)
|
|
|
|
s = ''
|
|
|
|
else:
|
|
|
|
s += ch
|
|
|
|
if not s == '':
|
|
|
|
lst.append(s)
|
|
|
|
|
|
|
|
return lst
|
|
|
|
|
|
|
|
|
2016-09-11 20:23:29 +02:00
|
|
|
def html_branches(text, fn=None):
|
2017-08-09 20:44:32 +02:00
|
|
|
# type: (str, Optional[str]) -> List[HtmlTreeBranch]
|
2016-09-11 20:23:29 +02:00
|
|
|
tree = html_tag_tree(text)
|
|
|
|
branches = [] # type: List[HtmlTreeBranch]
|
|
|
|
|
|
|
|
def walk(node, tag_info_list=None):
|
2017-03-03 20:30:49 +01:00
|
|
|
# type: (Node, Optional[List[TagInfo]]) -> None
|
2016-09-11 20:23:29 +02:00
|
|
|
info = get_tag_info(node.token)
|
|
|
|
if tag_info_list is None:
|
|
|
|
tag_info_list = [info]
|
|
|
|
else:
|
|
|
|
tag_info_list = tag_info_list[:] + [info]
|
|
|
|
|
|
|
|
if node.children:
|
|
|
|
for child in node.children:
|
|
|
|
walk(node=child, tag_info_list=tag_info_list)
|
|
|
|
else:
|
|
|
|
tree_branch = HtmlTreeBranch(tags=tag_info_list, fn=fn)
|
|
|
|
branches.append(tree_branch)
|
|
|
|
|
|
|
|
for node in tree.children:
|
|
|
|
walk(node, None)
|
|
|
|
|
|
|
|
return branches
|
|
|
|
|
|
|
|
|
|
|
|
def html_tag_tree(text):
|
|
|
|
# type: (str) -> Node
|
|
|
|
tokens = tokenize(text)
|
|
|
|
top_level = Node(token=None, parent=None)
|
|
|
|
stack = [top_level]
|
|
|
|
|
|
|
|
for token in tokens:
|
2016-09-11 22:36:55 +02:00
|
|
|
# Add tokens to the Node tree first (conditionally).
|
2016-09-11 20:23:29 +02:00
|
|
|
if token.kind in ('html_start', 'html_singleton'):
|
2016-09-11 22:36:55 +02:00
|
|
|
parent = stack[-1]
|
2016-11-28 23:29:01 +01:00
|
|
|
node = Node(token=token, parent=parent)
|
2016-09-11 22:36:55 +02:00
|
|
|
parent.children.append(node)
|
|
|
|
|
|
|
|
# Then update the stack to have the next node that
|
|
|
|
# we will be appending to at the top.
|
|
|
|
if token.kind == 'html_start':
|
|
|
|
stack.append(node)
|
2016-09-11 20:23:29 +02:00
|
|
|
elif token.kind == 'html_end':
|
|
|
|
stack.pop()
|
|
|
|
|
|
|
|
return top_level
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def build_id_dict(templates):
|
2017-10-05 08:05:41 +02:00
|
|
|
# type: (List[str]) -> (Dict[str, List[str]])
|
|
|
|
template_id_dict = defaultdict(list) # type: (Dict[str, List[str]])
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
for fn in templates:
|
2019-07-14 21:37:08 +02:00
|
|
|
with open(fn, 'r') as f:
|
|
|
|
text = f.read()
|
2017-01-06 15:11:15 +01:00
|
|
|
list_tags = tokenize(text)
|
|
|
|
|
|
|
|
for tag in list_tags:
|
|
|
|
info = get_tag_info(tag)
|
|
|
|
|
|
|
|
for ids in info.ids:
|
|
|
|
template_id_dict[ids].append("Line " + str(info.token.line) + ":" + fn)
|
|
|
|
|
|
|
|
return template_id_dict
|