-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.py
48 lines (40 loc) · 1.31 KB
/
content.py
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
import os
import pypandoc
import re
import yaml
class Content:
def __init__(self):
self.html = ''
self.metadata = {}
def parse(self, file_path):
self.html = pypandoc.convert_file(
source_file=file_path, to='html',
extra_args=['--mathjax', '--no-highlight', '--base-header-level=2']
)
meta_path = file_path.replace('.rst', '.meta')
with open(meta_path, 'r') as f:
self.metadata = yaml.load(f, Loader=yaml.FullLoader)
self.fix_pandoc_code_blocks()
self.add_custom_directives()
def fix_pandoc_code_blocks(self):
# https://github.com/jgm/pandoc/issues/3858
self.html = re.sub(
"<pre class=\"sourceCode ([0-9a-zA-Z]+)\"><code>",
"<pre><code class=\"\\1\">",
self.html
)
def add_custom_directives(self):
directives = [
# [[secret="title"]]
{
"old": "<p>\[\[secret="([0-9a-zA-Z_.]+)"\]\]</p>",
"new": "<details>\n<summary>\\1</summary>"
},
# [[/secret]]
{
"old": "<p>\[\[/secret\]\]</p>",
"new": "</details>"
}
]
for d in directives:
self.html = re.sub(d["old"], d["new"], self.html)