-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Pandoc to render markdown, fallback to plaintext
Also used for markdown rendering in console and copy-to-clipboard
- Loading branch information
1 parent
7443d3e
commit 8cb294f
Showing
8 changed files
with
85 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from toot.tui.utils import highlight_hashtags | ||
from toot.utils import html_to_paragraphs | ||
from toot.wcstring import wc_wrap | ||
from typing import List | ||
|
||
try: | ||
# first preference, render markup with pypandoc | ||
from .markdown import html_to_text | ||
|
||
except ImportError: | ||
# Fallback to render in plaintext | ||
def html_to_text(html: str, columns=80, highlight_tags=False) -> List: | ||
output = [] | ||
first = True | ||
for paragraph in html_to_paragraphs(html): | ||
if not first: | ||
output.append("") | ||
for line in paragraph: | ||
for subline in wc_wrap(line, columns): | ||
if highlight_tags: | ||
output.append(highlight_hashtags(subline)) | ||
else: | ||
output.append(subline) | ||
first = False | ||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pypandoc import convert_text | ||
from typing import List | ||
|
||
|
||
def html_to_text(html: str, columns=80, highlight_tags=False) -> List: | ||
return [convert_text( | ||
html, | ||
format="html", | ||
to="gfm-raw_html", | ||
extra_args=["--wrap=auto", f"--columns={columns}"], | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
import urwid | ||
import html2text | ||
|
||
from toot.tui.utils import highlight_hashtags | ||
from toot.utils import format_content | ||
from typing import List | ||
|
||
try: | ||
# our first preference is to render using urwidgets | ||
from .richtext import html_to_widgets, url_to_widget | ||
|
||
except ImportError: | ||
# Fallback if urwidgets are not available | ||
def html_to_widgets(html: str) -> List[urwid.Widget]: | ||
return [ | ||
urwid.Text(_format_markdown(html)) | ||
] | ||
try: | ||
# second preference, render markup with pypandoc | ||
from .markdown import html_to_widgets, url_to_widget | ||
|
||
except ImportError: | ||
# Fallback to render in plaintext | ||
|
||
def url_to_widget(url: str): | ||
return urwid.Text(("link", url)) | ||
def url_to_widget(url: str): | ||
return urwid.Text(("link", url)) | ||
|
||
def _format_markdown(html) -> str: | ||
h2t = html2text.HTML2Text() | ||
h2t.single_line_break = True | ||
h2t.ignore_links = True | ||
h2t.wrap_links = False | ||
h2t.wrap_list_items = False | ||
h2t.wrap_tables = False | ||
h2t.unicode_snob = True | ||
h2t.ul_item_mark = "\N{bullet}" | ||
return h2t.handle(html).strip() | ||
def html_to_widgets(html: str) -> List[urwid.Widget]: | ||
return [ | ||
urwid.Text(highlight_hashtags(line)) for line in format_content(html) | ||
] |
Oops, something went wrong.