NewHTML: solved centering and other stuff

This commit is contained in:
Lionel Dricot 2022-03-18 22:11:58 +01:00
parent a9b5c37b8a
commit 0e8557480a
1 changed files with 43 additions and 26 deletions

View File

@ -835,13 +835,14 @@ class HtmlRenderer(AbstractRenderer):
#we remember the position where to insert color codes #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")#+color+str(o))
def _endline(self): def _endline(self):
if len(self.last_line) >= 0: if len(self.last_line) > 0:
for c in self.opened: for c in self.opened:
self._insert(c,open=False) self._insert(c,open=False)
newline = "" newline = ""
added_char = 0
#we insert the color code at the saved positions #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()
@ -850,10 +851,13 @@ class HtmlRenderer(AbstractRenderer):
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
added_char += len(c)
self.last_line = self.last_line[:pos] self.last_line = self.last_line[:pos]
newline = self.last_line + newline newline = self.last_line + newline
if self.last_line_center: if self.last_line_center:
newline = newline.strip().center(term_width()) #we have to care about the ansi char while centering
width = term_width() + added_char
newline = newline.strip().center(width)
self.last_line_center = False self.last_line_center = False
else: else:
newline = newline.lstrip() newline = newline.lstrip()
@ -864,6 +868,7 @@ class HtmlRenderer(AbstractRenderer):
self._insert(c,open=True) self._insert(c,open=True)
else: else:
self.last_line = "" self.last_line = ""
@debug @debug
def center_line(self): def center_line(self):
@ -881,14 +886,17 @@ class HtmlRenderer(AbstractRenderer):
self.opened.remove(color) self.opened.remove(color)
@debug @debug
def close_all(self): def close_all(self):
self.last_line += "\x1b[0m" if len(self.colors) > 0:
self.opened.clear() self.last_line += "\x1b[0m"
self.opened.clear()
@debug @debug
def add_block(self,intext): def add_block(self,intext):
self._endline() if intext.strip("\n") != "":
for l in intext.splitlines():
self.final_text += l
self._endline() self._endline()
self.final_text += intext
#for l in intext.splitlines():
# self.final_text += l
self._endline()
@debug @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)
@ -997,7 +1005,7 @@ class HtmlRenderer(AbstractRenderer):
for child in element.children: for child in element.children:
r.add_block("\n") r.add_block("\n")
rendered_body += "\n" + title_tag + recursive_render(child) + "\x1b[0m" + "\n" rendered_body += "\n" + title_tag + recursive_render(child) + "\x1b[0m" + "\n"
r.add_block("\n") #r.add_block("\n")
r.close_all() r.close_all()
elif element.name in ["pre","code"]: elif element.name in ["pre","code"]:
rendered_body += "\n" rendered_body += "\n"
@ -1036,26 +1044,35 @@ class HtmlRenderer(AbstractRenderer):
elif element.name == "a": elif element.name == "a":
text = "" text = ""
link = element.get('href') link = element.get('href')
if link:
r.open_color("blue")
r.open_color("faint")
# support for images nested in links # support for images nested in links
for child in element.children:
if child.name == "img":
# recursive rendering seems to display some images twice
rendered_body += recursive_render(child)
else:
text += recursive_render(child,preformatted=preformatted)
if link: if link:
links.append(link+" "+text) for child in element.children:
link_id = " [%s]"%(len(links)) if child.name == "img":
rendered_body += "\x1b[2;34m" + text + link_id + "\x1b[0m" # recursive rendering seems to display some images twice
r.add_text(link_id) rendered_body += recursive_render(child)
r.close_color("blue") r.open_color("blue")
r.close_color("faint") r.open_color("faint")
text += "[IMG LINK]"
links.append(link+" "+text)
link_id = " [%s]"%(len(links))
rendered_body += "\x1b[2;34m" + text + link_id + "\x1b[0m"
r.center_line()
r.add_text(text+link_id)
r.close_color("blue")
r.close_color("faint")
else:
r.open_color("blue")
r.open_color("faint")
text += recursive_render(child,preformatted=preformatted)
links.append(link+" "+text)
link_id = " [%s]"%(len(links))
rendered_body += "\x1b[2;34m" + text + link_id + "\x1b[0m"
r.add_text(link_id)
r.close_color("blue")
r.close_color("faint")
else: else:
#No real link found #No real link found
rendered_body += text rendered_body += recursive_render(child,preformatted=preformatted)
elif element.name == "img": elif element.name == "img":
src = element.get("src") src = element.get("src")
text = "" text = ""
@ -1150,7 +1167,7 @@ class HtmlRenderer(AbstractRenderer):
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")
if r.debug_enabled: if r.debug_enabled:
print(r.get_final()[:30000]) print(r.get_final()[:40000])
#print("\n***** end of Internal representation") #print("\n***** end of Internal representation")
return r_body,links return r_body,links