This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsource.py
More file actions
52 lines (42 loc) · 1.57 KB
/
source.py
File metadata and controls
52 lines (42 loc) · 1.57 KB
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
from markdown import Markdown
from markdown.extensions.meta import MetaExtension, MetaPreprocessor
from markdown.extensions.tables import TableExtension
from markdown.extensions.toc import TocExtension
from markdown.extensions.wikilinks import WikiLinkExtension
from jinja2 import Markup
from .mdext import CMS7Extension
import logging
logger = logging.getLogger(__name__)
def load_source(path):
return MarkdownSource(path)
class Namespace:
pass
class MarkdownSource:
def __init__(self, source):
self.source = source
self.text = ''
with self.open_source() as f:
try:
self.text = f.read()
except UnicodeDecodeError:
logger.error('Invalid unicode in %s', self.source, exc_info=True)
except:
logger.error('Error loading %s', self.source, exc_info=True)
self.meta = self.read_metadata()
def read_metadata(self):
ns = Namespace()
mp = MetaPreprocessor(ns)
mp.run(self.text.split('\n'))
return ns.Meta
def render(self, gs, *, baselevel=2, hyphenate=False, paragraphs=None):
md = Markdown(extensions=[
MetaExtension(),
TableExtension(),
WikiLinkExtension(),
CMS7Extension(gs, path=self.source, baselevel=baselevel, hyphenate=hyphenate, paragraphs=paragraphs),
TocExtension(anchorlink=True)
],
output_format='html5')
return Markup(md.convert(self.text))
def open_source(self):
return self.source.open('r')