NewHTML: trying to center img comments

This commit is contained in:
Lionel Dricot 2022-03-18 18:10:38 +01:00
parent f5c10392c3
commit 6b1b527b04
1 changed files with 44 additions and 14 deletions

View File

@ -797,11 +797,16 @@ class HtmlRenderer(AbstractRenderer):
#This is an experiment to rewrite the HTML renderer. Currently not used. #This is an experiment to rewrite the HTML renderer. Currently not used.
class representation: class representation:
def __init__(self,width): def __init__(self,width):
#The following is just there to disable this class while developing it.
#This should result in very minimal performance fee for users while
#having the code directly at hand (git branches? What git branches…)
self.debug_enabled = False
self.final_text = "" self.final_text = ""
self.opened = [] self.opened = []
self.width = width self.width = width
self.last_line = "" self.last_line = ""
self.last_line_colors = {} self.last_line_colors = {}
self.last_line_center = False
# each color is an [open,close] pair code # each color is an [open,close] pair code
self.colors = { "italic" : ["3","23"], self.colors = { "italic" : ["3","23"],
"bold" : ["1","22"], "bold" : ["1","22"],
@ -811,13 +816,23 @@ class HtmlRenderer(AbstractRenderer):
"yellow" : ["33","39"], "yellow" : ["33","39"],
} }
def debug(inner):
def outer(self, *args, **kwargs):
if not self.debug_enabled:
return ""
else:
return inner(self, *args, **kwargs)
outer.__doc__ = inner.__doc__
return outer
def _insert(self,color,open=True): def _insert(self,color,open=True):
if open: o = 0 if open: o = 0
else: o = 1 else: o = 1
#if color == "faint" and o == 1: #if color == "faint" and o == 1:
# print(self.last_line.replace("\x1b","x1b")) # print(self.last_line.replace("\x1b","x1b"))
#self.last_line += "\x1b["+self.colors[color][o]+"m"
pos = len(self.last_line) pos = len(self.last_line)
#we remember the position where to insert color codes
if not pos in self.last_line_colors: if not pos in self.last_line_colors:
self.last_line_colors[pos] = [] self.last_line_colors[pos] = []
self.last_line_colors[pos].append("\x1b["+self.colors[color][o]+"m") self.last_line_colors[pos].append("\x1b["+self.colors[color][o]+"m")
@ -826,40 +841,52 @@ class HtmlRenderer(AbstractRenderer):
for c in self.opened: for c in self.opened:
self._insert(c,open=False) self._insert(c,open=False)
newline = "" newline = ""
#we insert the color code at the saved positions
while len (self.last_line_colors) > 0: while len (self.last_line_colors) > 0:
pos,colors = self.last_line_colors.popitem() pos,colors = self.last_line_colors.popitem()
#we go, backward, to the pos (starting at the end of last_line) #popitem itterates LIFO.
#So we go, backward, to the pos (starting at the end of last_line)
newline = self.last_line[pos:] + newline newline = self.last_line[pos:] + newline
for c in colors: for c in colors:
newline = c + newline newline = c + newline
self.last_line = self.last_line[:pos] self.last_line = self.last_line[:pos]
newline = self.last_line + newline newline = self.last_line + newline
self.final_text += newline.lstrip() if self.last_line_center:
newline = newline.strip().center(term_width())
self.last_line_center = False
else:
newline = newline.lstrip()
self.final_text += newline
self.last_line = "" self.last_line = ""
self.final_text += "\n" self.final_text += "\n"
for c in self.opened: for c in self.opened:
self._insert(c,open=True) self._insert(c,open=True)
@debug
def center_line(self):
self.last_line_center = True
@debug
def open_color(self,color): def open_color(self,color):
if color in self.colors and color not in self.opened: if color in self.colors and color not in self.opened:
self._insert(color,open=True) self._insert(color,open=True)
self.opened.append(color) self.opened.append(color)
@debug
def close_color(self,color): def close_color(self,color):
if color in self.colors and color in self.opened: if color in self.colors and color in self.opened:
self._insert(color,open=False) self._insert(color,open=False)
self.opened.remove(color) self.opened.remove(color)
@debug
def close_all(self): def close_all(self):
self.last_line += "\x1b[0m" self.last_line += "\x1b[0m"
self.opened.clear() self.opened.clear()
@debug
def add_block(self,intext): def add_block(self,intext):
self._endline() self._endline()
for l in intext.splitlines(): for l in intext.splitlines():
self.final_text += l self.final_text += l
self._endline() self._endline()
@debug
def add_text(self,intext): def add_text(self,intext):
#print("current_line is %s" %self.current_line) #print("current_line is %s" %self.current_line)
lines = [] lines = []
@ -873,8 +900,9 @@ class HtmlRenderer(AbstractRenderer):
self.last_line += l.strip() self.last_line += l.strip()
self._endline() self._endline()
if len(lines) == 1: if len(lines) == 1:
self.last_line = lines[0] li = lines[0]
self.last_line = li
@debug
def get_final(self): def get_final(self):
self.close_all() self.close_all()
self.final_text += self.last_line self.final_text += self.last_line
@ -947,7 +975,7 @@ class HtmlRenderer(AbstractRenderer):
div = "" div = ""
for child in element.children: for child in element.children:
div += recursive_render(child,indent=indent) div += recursive_render(child,indent=indent)
rendered_body += div#.strip() (this strip doesnt play well with centered images) rendered_body += div
r.add_block("\n\n") r.add_block("\n\n")
rendered_body += "\n\n" rendered_body += "\n\n"
elif element.name in ["h1","h2","h3","h4","h5","h6"]: elif element.name in ["h1","h2","h3","h4","h5","h6"]:
@ -1043,6 +1071,7 @@ class HtmlRenderer(AbstractRenderer):
r.add_block(ansi_img) r.add_block(ansi_img)
r.open_color("faint") r.open_color("faint")
r.open_color("yellow") r.open_color("yellow")
r.center_line()
rendered_body = ansi_img + "\x1b[2;33m" + alttext + "\x1b[0m\n\n" rendered_body = ansi_img + "\x1b[2;33m" + alttext + "\x1b[0m\n\n"
r.add_text(alttext) r.add_text(alttext)
r.close_color("faint") r.close_color("faint")
@ -1116,9 +1145,10 @@ class HtmlRenderer(AbstractRenderer):
r_body = title + "\n" + r_body r_body = title + "\n" + r_body
#We try to avoid huge empty gaps in the page #We try to avoid huge empty gaps in the page
r_body = r_body.replace("\n\n\n\n","\n\n").replace("\n\n\n","\n\n") r_body = r_body.replace("\n\n\n\n","\n\n").replace("\n\n\n","\n\n")
print("***** Internal representation:\n") #print("***** Internal representation:\n")
print(r.get_final()[:20000]) if r.debug_enabled:
print("\n***** end of Internal representation") print(r.get_final()[:30000])
#print("\n***** end of Internal representation")
return r_body,links return r_body,links
# Mapping mimetypes with renderers # Mapping mimetypes with renderers