switched Gemtext to the new renderer
This commit is contained in:
parent
17066d0f86
commit
68fb345ccc
91
offpunk.py
91
offpunk.py
|
@ -425,7 +425,7 @@ class AbstractRenderer():
|
||||||
|
|
||||||
# Take self.last line and add ANSI codes to it before adding it to
|
# Take self.last line and add ANSI codes to it before adding it to
|
||||||
# self.final_text.
|
# self.final_text.
|
||||||
def _endline(self,newline=True):
|
def _endline(self):
|
||||||
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)
|
||||||
|
@ -451,14 +451,14 @@ class AbstractRenderer():
|
||||||
nextline = nextline.strip().center(width)
|
nextline = nextline.strip().center(width)
|
||||||
self.last_line_center = False
|
self.last_line_center = False
|
||||||
else:
|
else:
|
||||||
nextline = self.current_indent + nextline.lstrip() + self.r_indent
|
#should we lstrip the nextline in the addition ?
|
||||||
|
nextline = self.current_indent + nextline + self.r_indent
|
||||||
self.current_indent = self.s_indent
|
self.current_indent = self.s_indent
|
||||||
self.final_text += nextline
|
self.final_text += nextline
|
||||||
self.last_line = ""
|
self.last_line = ""
|
||||||
if newline:
|
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)
|
|
||||||
else:
|
else:
|
||||||
self.last_line = ""
|
self.last_line = ""
|
||||||
|
|
||||||
|
@ -520,10 +520,10 @@ class AbstractRenderer():
|
||||||
self._endline()
|
self._endline()
|
||||||
|
|
||||||
#A new paragraph implies 2 newlines
|
#A new paragraph implies 2 newlines
|
||||||
#But it is only used if didn’t already started one
|
#But it is only used if didn’t already started one (except if forced)
|
||||||
#new_paragraph becomes false as soon as text is entered into it
|
#new_paragraph becomes false as soon as text is entered into it
|
||||||
def newparagraph(self):
|
def newparagraph(self,force=False):
|
||||||
if not self.new_paragraph:
|
if force or not self.new_paragraph:
|
||||||
self._endline()
|
self._endline()
|
||||||
self.final_text += "\n"
|
self.final_text += "\n"
|
||||||
self.new_paragraph = True
|
self.new_paragraph = True
|
||||||
|
@ -543,38 +543,43 @@ 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
|
# They are mostly useful for pictures
|
||||||
def add_block(self,intext,wrap=False):
|
def add_block(self,intext):
|
||||||
# We always add the title before a block
|
# We always add the title before a block
|
||||||
self._title_first()
|
self._title_first()
|
||||||
# we don’t want to indent blocks
|
# we don’t want to indent blocks
|
||||||
self._endline(newline=False)
|
self._endline()
|
||||||
self._disable_indents()
|
self._disable_indents()
|
||||||
if intext.strip("\n").strip() != "":
|
#if intext.strip("\n").strip() != "":
|
||||||
if wrap:
|
# if wrap:
|
||||||
intext = textwrap.fill(intext)
|
# intext = textwrap.fill(intext)
|
||||||
|
if True:
|
||||||
self.final_text += self.current_indent + intext
|
self.final_text += self.current_indent + intext
|
||||||
|
self.new_paragraph = False
|
||||||
self._endline()
|
self._endline()
|
||||||
self._enable_indents()
|
self._enable_indents()
|
||||||
|
|
||||||
def add_text(self,intext):
|
def add_text(self,intext):
|
||||||
self._title_first(intext=intext)
|
self._title_first(intext=intext)
|
||||||
lines = []
|
lines = []
|
||||||
last = (self.last_line + intext).lstrip()
|
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
|
||||||
if len(last) > self.width:
|
if len(last) > self.width:
|
||||||
width = self.width - len(self.current_indent) - len(self.r_indent)
|
width = self.width - len(self.current_indent) - len(self.r_indent)
|
||||||
lines = textwrap.wrap(last,width,drop_whitespace=False)
|
spaces_left = len(last) - len(last.lstrip())
|
||||||
|
spaces_right = len(last) - len(last.rstrip())
|
||||||
|
lines = textwrap.wrap(last,width,drop_whitespace=True)
|
||||||
|
self.last_line += spaces_left*" "
|
||||||
while len(lines) > 1:
|
while len(lines) > 1:
|
||||||
l = lines.pop(0)
|
l = lines.pop(0)
|
||||||
self.last_line = l.lstrip()
|
self.last_line += l#.lstrip()
|
||||||
self._endline()
|
self._endline()
|
||||||
if len(lines) == 1:
|
if len(lines) == 1:
|
||||||
li = lines[0]
|
li = lines[0]
|
||||||
self.last_line = li.lstrip()
|
self.last_line += li + spaces_right*" "
|
||||||
else:
|
else:
|
||||||
self.last_line = last.lstrip()
|
self.last_line = last#.lstrip()
|
||||||
|
|
||||||
def get_final(self):
|
def get_final(self):
|
||||||
self.close_all()
|
self.close_all()
|
||||||
|
@ -685,23 +690,6 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
r = self.representation(width)
|
r = self.representation(width)
|
||||||
links = []
|
links = []
|
||||||
preformatted = False
|
preformatted = False
|
||||||
rendered_text = ""
|
|
||||||
#This local method takes a line and apply the ansi code given as "color"
|
|
||||||
#The whole line is then wrapped and ansi code are ended.
|
|
||||||
def wrap_line(line,color=None,i_indent="",s_indent=""):
|
|
||||||
wrapped = wraplines(line,width,initial_indent=i_indent,\
|
|
||||||
subsequent_indent=s_indent)
|
|
||||||
final = ""
|
|
||||||
for l in wrapped:
|
|
||||||
if color:
|
|
||||||
spaces = ""
|
|
||||||
while l.startswith(" "):
|
|
||||||
l = l[1:]
|
|
||||||
spaces += " "
|
|
||||||
l = spaces + color + l + "\x1b[0m"
|
|
||||||
if l.strip() != "":
|
|
||||||
final += l + "\n"
|
|
||||||
return final
|
|
||||||
def format_link(url,index,name=None):
|
def format_link(url,index,name=None):
|
||||||
if "://" in url:
|
if "://" in url:
|
||||||
protocol,adress = url.split("://",maxsplit=1)
|
protocol,adress = url.split("://",maxsplit=1)
|
||||||
|
@ -716,20 +704,14 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
line = "[%d%s] %s" % (index, protocol, name)
|
line = "[%d%s] %s" % (index, protocol, name)
|
||||||
return line
|
return line
|
||||||
for line in gemtext.splitlines():
|
for line in gemtext.splitlines():
|
||||||
|
r.newline()
|
||||||
if line.startswith("```"):
|
if line.startswith("```"):
|
||||||
preformatted = not preformatted
|
preformatted = not preformatted
|
||||||
elif preformatted:
|
elif preformatted:
|
||||||
# infinite line to not wrap preformated
|
# infinite line to not wrap preformated
|
||||||
r.add_block(line)
|
r.add_block(line+"\n")
|
||||||
l = wraplines(line,100000000)
|
|
||||||
if len(l) > 0:
|
|
||||||
l = l[0]
|
|
||||||
else:
|
|
||||||
l = ""
|
|
||||||
rendered_text += l + "\n"
|
|
||||||
elif len(line.strip()) == 0:
|
elif len(line.strip()) == 0:
|
||||||
r.newparagraph()
|
r.newparagraph(force=True)
|
||||||
rendered_text += "\n"
|
|
||||||
elif line.startswith("=>"):
|
elif line.startswith("=>"):
|
||||||
strippedline = line[2:].strip()
|
strippedline = line[2:].strip()
|
||||||
if strippedline:
|
if strippedline:
|
||||||
|
@ -748,43 +730,30 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
r.add_text(link)
|
r.add_text(link)
|
||||||
r.endindent()
|
r.endindent()
|
||||||
#r.close_all()
|
#r.close_all()
|
||||||
wrapped = wrap_line(link,s_indent=startpos*" ")
|
|
||||||
rendered_text += wrapped
|
|
||||||
elif line.startswith("* "):
|
elif line.startswith("* "):
|
||||||
line = line[1:].lstrip("\t ")
|
line = line[1:].lstrip("\t ")
|
||||||
r.startindent("• ",sub=" ")
|
r.startindent("• ",sub=" ")
|
||||||
r.add_text(line)
|
r.add_text(line)
|
||||||
r.endindent()
|
r.endindent()
|
||||||
rendered_text += wrapparagraph(line, width, initial_indent = "• ",
|
|
||||||
subsequent_indent=" ") + "\n"
|
|
||||||
elif line.startswith(">"):
|
elif line.startswith(">"):
|
||||||
line = line[1:].lstrip("\t ")
|
line = line[1:].lstrip("\t ")
|
||||||
r.startindent("> ")
|
r.startindent("> ")
|
||||||
r.add_text(line)
|
r.add_text(line)
|
||||||
r.endindent()
|
r.endindent()
|
||||||
rendered_text += wrapparagraph(line,width, initial_indent = "> ",
|
|
||||||
subsequent_indent="> ") + "\n"
|
|
||||||
elif line.startswith("###"):
|
elif line.startswith("###"):
|
||||||
line = line[3:].lstrip("\t ")
|
line = line[3:].lstrip("\t ")
|
||||||
r.newparagraph()
|
|
||||||
r.open_color("blue")
|
r.open_color("blue")
|
||||||
r.open_color("faint")
|
|
||||||
r.add_text(line)
|
r.add_text(line)
|
||||||
r.close_color("blue")
|
r.close_color("blue")
|
||||||
r.close_color("faint")
|
|
||||||
rendered_text += wrap_line(line, color="\x1b[34m\x1b[2m")
|
|
||||||
elif line.startswith("##"):
|
elif line.startswith("##"):
|
||||||
line = line[2:].lstrip("\t ")
|
line = line[2:].lstrip("\t ")
|
||||||
r.newparagraph()
|
|
||||||
r.open_color("blue")
|
r.open_color("blue")
|
||||||
r.add_text(line)
|
r.add_text(line)
|
||||||
r.close_color("blue")
|
r.close_color("blue")
|
||||||
rendered_text += wrap_line(line, color="\x1b[34m")
|
|
||||||
elif line.startswith("#"):
|
elif line.startswith("#"):
|
||||||
line = line[1:].lstrip("\t ")
|
line = line[1:].lstrip("\t ")
|
||||||
if not self.title:
|
if not self.title:
|
||||||
self.title = line
|
self.title = line
|
||||||
r.newparagraph()
|
|
||||||
r.open_color("bold")
|
r.open_color("bold")
|
||||||
r.open_color("blue")
|
r.open_color("blue")
|
||||||
r.open_color("underline")
|
r.open_color("underline")
|
||||||
|
@ -792,16 +761,12 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
r.close_color("underline")
|
r.close_color("underline")
|
||||||
r.close_color("bold")
|
r.close_color("bold")
|
||||||
r.close_color("blue")
|
r.close_color("blue")
|
||||||
rendered_text += wrap_line(line,color="\x1b[1;34m\x1b[4m")
|
|
||||||
else:
|
else:
|
||||||
rendered_text += wrap_line(line).rstrip() + "\n"
|
|
||||||
# with the add_block, we keep leading spaces and handmade formatting.
|
# with the add_block, we keep leading spaces and handmade formatting.
|
||||||
#r.add_block(line.rstrip(),wrap=True)
|
#r.add_block(line.rstrip(),wrap=True)
|
||||||
# while with add_text, we justify on the left margin
|
# while with add_text, we justify on the left margin
|
||||||
r.add_text(line.rstrip())
|
r.add_text(line.rstrip())
|
||||||
if BETA:
|
return r.get_final(), links
|
||||||
rendered_text = r.get_final()
|
|
||||||
return rendered_text, links
|
|
||||||
|
|
||||||
class GopherRenderer(AbstractRenderer):
|
class GopherRenderer(AbstractRenderer):
|
||||||
def get_mime(self):
|
def get_mime(self):
|
||||||
|
|
Loading…
Reference in New Issue