support for preformatted them - close #38

This commit is contained in:
Ploum 2024-01-31 17:25:33 +01:00
parent 2faf460f0f
commit 1bf98cf060
2 changed files with 20 additions and 7 deletions

View File

@ -12,7 +12,8 @@
- offpunk: "gus" was broken, it is functionnal again - offpunk: "gus" was broken, it is functionnal again
- opnk/offpunk: more informative prompt in less - opnk/offpunk: more informative prompt in less
- ansicat: added support for HTML description elements <dt> and <dd> (by Bert Livens) - ansicat: added support for HTML description elements <dt> and <dd> (by Bert Livens)
- opnk: added "--mode" command-line argument - opnk: added "--mode" command-line argument (bug #37)
- offpunk: support for "preformatted" theming (bug #38)
## 2.1 - December 15th 2023 ## 2.1 - December 15th 2023
- freshly updated gemtext/rss links are highlighted ("new_link" theme option) - freshly updated gemtext/rss links are highlighted ("new_link" theme option)

View File

@ -365,15 +365,27 @@ class AbstractRenderer():
# Beware, blocks are not wrapped nor indented and left untouched! # Beware, blocks are not wrapped nor indented and left untouched!
# They are mostly useful for pictures and preformatted text. # They are mostly useful for pictures and preformatted text.
def add_block(self,intext): def add_block(self,intext,theme=None):
# If necessary, we add the title before a block # If necessary, we add the title before a block
self._title_first() self._title_first()
# we dont want to indent blocks # we dont want to indent blocks
self._endline() self._endline()
self._disable_indents() self._disable_indents()
self.final_text += self.current_indent + intext #we have to apply the theme for every line in the intext
self.new_paragraph = False #applying theme to preformatted is controversial as it could change it
self._endline() if theme:
block = ""
lines = intext.split("\n")
for l in lines:
self.open_theme(theme)
self.last_line += self.current_indent + l
self.close_theme(theme)
self._endline()
#one thing is sure : we need to keep unthemed blocks for images!
else:
self.final_text += self.current_indent + intext
self.new_paragraph = False
self._endline()
self._enable_indents() self._enable_indents()
def add_text(self,intext): def add_text(self,intext):
@ -641,7 +653,7 @@ class GemtextRenderer(AbstractRenderer):
r.close_theme("preformatted") r.close_theme("preformatted")
elif preformatted: elif preformatted:
# infinite line to not wrap preformated # infinite line to not wrap preformated
r.add_block(line+"\n") r.add_block(line+"\n",theme="preformatted")
elif len(line.strip()) == 0: elif len(line.strip()) == 0:
r.newparagraph(force=True) r.newparagraph(force=True)
elif line.startswith("=>"): elif line.startswith("=>"):
@ -1122,7 +1134,7 @@ class HtmlRenderer(AbstractRenderer):
recursive_render(child,indent=indent,preformatted=True) recursive_render(child,indent=indent,preformatted=True)
elif element.name in ["pre"]: elif element.name in ["pre"]:
r.newparagraph() r.newparagraph()
r.add_block(element.text) r.add_block(element.text,theme="preformatted")
r.newparagraph() r.newparagraph()
elif element.name in ["li"]: elif element.name in ["li"]:
r.startindent("",sub=" ") r.startindent("",sub=" ")