def __init__(self, article, preview=False, *args, **kwargs):
kwargs = settings.MARKDOWN_KWARGS
kwargs['extensions'] = self.get_markdown_extensions()
markdown.Markdown.__init__(self, *args, **kwargs)
self.article = article
self.preview = preview
python类markdown()的实例源码
def setUp(self):
self.md = markdown.Markdown(extensions=[
'extra',
ResponsiveTableExtension()
])
self.md_without = markdown.Markdown(extensions=['extra'])
def load_posts(self, fpath=None):
"""\
- Load valid posts from source directory
- Convert the text to extract the HTML data, metas and filenames
- Returns a list of tuples that store the above information
"""
if fpath == None:
fpath = self.ifpath
if path.exists(fpath):
posts = []
for f in [fpath] if path.isfile(fpath) \
else list(map(lambda x: path.join(fpath, x),
listdir(fpath))):
if path.splitext(f)[1] in self._md_exts:
with codecs.open(f, 'r', encoding='utf-8') as fh:
md = Markdown(extensions=self.pymd_exts)
html = BeautifulSoup(md.convert(fh.read()), 'lxml')
if html.html:
# Remove html and body tags
html.html.hidden = True
html.body.hidden = True
meta = md.Meta
if not meta.get('type'):
meta['type'] = [self.default_post_type]
posts.append((html, meta, f))
if not posts:
print('warning: Nothing to publish')
return posts
else:
raise OSError(errno.ENOENT, strerror(errno.ENOENT), fpath)
def run(self, lines):
HEADER_REGEX = re.compile('\{\{inline\(list\)::\n(.*?)\n\}\}', re.DOTALL) # maybe too much sensitive
def replace(m):
text = m.groups()[0]
md = Markdown()
table = md.convert(text).replace("\n", "")
return table
new_lines = HEADER_REGEX.sub(replace, "\n".join(lines))
return new_lines.split("\n")
def post(self):
raw_text = self.request.body
unicode_raw_text = unicode(raw_text, "utf-8")
md = markdown.Markdown(extensions=MARKDOWN_EXT)
html_text = md.reset().convert(unicode_raw_text)
# ??????
def convert_checkbox1(match):
return '<li><input type="checkbox" disabled>' if match.group('checked') == ' ' \
else '<li><input type="checkbox" disabled checked>'
def convert_checkbox2(match):
return '<li>\n<p><input type="checkbox" disabled>' if match.group('checked') == ' ' \
else '<li>\n<p><input type="checkbox" disabled checked>'
# ??img out link
def convert_src(match):
return 'src="' + match.group('src') + '"'
def filter_xss(match):
return ' '
pattern_actions = {xss_pattern1: filter_xss,
xss_pattern2: filter_xss,
checked_pattern1: convert_checkbox1,
checked_pattern2: convert_checkbox2,
src_pattern: convert_src}
for pattern, action in pattern_actions.items():
html_text = re.sub(pattern, action, html_text)
self.write(html_text)
def pinyin_markdown(request):
return Markdown(extensions=[request.param])
def pinyin_md_no_classes():
return Markdown(extensions=['pinyin_markdown(tone_class=,erhua_class=,apostrophe_class=)'])
def pinyin_md_entities():
return Markdown(extensions=['pinyin_markdown(entities=True)'])
def __init__(self):
self._extensions = [
'codehilite(css_class=highlight)',
'markdown.extensions.tables'
]
self._parser = Markdown(
extensions=self._extensions
)
def convert_to_html(markdown_text):
md = markdown.Markdown(
extensions=[
'pymdownx.github',
'markdown.extensions.toc',
],
extension_configs={
'markdown.extensions.toc':
{
'title': '??',
},
},
output_format="html5"
)
return md.convert(markdown_text)