~sircmpwn/sr.ht-dev

core.sr.ht: Replace misaka (hoedown) with mistletoe v4 PROPOSED

ARaspiK: 1
 Replace misaka (hoedown) with mistletoe

 2 files changed, 46 insertions(+), 46 deletions(-)
Maybe having a simple counter (e.g. "this is the 7th checkbox") would
be enough?



          
          
          
        
      

      
      

      

      
      

      

      
      

      
      
        
          

ARaspiK
> Probably not, because you'd still have to parse the markdown to figure
> out where the 7th checkbox is. For example:
> ...
Oops. In that case, there probably isn't a straightforward way to
implement this. Even mistune [0] doesn't support this yet.

[0]: https://github.com/lepture/mistune/issues/218
Alright, no worries for now. Let's just ship v5 once the other issues
are addressed.



          
          
          
        
      

      
      

      

      
      

      

      
    
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/9751/mbox | git am -3
Learn more about email & git

[PATCH core.sr.ht v4] Replace misaka (hoedown) with mistletoe Export this patch

It looked like using hoedown (through misaka), which is very
unmaintained, was leading to a lot of issues (see [0]). This replaces
misaka by mistletoe [1], without losing any functionality (I hope).
Others have tested the patch successfully, so it should work.

[0]: https://todo.sr.ht/~sircmpwn/sr.ht/20
[1]: https://github.com/miyuchina/mistletoe
---
Added `import html` as per Benjamin Lowry. Thanks!

 setup.py         |  2 +-
 srht/markdown.py | 90 ++++++++++++++++++++++++------------------------
 2 files changed, 46 insertions(+), 46 deletions(-)

diff --git a/setup.py b/setup.py
index 4834607..58ddebf 100755
--- a/setup.py
+++ b/setup.py
@@ -32,11 +32,11 @@ setup(
      'sqlalchemy-utils',
      'psycopg2',
      'markdown',
      'mistletoe',
      'bleach',
      'requests',
      'BeautifulSoup4',
      'pgpy',
      'misaka',
      'pygments',
      'cryptography',
  ],
diff --git a/srht/markdown.py b/srht/markdown.py
index cf9677c..e1f5175 100644
--- a/srht/markdown.py
+++ b/srht/markdown.py
@@ -7,52 +7,56 @@ from pygments.formatters import HtmlFormatter, ClassNotFound
from pygments.lexers import get_lexer_by_name
from urllib.parse import urlparse, urlunparse
import bleach
import misaka as m
import html
import mistletoe as m
import re

class RelativeLinkPrefixRenderer(m.HtmlRenderer):
    def __init__(self, *args, link_prefix=None, **kwargs):
        super().__init__(args, **kwargs)
class SrhtRenderer(m.HTMLRenderer):
    def __init__(self, link_prefix=None, baselevel=1):
        super().__init__()
        self.link_prefix = link_prefix
        self.baselevel = baselevel
    
    def render_link(self, token):
        template = '<a href="{target}"{title}>{inner}</a>'
        url = token.target
        if not url.startswith("#"):
            p = urlparse(url)
            if not p.netloc and not p.scheme and self.link_prefix:
                path = join(self.link_prefix, p.path)
                url = urlunparse(('', '', path, p.params, p.query, p.fragment))
        target = self.escape_url(url)
        if token.title:
            title = ' title="{}"'.format(self.escape_html(token.title))
        else:
            title = ''
        inner = self.render_inner(token)
        return template.format(target=target, title=title, inner=inner)

    def link(self, content, url, title=''):
        maybe_title = f' title="{m.escape_html(title)}"' if title else ''
        if url.startswith("#"):
            return f'<a href="{url}"{maybe_title}>{content}</a>'
        p = urlparse(url)
        if not p.netloc and not p.scheme and self.link_prefix:
            path = join(self.link_prefix, p.path)
            url = urlunparse(('', '', path, p.params, p.query, p.fragment))
        return f'<a href="{url}"{maybe_title}>{content}</a>'
    def render_block_code(self, token):
        template = '<pre><code{attr}>{inner}</code></pre>'
        if token.language:
            try:
                lexer = get_lexer_by_name(lang, stripall=True)
            except ClassNotFound:
                lexer = None
            if lexer:
                formatter = HtmlFormatter()
                return highlight(token.children[0].content, lexer, formatter)
            else:
                attr = ' class="{}"'.format('language-{}'.format(self.escape_html(token.language)))
        else:
            attr = ''
        inner = html.escape(token.children[0].content)
        return template.format(attr=attr, inner=inner)

class HighlighterRenderer(m.HtmlRenderer):
    def __init__(self, *args, baselevel=1, **kwargs):
        super().__init__(*args, **kwargs)
        self.baselevel = 1

    def blockcode(self, text, lang):
        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except ClassNotFound:
            lexer = None
        if lexer:
            formatter = HtmlFormatter()
            return highlight(text, lexer, formatter)
        # default
        return '\n<pre><code>{}</code></pre>\n'.format(
                escape(text.strip()))

    def header(self, content, level):
        level += self.baselevel
    def render_heading(self, token):
        template = '<h{level}>{inner}</h{level}>'
        level = token.level + self.baselevel
        if level > 6:
            level = 6
        _id = re.sub(r'[^a-z0-9-_]', '', content.lower().replace(" ", "-"))
        return f'''\n<h{str(level)} id="{_id}">
            {content}
        </h{str(level)}>\n'''

class CustomRenderer(RelativeLinkPrefixRenderer, HighlighterRenderer):
    pass
        inner = self.render_inner(token)
        return template.format(level=level, inner=inner)

urlregex = re.compile(r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')

@@ -107,12 +111,8 @@ def markdown(text, tags=[], baselevel=1, link_prefix=None):
        + ["padding-{}".format(p) for p in ["left", "right", "bottom", "top"]]
        + ["margin-{}".format(p) for p in ["left", "right", "bottom", "top"]],
        strip=True)
    renderer = md = m.Markdown(
        CustomRenderer(baselevel=baselevel, link_prefix=link_prefix),
        extensions=(
            'tables', 'fenced-code', 'footnotes', 'strikethrough', 'highlight',
            'quote', 'autolink'))
    html = renderer(text)
    with SrhtRenderer() as renderer:
        html = renderer.render(m.Document(text))
    html = cleaner.clean(html)
    formatter = HtmlFormatter()
    style = formatter.get_style_defs('.highlight') + " .highlight { background: inherit; }"
-- 
2.25.0
This doesn't seem to work with code blocks, like so:

```c
int main(int argc, char *argv[]) {
	return 0;
}
```

It raises `NameError: name 'lang' is not defined` on line 40 of
render_block_code.

Unrelated: do you reckon it's feasible to implement GitHub-style
checkbox lists based on mistletoe? The main thing we need to do is find
the line/col or index of the [ ]/[x] so that we can emit a checkbox
which encodes the appropriate character in the markdown source to be
updated when the checkbox is ticked or unticked.
View this thread in the archives