Coder Social home page Coder Social logo

mistune-contrib's Introduction

Contribution for Mistune

Contribution for Mistune.

mistune-contrib's People

Contributors

frostming avatar lepture avatar perexg avatar pylixm avatar takluyver avatar vc12345679 avatar yxy avatar zcchen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mistune-contrib's Issues

ToC crashes when there are no headers/titles

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, are these codes (in comments) in a reverse order ?

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

updated pypi release?

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!

MdRenderer not exported?

@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

Examples or tutorial ?

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.

License for mistune-contrib

I could not find a LICENSE file in this repo.
Perhaps it is meant to have the same license as mistune proper (?).

Emoji Grammar

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.

Upload to PyPI

Are there any plans to package those? It is kind of hard to setup dependencies for HTML TOC extension,

TOC API is overcomplicated

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.