/usr/lib/python3/dist-packages/markups/markdown.py is in python3-markups 0.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | # This file is part of python-markups module
# License: BSD
# Copyright: (C) Dmitry Shachnev, 2012
from __future__ import absolute_import
import os
import sys
import markups.common as common
from markups.abstract import AbstractMarkup
MATHJAX_CONFIG = \
'''<script type="text/x-mathjax-config">
MathJax.Hub.Config({
config: ["MMLorHTML.js"],
jax: ["input/TeX", "output/HTML-CSS", "output/NativeMML"],
extensions: ["MathMenu.js", "MathZoom.js"],
TeX: {
equationNumbers: {autoNumber: "AMS"}
}
});
</script>
'''
class MarkdownMarkup(AbstractMarkup):
"""Markdown language"""
name = 'Markdown'
attributes = {
common.LANGUAGE_HOME_PAGE: 'http://daringfireball.net/projects/markdown/',
common.MODULE_HOME_PAGE: 'https://github.com/Waylan/Python-Markdown/',
common.SYNTAX_DOCUMENTATION: 'http://daringfireball.net/projects/markdown/syntax'
}
file_extensions = ('.md', '.mkd', '.mkdn', '.mdwn', '.mdown', '.markdown')
default_extension = '.mkd'
@staticmethod
def available():
try:
import markdown
except ImportError:
return False
return True
def _load_extensions_list_from_file(self, filename):
try:
extensions_file = open(filename)
except IOError:
return []
else:
extensions = [line.rstrip() for line in extensions_file]
extensions_file.close()
return extensions
def _check_extension_exists(self, extension_name):
try:
__import__('markdown.extensions.'+extension_name, {}, {},
['markdown.extensions'])
except ImportError:
try:
__import__('mdx_'+extension_name)
except ImportError:
return False
return True
def _get_mathjax_patterns(self, markdown):
def handle_match_inline(m):
node = markdown.util.etree.Element('script')
node.set('type', 'math/tex')
node.text = markdown.util.AtomicString(m.group(3))
return node
def handle_match(m):
node = markdown.util.etree.Element('script')
node.set('type', 'math/tex; mode=display')
node.text = markdown.util.AtomicString(m.group(3))
if '\\begin' in m.group(2):
node.text = markdown.util.AtomicString(m.group(2) +
m.group(3) + m.group(4))
return node
inlinemathpatterns = (
markdown.inlinepatterns.Pattern(r'(?<!\\|\$)(\$)([^\$]+)(\$)'),
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\()(.+?)(\\\))')
)
mathpatterns = (
markdown.inlinepatterns.Pattern(r'(?<!\\)(\$\$)([^\$]+)(\$\$)'),
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\[)(.+?)(\\\])'),
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\begin{[a-z]+\*?})(.+)(\\end{[a-z]+\*?})')
)
patterns = []
for pattern in inlinemathpatterns:
pattern.handleMatch = handle_match_inline
patterns.append(pattern)
for pattern in mathpatterns:
pattern.handleMatch = handle_match
patterns.append(pattern)
return patterns
def __init__(self, filename=None, extensions=None):
AbstractMarkup.__init__(self, filename)
import markdown
if extensions is None:
self.extensions = self._load_extensions_list_from_file(
os.path.join(common.CONFIGURATION_DIR, 'markdown-extensions.txt'))
local_directory = os.path.split(filename)[0] if filename else '.'
if not local_directory: local_directory = '.'
self.extensions += self._load_extensions_list_from_file(
os.path.join(local_directory, 'markdown-extensions.txt'))
# Remove duplicate entries
self.extensions = list(set(self.extensions))
else:
self.extensions = extensions
# We have two virtual extensions
if 'remove_extra' in self.extensions:
self.extensions.remove('remove_extra')
else:
self.extensions.append('extra')
self.mathjax = ('mathjax' in self.extensions)
if self.mathjax:
self.extensions.remove('mathjax')
for extension in self.extensions:
if not self._check_extension_exists(extension):
sys.stderr.write('Failed loading extension %s\n' % extension)
self.extensions.remove(extension)
self.md = markdown.Markdown(self.extensions, output_format='html4')
if self.mathjax:
patterns = self._get_mathjax_patterns(markdown)
for i, pattern in enumerate(patterns):
self.md.inlinePatterns.add('mathjax%d' % i, pattern, '<escape')
def get_document_title(self, text):
if 'meta' not in self.extensions:
return ''
if not 'body' in self.cache:
self.get_document_body(text)
if hasattr(self.md, 'Meta') and 'title' in self.md.Meta:
return str.join(' ', self.md.Meta['title'])
else:
return ''
def get_stylesheet(self, text=''):
if 'codehilite' in self.extensions:
return common.get_pygments_stylesheet('.codehilite')
return ''
def get_javascript(self, text='', webenv=False):
if not self.mathjax:
return ''
if 'body' in self.cache:
body = self.cache['body']
else:
body = self.get_document_body(text)
if not '<script type="math/tex' in body:
return ''
return (MATHJAX_CONFIG + '<script type="text/javascript" src="'
+ common.get_mathjax_url(webenv) + '"></script>')
def get_document_body(self, text):
self.md.reset()
converted_text = self.md.convert(text) + '\n'
if self.enable_cache:
self.cache['body'] = converted_text
return converted_text
|