Move gemtext parsing to a separate function.

This commit is contained in:
nervuri 2021-12-25 12:47:33 +00:00
parent 265a69a6ed
commit e79438c2e2
1 changed files with 41 additions and 30 deletions

71
av98.py
View File

@ -884,6 +884,40 @@ you'll be able to transparently follow links to Gopherspace!""")
self._debug("Using handler: %s" % cmd_str)
return cmd_str
def _parse_gemtext_line(self, line, menu_gi, preformatted = False):
rendered_line = ''
if line.startswith("```"):
preformatted = not preformatted
elif preformatted:
rendered_line = line + "\n"
elif line.startswith("=>"):
try:
gi = GeminiItem.from_map_line(line, menu_gi)
self.index.append(gi)
rendered_line = self._format_geminiitem(len(self.index), gi) + "\n"
except:
self._debug("Skipping possible link: %s" % line)
elif line.startswith("* "):
rendered_line = line[1:].lstrip("\t ")
rendered_line = textwrap.fill(rendered_line, self.options["width"],
initial_indent = "", subsequent_indent=" ") + "\n"
elif line.startswith(">"):
rendered_line = line[1:].lstrip("\t ")
rendered_line = textwrap.fill(rendered_line, self.options["width"],
initial_indent = "> ", subsequent_indent="> ") + "\n"
elif line.startswith("###"):
rendered_line = line[3:].lstrip("\t ")
rendered_line = "\x1b[4m" + rendered_line + "\x1b[0m""\n"
elif line.startswith("##"):
rendered_line = line[2:].lstrip("\t ")
rendered_line = "\x1b[1m" + rendered_line + "\x1b[0m""\n"
elif line.startswith("#"):
rendered_line = line[1:].lstrip("\t ")
rendered_line = "\x1b[1m\x1b[4m" + rendered_line + "\x1b[0m""\n"
else:
rendered_line = textwrap.fill(line, self.options["width"]) + "\n"
return (rendered_line, preformatted)
def _handle_gemtext(self, body, menu_gi, display=True):
self.index = []
preformatted = False
@ -891,37 +925,14 @@ you'll be able to transparently follow links to Gopherspace!""")
os.unlink(self.idx_filename)
tmpf = tempfile.NamedTemporaryFile("w", encoding="UTF-8", delete=False)
self.idx_filename = tmpf.name
for line in body.splitlines():
if line.startswith("```"):
preformatted = not preformatted
elif preformatted:
tmpf.write(line + "\n")
elif line.startswith("=>"):
try:
gi = GeminiItem.from_map_line(line, menu_gi)
self.index.append(gi)
tmpf.write(self._format_geminiitem(len(self.index), gi) + "\n")
except:
self._debug("Skipping possible link: %s" % line)
elif line.startswith("* "):
line = line[1:].lstrip("\t ")
tmpf.write(textwrap.fill(line, self.options["width"],
initial_indent = "", subsequent_indent=" ") + "\n")
elif line.startswith(">"):
line = line[1:].lstrip("\t ")
tmpf.write(textwrap.fill(line, self.options["width"],
initial_indent = "> ", subsequent_indent="> ") + "\n")
elif line.startswith("###"):
line = line[3:].lstrip("\t ")
tmpf.write("\x1b[4m" + line + "\x1b[0m""\n")
elif line.startswith("##"):
line = line[2:].lstrip("\t ")
tmpf.write("\x1b[1m" + line + "\x1b[0m""\n")
elif line.startswith("#"):
line = line[1:].lstrip("\t ")
tmpf.write("\x1b[1m\x1b[4m" + line + "\x1b[0m""\n")
else:
tmpf.write(textwrap.fill(line, self.options["width"]) + "\n")
# Parse gemtext.
rendered_line, preformatted = self._parse_gemtext_line(
line, menu_gi, preformatted)
# Write to file.
if rendered_line:
tmpf.write(rendered_line)
tmpf.close()
self.lookup = self.index