NewHTML: handling PRE, indents and tables

This commit is contained in:
Lionel Dricot 2022-03-20 19:25:04 +01:00
parent ce36d1adbc
commit bf77dec666
1 changed files with 65 additions and 27 deletions

View File

@ -264,11 +264,13 @@ else:
# -M : long prompt (to have info about where you are in the file) # -M : long prompt (to have info about where you are in the file)
# -w : hilite the new first line after a page skip (space) # -w : hilite the new first line after a page skip (space)
# -i : ignore case in search # -i : ignore case in search
# -S : do not wrap long lines. Wrapping is done by offpunk, longlines
# are there on purpose (surch in asciiart)
#--incsearch : incremental search starting rev581 #--incsearch : incremental search starting rev581
if less_version >= 581: if less_version >= 581:
less_base = "less --incsearch --save-marks -XRfMwi" less_base = "less --incsearch --save-marks -~ -XRfMwiS"
else: else:
less_base = "less --save-marks -XRfMwi" less_base = "less --save-marks -XRfMwiS"
_DEFAULT_LESS = less_base + " \"+''\" %s" _DEFAULT_LESS = less_base + " \"+''\" %s"
_DEFAULT_CAT = less_base + " -EF %s" _DEFAULT_CAT = less_base + " -EF %s"
def less_cmd(file, histfile=None,cat=False): def less_cmd(file, histfile=None,cat=False):
@ -816,6 +818,7 @@ class HtmlRenderer(AbstractRenderer):
self.s_indent = "" self.s_indent = ""
self.r_indent = "" self.r_indent = ""
self.current_indent = "" self.current_indent = ""
self.disabled_indents = None
# 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"],
@ -838,8 +841,6 @@ class HtmlRenderer(AbstractRenderer):
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:
# print(self.last_line.replace("\x1b","x1b"))
pos = len(self.last_line) pos = len(self.last_line)
#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:
@ -849,8 +850,12 @@ class HtmlRenderer(AbstractRenderer):
self.last_line_colors[pos].remove([color,int(not o)]) self.last_line_colors[pos].remove([color,int(not o)])
else: else:
self.last_line_colors[pos].append([color,o])#+color+str(o)) self.last_line_colors[pos].append([color,o])#+color+str(o))
# Take self.last line and add ANSI codes to it before adding it to
# self.final_text.
def _endline(self,newline=True): def _endline(self,newline=True):
if "Date Prev" in self.last_line:
print("We end **%s**"%self.last_line)
if len(self.last_line.strip()) > 0: if len(self.last_line.strip()) > 0:
for c in self.opened: for c in self.opened:
self._insert(c,open=False) self._insert(c,open=False)
@ -930,6 +935,22 @@ class HtmlRenderer(AbstractRenderer):
self.r_indent = "" self.r_indent = ""
self.current_indent = "" self.current_indent = ""
def _disable_indents(self):
self.disabled_indents = []
self.disabled_indents.append(self.current_indent)
self.disabled_indents.append(self.i_indent)
self.disabled_indents.append(self.s_indent)
self.disabled_indents.append(self.r_indent)
self.endindent()
def _enable_indents(self):
if self.disabled_indents:
self.current_indent = self.disabled_indents[0]
self.i_indent = self.disabled_indents[1]
self.s_indent = self.disabled_indents[2]
self.r_indent = self.disabled_indents[3]
self.disabled_indents = None
@debug @debug
def newline(self): def newline(self):
self._endline() self._endline()
@ -944,33 +965,40 @@ class HtmlRenderer(AbstractRenderer):
self.final_text += "\n" self.final_text += "\n"
self.new_paragraph = True self.new_paragraph = True
def _title_first(self,intext): def _title_first(self,intext=None):
if self.title: if self.title:
if not self.title == intext: if not self.title == intext:
self._disable_indents()
self.open_color("blue") self.open_color("blue")
self.open_color("bold") self.open_color("bold")
self.open_color("underline") self.open_color("underline")
self.add_text(self.title) self.add_text(self.title)
self.close_all() self.close_all()
self.newparagraph() self.newparagraph()
self._enable_indents()
self.title = None self.title = None
@debug @debug
# Beware, blocks are not wrapped and left untouched! # Beware, blocks are not wrapped nor indented and left untouched!
# They are mostly useful for pictures # They are mostly useful for pictures
def add_block(self,intext): def add_block(self,intext):
# We always add the title before a block # We always add the title before a block
self._title_first(None) self._title_first()
if intext.strip("\n") != "": # we dont want to indent blocks
self._endline(newline=False) self._endline(newline=False)
self._disable_indents()
if intext.strip("\n").strip() != "":
self.final_text += self.current_indent + intext self.final_text += self.current_indent + intext
self._endline() self._endline()
self._enable_indents()
@debug @debug
def add_text(self,intext): def add_text(self,intext):
self._title_first(intext) self._title_first(intext=intext)
lines = [] lines = []
last = self.last_line + intext last = (self.last_line + intext).lstrip()
self.last_line = "" self.last_line = ""
if len(last) > 0: if len(last) > 0:
self.new_paragraph = False self.new_paragraph = False
@ -985,14 +1013,14 @@ class HtmlRenderer(AbstractRenderer):
li = lines[0] li = lines[0]
self.last_line = li.lstrip() self.last_line = li.lstrip()
else: else:
self.last_line = last self.last_line = last.lstrip()
@debug @debug
def get_final(self): def get_final(self):
self.close_all() self.close_all()
self.final_text += self.last_line self._endline()
#self.final_text = self.final_text.replace("\n\n\n\n","\n\n").replace("\n\n\n","\n\n") #if no content, we still add the title
self.last_line = "" self._title_first()
lines = self.final_text.splitlines() lines = self.final_text.splitlines()
lines2 = [] lines2 = []
termspace = shutil.get_terminal_size()[0] termspace = shutil.get_terminal_size()[0]
@ -1056,14 +1084,15 @@ class HtmlRenderer(AbstractRenderer):
def recursive_render(element,indent="",preformatted=False): def recursive_render(element,indent="",preformatted=False):
rendered_body = "" rendered_body = ""
if element.name == "blockquote": if element.name == "blockquote":
r.newparagraph()
r.startindent(" ",reverse=" ")
for child in element.children: for child in element.children:
rendered_body += "\x1b[3m" rendered_body += "\x1b[3m"
r.open_color("italic") r.open_color("italic")
r.startindent(" ",reverse=" ")
rendered_body += recursive_render(child,indent="\t").rstrip("\t") rendered_body += recursive_render(child,indent="\t").rstrip("\t")
rendered_body += "\x1b[23m" rendered_body += "\x1b[23m"
r.close_color("italic") r.close_color("italic")
r.endindent() r.endindent()
elif element.name in ["div","p"]: elif element.name in ["div","p"]:
rendered_body += "\n" rendered_body += "\n"
r.newparagraph() r.newparagraph()
@ -1089,24 +1118,37 @@ class HtmlRenderer(AbstractRenderer):
r.newparagraph() r.newparagraph()
rendered_body += "\n" + title_tag + recursive_render(child) + "\x1b[0m" + "\n" rendered_body += "\n" + title_tag + recursive_render(child) + "\x1b[0m" + "\n"
r.close_all() r.close_all()
elif element.name in ["pre","code"]: elif element.name in ["code","tt"]:
rendered_body += "\n" rendered_body += "\n"
#r.newparagraph()
for child in element.children: for child in element.children:
rendered_body += recursive_render(child,indent=indent,preformatted=True) rendered_body += recursive_render(child,indent=indent,preformatted=True)
rendered_body += "\n\n" rendered_body += "\n\n"
elif element.name in ["li","tr"]: elif element.name in ["pre"]:
rendered_body += "\n"
rendered_body += element.text
r.add_block(element.text)
rendered_body += "\n\n"
elif element.name in ["li"]:
line = "" line = ""
r.startindent("",sub=" ") r.startindent("",sub=" ")
for child in element.children: for child in element.children:
line += recursive_render(child,indent=indent).strip("\n") line += recursive_render(child,indent=indent).strip("\n")
rendered_body += " * " + line.strip() + "\n" rendered_body += " * " + line.strip() + "\n"
r.endindent() r.endindent()
elif element.name in ["td"]: elif element.name in ["tr"]:
line = ""
r.startindent("|",reverse="|")
for child in element.children:
line += recursive_render(child,indent=indent).strip("\n")
rendered_body += " " + line.strip() + "\n"
r.endindent()
elif element.name in ["td","th"]:
line = "| " line = "| "
r.add_text("| ")
for child in element.children: for child in element.children:
line += recursive_render(child) line += recursive_render(child)
line += " |" line += " |"
r.add_text(" |")
rendered_body += line rendered_body += line
# italics # italics
elif element.name in ["em","i"]: elif element.name in ["em","i"]:
@ -1189,10 +1231,8 @@ class HtmlRenderer(AbstractRenderer):
if element.string: if element.string:
if preformatted : if preformatted :
rendered_body = element.string rendered_body = element.string
r.open_color("italic")
r.open_color("faint") r.open_color("faint")
r.add_text(element.string) r.add_text(element.string)
r.close_color("italic")
r.close_color("faint") r.close_color("faint")
else: else:
s = sanitize_string(element.string) s = sanitize_string(element.string)
@ -1256,10 +1296,8 @@ 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")
if BETA: if BETA:
r_body = r.get_final() r_body = r.get_final()
#print("\n***** end of Internal representation")
return r_body,links return r_body,links
# Mapping mimetypes with renderers # Mapping mimetypes with renderers