Contribution for Mistune.
lepture / mistune-contrib Goto Github PK
View Code? Open in Web Editor NEWContribution for mistune.
License: BSD 3-Clause "New" or "Revised" License
Contribution for mistune.
License: BSD 3-Clause "New" or "Revised" License
Contribution for Mistune.
You can reproduce this by simply trying to render and empty string with the ToC enabled.
Traceback (most recent call last):
[... django stuff ...]
File "/home/alex/projects/MfD-wiki/core/models.py", line 45, in save
self.renderedToC = toc.render_toc(level=3)
File "/home/alex/.local/lib/python3.6/site-packages/mistune_contrib/toc.py", line 45, in render_toc
return ''.join(self._iter_toc(level))
File "/home/alex/.local/lib/python3.6/site-packages/mistune_contrib/toc.py", line 80, in _iter_toc
while last_level > first_level:
TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'
In mistune_contrib/math.py, the comments under class MathInlineLexer()
and class MathBlockMixin()
give example codes on how to declare a subclass.
But when I tried to follow the instructions, I met this error,
AttributeError: 'MathInlineLexer' object has no attribute 'rules'
Maybe line 20 and 21 in mistune_contrib/math.py are reversed?
So do line 50 and 51.
class MathInlineLexer(InlineLexer, MathInlineMixin):
def __init__(self, *args, **kwargs):
self.enable_math() # I think these two lines
super(MathInlineLexer, self).__init__(*args, **kwargs) # should be swapped
class MathBlockLexer(MathBlockMixin, BlockLexer):
def __init__(self, *args, **kwargs):
self.enable_math() # So do these
super(MathBlockLexer, self).__init__(*args, **kwargs) # two lines
This project is really handy but would it be possible to get an updated release on pypi please?
The released version is from 2014 so there are bugs fixed here that are still in the release.
Thanks!
@perexg Looks like MdRenderer
is not exported?
$ virtualenv --python=python3.6 venv -q
$ source venv/bin/activate
$ pip install mistune-contrib mistune -q
$ venv/bin/python -c "from mistune_contrib.toc import TocMixin"
$ venv/bin/python -c "from mistune_contrib.mdrenderer import MdRenderer"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mistune_contrib.mdrenderer'
$ # why is this happening? because it's not in the venv:
$ ls venv/lib/python3.6/site-packages/mistune_contrib/
highlight.py __init__.py math.py meta.py __pycache__/ toc.py
Hi,
I find the documentation in the docstrings overly complicated and I can't seem to understand what's going on. I want to use the math/mathjax syntax in my markdown. Could you provide some example on how to achieve that ?
I already have a custom renderer for syntax highlighting, and I'd like to keep it that way :
class HighlightRenderer(mistune.Renderer):
def block_code(self, code, lang=None):
if not lang:
lang = 'text'
try:
lexer = get_lexer_by_name(lang, stripall=True)
except:
lexer = get_lexer_by_name('text', stripall=True)
formatter = HtmlFormatter()
return "{open_block}{formatted}{close_block}".format(
open_block="<div class='code-highlight'>" if lang != 'text' else '',
formatted=highlight(code, lexer, formatter),
close_block="</div>" if lang != 'text' else ''
)
def table(self, header, body):
return "<table class='table table-bordered table-hover'>" + header + body + "</table>"
Thanks a lot.
I could not find a LICENSE file in this repo.
Perhaps it is meant to have the same license as mistune
proper (?).
Do You consider adding python3 typeshed for your library?
Hey,
I have been trying to add a 'emoji' grammar to mistune but unfortunately I have been not so successful and have already spent more time on it than I'd like to admit. I am not that experienced with regex'es so there might be something wrong with mine but I just can't figure out what. The problem is, that mistune will only match the first pattern (like, literally the first in a block) and every emoji pattern after the first match is handled as a normal text. This is the code I've come up with:
import re
import mistune
class EmojiRenderer(object):
def emoji(self, text):
return "<emoji>%s</emoji>" % text
class EmojiInlineLexer(mistune.InlineLexer):
def __init__(self, **kwargs):
super(EmojiInlineLexer, self).__init__(**kwargs)
self.default_rules.insert(0, "emoji")
self.rules.emoji = re.compile(r'^:([a-zA-Z0-9\+\-_]+):', re.I)
def output_emoji(self, m):
text = self.output(m.group(1))
return self.renderer.emoji(text)
class MarkdownRenderer(mistune.Renderer, EmojiRenderer):
def __init__(self, **kwargs):
super(MarkdownRenderer, self).__init__(**kwargs)
renderer = MarkdownRenderer()
inline = EmojiInlineLexer(renderer=renderer)
markdown = mistune.Markdown(renderer=renderer, inline=inline)
demo_text = """
:thumbs_up: *this works*
this doesn't work :thumbs_down:
:smile: :cry:
"""
print(markdown(demo_text))
and this is the output:
<p><emoji>thumbs_up</emoji> <em>this works</em></p>
<p>this doesn't work :thumbs_down:</p>
<p><emoji>smile</emoji> :cry:</p>
I would really appreciate any input/help.
Are there any plans to package those? It is kind of hard to setup dependencies for HTML TOC extension,
Instead of
import mistune
from mistune_contrib.toc import TocMixin
class TocRenderer(TocMixin, mistune.Renderer):
pass
toc = TocRenderer()
markdown = mistune.Markdown(renderer=toc)
toc = toc.reset_toc()
import mistune
from mistune_contrib.toc import TocRenderer
markdown = mistune.Markdown(renderer=TocRenderer)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.