#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2016 Chintalagiri Shashank
#
# This file is part of tendril.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Docstring for markdown
"""
import re
from mistune import escape
from mistune import Renderer
from mistune import Markdown
regex_email = re.compile(ur'^<([\w]+\.*[\w]*)+@(([\w]+\.[\w]*)+)>$')
[docs]class TendrilMistuneRenderer(Renderer):
[docs] def placeholder(self):
return MarkdownTreeContainer()
[docs] def block_code(self, code, lang=None):
raise NotImplementedError
[docs] def block_quote(self, text):
raise NotImplementedError
[docs] def block_html(self, html):
raise NotImplementedError
[docs] def hrule(self):
raise NotImplementedError
[docs] def list(self, body, ordered=True):
ltype = 'unordered_list'
if ordered:
ltype = 'ordered_list'
return ltype, body
[docs] def list_item(self, text):
return 'list_item', text
[docs] def paragraph(self, text):
return 'paragraph', text
[docs] def table(self, header, body):
raise NotImplementedError
[docs] def table_row(self, content):
raise NotImplementedError
[docs] def table_cell(self, content, **flags):
raise NotImplementedError
[docs] def double_emphasis(self, text):
raise NotImplementedError
[docs] def emphasis(self, text):
raise NotImplementedError
[docs] def codespan(self, text):
raise NotImplementedError
[docs] def linebreak(self):
raise NotImplementedError
[docs] def strikethrough(self, text):
raise NotImplementedError
[docs] def text(self, text):
return 'text', escape(text)
[docs] def autolink(self, link, is_email=False):
text = link = escape(link)
if is_email:
link = 'mailto:%s' % link
ltype = 'email'
else:
ltype = 'link'
return ltype, link, text
[docs] def link(self, link, title, text):
raise NotImplementedError
[docs] def image(self, src, title, text):
raise NotImplementedError
[docs] def inline_html(self, html):
# TODO we're only doing the bare minimum here to handle
# mislabelled email addresses.
m = regex_email.match(html)
if m:
email = m.groups()[0] + '@' + m.groups()[1]
return 'email', 'mailto:' + email, email
[docs] def newline(self):
raise NotImplementedError
[docs]class MarkdownTreeContainer(object):
def __init__(self):
self._pieces = []
# print "Creating {}".format(self)
def __iadd__(self, other):
# print "Adding to {} : {}".format(self, other)
self._pieces.append(other)
return self
[docs] def collapse_to_text(self):
out = ''
for piece in self._pieces:
if isinstance(piece, MarkdownTreeContainer):
out += piece.collapse_to_text()
elif piece[0] == 'text':
out += piece[1]
return out
[docs] def collapse_to_markup(self):
raise NotImplementedError
[docs]def parse_markdown(fpath):
with open(fpath, 'r') as f:
text = f.read()
md = Markdown(renderer=TendrilMistuneRenderer())
return md(text)