[Sync] Kazimir fixes quote inside A links
This commit is contained in:
1 parent
9820b4a1c2
commit
32da2fa816
1 file changed
+55
-1
@@ -6,6 +6,11 @@ from kazimir.figure import FigureExtension
|
||||
from kazimir.instagram import InstagramExtension
|
||||
from sync.data import request
|
||||
|
||||
quote_pairs = [
|
||||
('"', '"'),
|
||||
('«', '»'),
|
||||
]
|
||||
|
||||
|
||||
def markdown_to_html(text):
|
||||
return markdown(text, extensions=[
|
||||
@@ -28,7 +33,56 @@ def create_tree(text: str) -> str:
|
||||
tokens = join_tokens(tokens)
|
||||
tokens = merge_tokens(tokens)
|
||||
|
||||
return compile_tokens(tokens)
|
||||
kz = compile_tokens(tokens)
|
||||
tree = lxml.html.fromstring(kz)
|
||||
return create_tree(tree)
|
||||
|
||||
|
||||
def clean_tree(tree):
|
||||
return fix_links_quotes(tree)
|
||||
|
||||
|
||||
def fix_links_quotes(tree):
|
||||
for link in tree.iterlinks():
|
||||
a = link[0]
|
||||
if a.tag != 'a':
|
||||
continue
|
||||
|
||||
quote = get_quote_pair(a.text)
|
||||
if not quote:
|
||||
continue
|
||||
|
||||
# New text of A without quotes
|
||||
a.text = a.text[1:-1]
|
||||
|
||||
# Move first quote out of A to the prefix
|
||||
a.getparent().text = a.getparent().text + quote[0]
|
||||
|
||||
# Move last quote out of A to the suffix
|
||||
a.tail = quote[1] + a.tail
|
||||
return tree
|
||||
|
||||
|
||||
def get_quote_pair(text: str):
|
||||
"""
|
||||
sample text -> [s]ample tex[t] -> (s, t) -> match pair from quote_pair
|
||||
? (s, t)
|
||||
: None
|
||||
|
||||
:param text:
|
||||
:return:
|
||||
"""
|
||||
if len(text) < 2:
|
||||
return None
|
||||
|
||||
first = text[0]
|
||||
last = text[-1]
|
||||
|
||||
for pair_first, pair_last in quote_pairs:
|
||||
if pair_first == first and pair_last == last:
|
||||
return first, last
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def html_from_tree(tree):
|
||||
|
||||
Reference in new issue
Block a user