mirror of https://github.com/zulip/zulip.git
dead code: Kill off most of html_branches.
The code that was removed here was intended for tooling that either never got built or became obsolete.
This commit is contained in:
parent
5a5dcd6962
commit
42061359a9
|
@ -1,60 +1,10 @@
|
||||||
import re
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import Dict, List, Optional, Sequence, Set
|
from typing import Dict, List
|
||||||
|
|
||||||
from .template_parser import FormattedException, Token, tokenize
|
from .template_parser import FormattedException, Token, tokenize
|
||||||
|
|
||||||
|
|
||||||
class HtmlTreeBranch:
|
|
||||||
"""
|
|
||||||
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: List["TagInfo"], fn: Optional[str]) -> None:
|
|
||||||
self.tags = tags
|
|
||||||
self.fn = fn
|
|
||||||
self.line = tags[-1].token.line
|
|
||||||
|
|
||||||
self.words: Set[str] = set()
|
|
||||||
for tag in tags:
|
|
||||||
for word in tag.words:
|
|
||||||
self.words.add(word)
|
|
||||||
|
|
||||||
def staircase_text(self) -> 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) -> str:
|
|
||||||
"""
|
|
||||||
produces one-line representation of branch:
|
|
||||||
|
|
||||||
html body.main-section p#intro
|
|
||||||
"""
|
|
||||||
return " ".join(t.text() for t in self.tags)
|
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
|
||||||
def __init__(self, token: Optional[Token], parent: "Optional[Node]") -> None:
|
|
||||||
# FIXME parent parameter is not used!
|
|
||||||
self.token = token
|
|
||||||
self.children: List[Node] = []
|
|
||||||
self.parent: Optional[Node] = None
|
|
||||||
|
|
||||||
|
|
||||||
class TagInfo:
|
class TagInfo:
|
||||||
def __init__(self, tag: str, classes: List[str], ids: List[str], token: Token) -> None:
|
def __init__(self, tag: str, classes: List[str], ids: List[str], token: Token) -> None:
|
||||||
self.tag = tag
|
self.tag = tag
|
||||||
|
@ -124,50 +74,6 @@ def split_for_id_and_class(element: str) -> List[str]:
|
||||||
return lst
|
return lst
|
||||||
|
|
||||||
|
|
||||||
def html_branches(text: str, fn: Optional[str] = None) -> List[HtmlTreeBranch]:
|
|
||||||
tree = html_tag_tree(text, fn)
|
|
||||||
branches: List[HtmlTreeBranch] = []
|
|
||||||
|
|
||||||
def walk(node: Node, tag_info_list: Sequence[TagInfo] = []) -> None:
|
|
||||||
assert node.token is not None
|
|
||||||
info = get_tag_info(node.token)
|
|
||||||
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, [])
|
|
||||||
|
|
||||||
return branches
|
|
||||||
|
|
||||||
|
|
||||||
def html_tag_tree(text: str, fn: Optional[str] = None) -> Node:
|
|
||||||
tokens = tokenize(text)
|
|
||||||
top_level = Node(token=None, parent=None)
|
|
||||||
stack = [top_level]
|
|
||||||
|
|
||||||
for token in tokens:
|
|
||||||
# Add tokens to the Node tree first (conditionally).
|
|
||||||
if token.kind in ("html_start", "html_singleton"):
|
|
||||||
parent = stack[-1]
|
|
||||||
node = Node(token=token, parent=parent)
|
|
||||||
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)
|
|
||||||
elif token.kind == "html_end":
|
|
||||||
stack.pop()
|
|
||||||
|
|
||||||
return top_level
|
|
||||||
|
|
||||||
|
|
||||||
def build_id_dict(templates: List[str]) -> (Dict[str, List[str]]):
|
def build_id_dict(templates: List[str]) -> (Dict[str, List[str]]):
|
||||||
template_id_dict: (Dict[str, List[str]]) = defaultdict(list)
|
template_id_dict: (Dict[str, List[str]]) = defaultdict(list)
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,7 @@ import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import tools.lib.template_parser
|
import tools.lib.template_parser
|
||||||
from tools.lib.html_branches import (
|
from tools.lib.html_branches import build_id_dict, get_tag_info, split_for_id_and_class
|
||||||
Node,
|
|
||||||
build_id_dict,
|
|
||||||
get_tag_info,
|
|
||||||
html_branches,
|
|
||||||
html_tag_tree,
|
|
||||||
split_for_id_and_class,
|
|
||||||
)
|
|
||||||
|
|
||||||
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
TEST_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_template_data")
|
TEST_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_template_data")
|
||||||
|
@ -28,93 +21,6 @@ class TestHtmlBranches(unittest.TestCase):
|
||||||
self.assertEqual(end_tag_info.text(), "p")
|
self.assertEqual(end_tag_info.text(), "p")
|
||||||
self.assertEqual(text.s, "foo")
|
self.assertEqual(text.s, "foo")
|
||||||
|
|
||||||
def test_html_tag_tree(self) -> None:
|
|
||||||
html = """
|
|
||||||
<!-- test -->
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<!-- test -->
|
|
||||||
<head>
|
|
||||||
<title>Test</title>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<link rel="stylesheet" href="style.css" />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>Hello<br>world!</p>
|
|
||||||
<p>Goodbye<!-- test -->world!</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
<!-- test -->
|
|
||||||
"""
|
|
||||||
|
|
||||||
tree = html_tag_tree(html)
|
|
||||||
|
|
||||||
def serialize(node: Node) -> object:
|
|
||||||
return (
|
|
||||||
node.token and (node.token.kind, node.token.tag),
|
|
||||||
[serialize(child) for child in node.children],
|
|
||||||
)
|
|
||||||
|
|
||||||
expected = (
|
|
||||||
None,
|
|
||||||
[
|
|
||||||
(
|
|
||||||
("html_start", "html"),
|
|
||||||
[
|
|
||||||
(
|
|
||||||
("html_start", "head"),
|
|
||||||
[
|
|
||||||
(("html_start", "title"), []),
|
|
||||||
(("html_singleton", "meta"), []),
|
|
||||||
(("html_singleton", "link"), []),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
("html_start", "body"),
|
|
||||||
[
|
|
||||||
(
|
|
||||||
("html_start", "p"),
|
|
||||||
[(("html_start", "br"), []), (("html_start", "p"), [])],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
self.assertEqual(serialize(tree), expected)
|
|
||||||
|
|
||||||
def test_html_branches(self) -> None:
|
|
||||||
html = """
|
|
||||||
<!-- test -->
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<!-- test -->
|
|
||||||
<head>
|
|
||||||
<title>Test</title>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<link rel="stylesheet" href="style.css" />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>Hello<br>world!</p>
|
|
||||||
<p>Goodbye<!-- test -->world!</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
<!-- test -->
|
|
||||||
"""
|
|
||||||
|
|
||||||
branches = html_branches(html)
|
|
||||||
self.assertEqual(
|
|
||||||
[(branch.text(), branch.staircase_text()) for branch in branches],
|
|
||||||
[
|
|
||||||
("html head title", "\n html\n head\n title\n"),
|
|
||||||
("html head meta", "\n html\n head\n meta\n"),
|
|
||||||
("html head link", "\n html\n head\n link\n"),
|
|
||||||
("html body p br", "\n html\n body\n p\n br\n"),
|
|
||||||
("html body p p", "\n html\n body\n p\n p\n"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_build_id_dict(self) -> None:
|
def test_build_id_dict(self) -> None:
|
||||||
templates = ["test_template1.html", "test_template2.html"]
|
templates = ["test_template1.html", "test_template2.html"]
|
||||||
templates = [os.path.join(TEST_TEMPLATES_DIR, fn) for fn in templates]
|
templates = [os.path.join(TEST_TEMPLATES_DIR, fn) for fn in templates]
|
||||||
|
|
Loading…
Reference in New Issue