コード例 #1
0
    def read(self, source_path):
        """Parse content and metadata of markdown files"""

        self._source_path = source_path
        self._md = Markdown(extensions=[
            'markdown.extensions.toc',
            'markdown.extensions.tables',
            'markdown.extensions.meta',
            'markdown.extensions.extra',
            codehilite.CodeHiliteExtension(css_class='highlight'),
        ], )
        img_path = os.path.dirname(source_path) + '/img/' \
         + os.path.basename(source_path)[:-3]
        lst_path = os.path.dirname(source_path) + '/lst/' \
         + os.path.basename(source_path)[:-3]
        tbl_path = os.path.dirname(source_path) + '/tbl/' \
         + os.path.basename(source_path)[:-3]
        build.path = [img_path, lst_path, tbl_path] + build.path
        with open(source_path) as fd:
            text = fd.read()
            if hasattr(text, 'decode'):  # Python 2
                text = text.decode('utf-8')
            for m in re.finditer('```python(?P<code>.*?)```', text, re.DOTALL):
                new_block = (u'\n%--\npython: |\n' + u'\n'.join(
                    [u' ' + s for s in m.group('code').strip().split(u'\n')]) +
                             u'\n--%\n')
                text = text.replace(m.group(0), new_block)
            text = build.MD(text)
            # Process internal links
            for m in re.finditer('%link:(?P<link>[\w/-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('link', link, full)
                if link not in links:
                    raise Exception(u'%s not a key in %s' % (link, links))
                text = text.replace(full, '<%s/%s>' % (SITEURL, links[link]))
            for m in re.finditer('%url:(?P<link>[\w/-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('url', link, full)
                if link not in links:
                    raise Exception(u'%s not a key in %s' % (link, links))
                text = text.replace(full, '%s/%s' % (SITEURL, links[link]))
            for m in re.finditer('%static:(?P<link>[\w/.-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('static', link, full)
                text = text.replace(full, '<%s/%s>' % (SITEURL, link))
            text = text.replace(root, u'')
            text = HTMLFilter.DOI(text)
            content = self._md.convert(text)
            for var, val in const.items():
                content = content.replace(u'$%s$' % var, str(val))
            for item_type in ITEM_TYPES:
                content = content.replace(
                    item_type,
                    u'<span class="item-type">%s</span>' % item_type.lower())
        metadata = self._parse_metadata(self._md.Meta)
        build.path = build.path[3:]
        return content, metadata
コード例 #2
0
    def read(self, source_path):
        """Parse content and metadata of markdown files"""

        self._source_path = source_path
        self._md = Markdown(extensions=[
            'markdown.extensions.toc',
            'markdown.extensions.tables',
            'markdown.extensions.meta',
            'markdown.extensions.extra',
            codehilite.CodeHiliteExtension(css_class='highlight'),
        ], )
        # self._md = Markdown(
        # 	extensions=self.extensions + [TocExtension(title='Overview'),
        # 		TableExtension()],
        # 	extpeension_configs=self.extensions)
        img_path = os.path.dirname(source_path) + '/img/' \
         + os.path.basename(source_path)[:-3]
        lst_path = os.path.dirname(source_path) + '/lst/' \
         + os.path.basename(source_path)[:-3]
        tbl_path = os.path.dirname(source_path) + '/tbl/' \
         + os.path.basename(source_path)[:-3]
        build.path = [img_path, lst_path, tbl_path] + build.path
        with open(source_path) as fd:
            text = fd.read()
            text = build.MD(text)
            # Process internal links
            for m in re.finditer('%link:(?P<link>[\w/-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('link', link, full)
                if link not in links:
                    raise Exception(u'%s not a key in %s' % (link, links))
                text = text.replace(full, '<%s/%s>' % (SITEURL, links[link]))
            for m in re.finditer('%url:(?P<link>[\w/-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('url', link, full)
                if link not in links:
                    raise Exception(u'%s not a key in %s' % (link, links))
                text = text.replace(full, '%s/%s' % (SITEURL, links[link]))
            for m in re.finditer('%static:(?P<link>[\w/.-]+)%', text):
                full = m.group(0)
                link = m.group('link')
                print('static', link, full)
                text = text.replace(full, '<%s/%s>' % (SITEURL, link))
            text = text.replace(root, u'')
            text = HTMLFilter.DOI(text)
            content = self._md.convert(text)
            for var, val in const.items():
                content = content.replace(u'$%s$' % var, str(val))
            for item_type in ITEM_TYPES:
                content = content.replace(
                    item_type,
                    u'<span class="item-type">%s</span>' % item_type.lower())
        metadata = self._parse_metadata(self._md.Meta)
        build.path = build.path[3:]
        return content, metadata
コード例 #3
0
ファイル: admin.py プロジェクト: richardbann/gdemo
 def save_model(self, request, obj, form, change):
     obj.owner = request.user
     obj.html = markdown.markdown(obj.source,
                                  output_format="html5",
                                  extensions=[
                                      EscapeHtml(),
                                      codehilite.CodeHiliteExtension(),
                                      fenced_code.FencedCodeExtension(),
                                  ])
     super().save_model(request, obj, form, change)
コード例 #4
0
def build_html_output(content, node):
    return markdown.markdown(
        content,
        extensions=[
            wikilinks.WikiLinkExtension(
                configs=[('base_url', ''), (
                    'end_url',
                    ''), ('build_url',
                          functools.partial(build_wiki_url, node))]),
            fenced_code.FencedCodeExtension(),
            codehilite.CodeHiliteExtension([('css_class', 'highlight')])
        ])
コード例 #5
0
 def get(self, request, alias_name):
     tpl_name = 'articles/detail.html'
     try:
         article = Article.objects.get(alias_name=alias_name)
         article.content = markdown(article.content,
                                    extensions=[
                                        extra.ExtraExtension(),
                                        codehilite.CodeHiliteExtension()
                                    ])
     except (DoesNotExist, MultipleObjectsReturned):
         raise Http404
     return render(request, tpl_name, {'article': article})
コード例 #6
0
def render_content(content, node):
    html_output = markdown.markdown(
        content,
        extensions=[
            wikilinks.WikiLinkExtension(
                configs=[('base_url', ''), (
                    'end_url',
                    ''), ('build_url',
                          functools.partial(build_wiki_url, node))]),
            fenced_code.FencedCodeExtension(),
            codehilite.CodeHiliteExtension([('css_class', 'highlight')])
        ])

    # linkify gets called after santize, because we're adding rel="nofollow"
    #   to <a> elements - but don't want to allow them for other elements.
    sanitized_content = sanitize(html_output, **settings.WIKI_WHITELIST)
    return sanitized_content
コード例 #7
0
ファイル: lmv.py プロジェクト: HugoKlepsch/liveMarkdownViewer
 def render_to_html_string(self):
     """
     Render the watched document to html, returning the html string
     :return: The rendered HTML of the markdown file
     """
     self.logger.debug('Rendering markdown (%s)', self.filename)
     fake_file = BytesIO()
     markdown.markdownFromFile(input=self.filename,
                               output=fake_file,
                               extensions=[abbr.AbbrExtension(), admonition.AdmonitionExtension(),
                                           attr_list.AttrListExtension(), codehilite.CodeHiliteExtension(),
                                           def_list.DefListExtension(), extra.ExtraExtension(),
                                           fenced_code.FencedCodeExtension(), footnotes.FootnoteExtension(),
                                           legacy_attrs.LegacyAttrExtension(), legacy_em.LegacyEmExtension(),
                                           meta.MetaExtension(), nl2br.Nl2BrExtension(),
                                           sane_lists.SaneListExtension(), smarty.SmartyExtension(),
                                           tables.TableExtension(), toc.TocExtension(),
                                           wikilinks.WikiLinkExtension()])
     fake_file.seek(0)
     return str(fake_file.read(), 'utf-8')
コード例 #8
0
def my_slugify(value, sep):
    m = md5.new()
    m.update(value)
    return m.digest().encode('hex')


MY_SLUGIFY_FUNC = my_slugify
MY_HEADING_LIST_STYLE = 'ol'

from markdown.extensions import headerid, codehilite
MD_EXTENSIONS = ([
    'extra',
    'footnotes',
    'tables',
    codehilite.CodeHiliteExtension(configs=[('linenums',
                                             False), ('guess_lang', False)]),
    headerid.HeaderIdExtension(configs=[('slugify', my_slugify)]),
])

# sitemap plugin config
SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.5,
        'indexes': 0.5,
        'pages': 0.5
    },
    'changefreqs': {
        'articles': 'weekly',
        'indexes': 'daily',
        'pages': 'monthly'
コード例 #9
0
from markdown.inlinepatterns import Pattern
from markdown.preprocessors import Preprocessor
from markdown.util import AtomicString, etree
import base64
import markdown
import mimetypes

MARKDOWN_EXTENSIONS = ['extra',
                       'abbr',
                       'attr_list',
                       'def_list',
                       'fenced_code',
                       'footnotes',
                       'tables',
                       'admonition',
                       codehilite.CodeHiliteExtension(guess_lang=False),
                       'meta',
                       'sane_lists',
                       'smarty',
                       toc.TocExtension(baselevel=1),
                       'wikilinks',
                       'knowledge_repo.converters.html:KnowledgeMetaExtension',
                       'knowledge_repo.converters.html:MathJaxExtension',
                       'knowledge_repo.converters.html:IndentsAsCellOutput',
                       'knowledge_repo.converters.html:InlineSpanStyles']


class InlineSpanStyles(Extension):

    SPAN_PATTERN = r'\[([\s\S]*?)\]\{((?:\ ?.[^\,\}]+?)*?)\}'
コード例 #10
0
ファイル: html.py プロジェクト: ppstacy/knowledge-repo
from markdown.blockprocessors import BlockProcessor
from markdown.preprocessors import Preprocessor
from markdown.util import AtomicString
from markdown.extensions import codehilite, toc

import re
import base64
import mimetypes

from ..converter import KnowledgePostConverter
from ..mapping import SubstitutionMapper

MARKDOWN_EXTENSIONS = [
    'extra', 'abbr', 'attr_list', 'def_list', 'fenced_code', 'footnotes',
    'tables', 'admonition',
    codehilite.CodeHiliteExtension(guess_lang=False), 'meta', 'sane_lists',
    'smarty',
    toc.TocExtension(baselevel=1), 'wikilinks',
    'knowledge_repo.converters.html:KnowledgeMetaExtension',
    'knowledge_repo.converters.html:MathJaxExtension',
    'knowledge_repo.converters.html:IndentsAsCellOutput'
]


class IndentsAsCellOutputPreprocessor(Preprocessor):
    """
    Ensure all indented blocks are followed by a blank line to allow html
    preprocessors to extract html elements (like scripts) properly.
    """
    def run(self, lines):
        in_block = False