2023-07-04 18:28:49 +02:00

47 lines
2.7 KiB
Python

import se
def render(text, info = None):
finder = se.Finder()
patterns = ["c(<start>> \n>)v()c(\n <end>)","#v(range 0,9)c(\s \r \n <end>)","[v()]","https://v()c(<end> \n \s)"]
p_green, p_tlink, p_title, p_link = patterns
form_green = '<span style="color:#70AD47;text-align:justify">{text}</span>'
form_tlink = '<a href={thread_link} style:"color:#7c7c7c;background-color: transparent;text-decoration:none"><span style="text-align:justify">{text}</span></a>'
form_smartlink = '''<a href="{linkstart} document.getElementById('{id}').firstElementChild.style.backgroundColor='#FFEFCD'; window.open('{thread_link}','_self')" style:"color:#7c7c7c;background-color: transparent;text-decoration:none"><span style="text-align:justify">{text}</span></a>'''
form_title = '<b><span style="font-size:18.0pt;text-align:justify">{text}</span></b>'
form_link = '<a href="{link}" style:"color:#7c7c7c;background-color: transparent;text-decoration:none"><span style="text-align:justify">{text}</span></a>'
ps = text.split("\n")
for n, p in enumerate(ps):
#green
indexes = finder.get_indexes_tuples(p,p_green)
matches = [p[i[0]:i[1]] for i in indexes]
for match in matches:
p = p.replace(match, form_green.format(text = match))
#replies
indexes = finder.get_indexes_tuples(p,p_tlink)
matches = [p[i[0]:i[1]] for i in indexes]
for match in matches:
link = f"/{info['board']}/{info['thread']}{match}"
number = match
while not number[0] in "0123456789":
number = number[1:]
while not number[-1] in "0123456789":
number = number[:-1]
p = p.replace(match, form_smartlink.format( text = match , id = number, thread_link = link, linkstart = "javascript:for (const post of document.getElementsByClassName('post')) {post.firstElementChild.style.backgroundColor='white';};"))#form_tlink.format(text = match, thread_link = link)
#titles
indexes = finder.get_indexes_tuples(p,p_title)
matches = [p[i[0]:i[1]] for i in indexes]
for match in matches:
p = p.replace(match, form_title.format(text = match))
#outer links
indexes = finder.get_indexes_tuples(p,p_link)
matches = [p[i[0]:i[1]] for i in indexes]
for match in matches:
link = match if not match[-1] in [" ","\n"] else match[:-1]
p = p.replace(match, form_link.format(text = match, link = link))
ps[n] = f"<p style='margin: 0; max-width: 700px;'>{p}</p>"
return "\n".join(ps)
if __name__=="__main__":
d = {"board": "ran", "thread": 1}
print(render(">Sample Text\n>Amongus\n#11\n[SUS]\nhttps://www.litechan.org/ran/67", d))