-
Notifications
You must be signed in to change notification settings - Fork 854
/
md2pdf.py
102 lines (81 loc) · 2.66 KB
/
md2pdf.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
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
# API generator script
#
# Sebastian Raschka 2014-2017
# mlxtend Machine Learning Library Extensions
#
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import yaml
import os
import subprocess
from mlxtend import __version__
yaml_path = "./mkdocs.yml"
source_path = os.path.join(os.path.dirname(yaml_path), 'sources')
md_out_path = "./temp.md"
with open(yaml_path, 'r') as f:
content = f.read()
tree = yaml.load(content)
mkdocs = []
def get_leaf_nodes(tree):
if isinstance(tree, dict):
for v in tree.values():
get_leaf_nodes(v)
elif isinstance(tree, list) or isinstance(tree, tuple):
for ele in tree:
get_leaf_nodes(ele)
else:
mkdocs.append(tree)
get_leaf_nodes(tree['pages'])
mkdocs = [s for s in mkdocs if 'api_subpackages' not in s and
'USER_GUIDE_INDEX.md' not in s]
def abs_imagepath(line, md_path):
elements = line.split('](')
rel_path = elements[1].strip().rstrip(')')
img_path = os.path.join(md_path, rel_path)
img_link = '%s](%s)\n' % (elements[0], img_path)
img_link = img_link.replace('/./', '/')
return img_link
def gen_title(fname):
stem, title = os.path.split(fname)
title = title.rstrip('.md')
s = '# `%s.%s`' % (os.path.split(stem)[1], title)
return s
with open(md_out_path, 'w') as f_out:
meta = r"""---
title: Mlxtend %s
subtitle: Library Documentation
author: Sebastian Raschka
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[LO,LE]{\thepage}
- \fancyfoot[CE,CO]{}
---
""" % __version__
f_out.write(meta)
for md in mkdocs:
md_path = os.path.join(source_path, md)
img_path = os.path.dirname(md_path)
with open(md_path, 'r') as f_in:
content = f_in.readlines()
if md.startswith('user_guide'):
title = gen_title(md)
f_out.write(title + '\n')
if content[0].startswith('# '):
content = content[1:]
for line in content:
if '![png]' in line:
line = line.replace('![png]', '![]')
if '.svg' in line:
continue
if line.startswith('!['):
line = abs_imagepath(line, img_path)
f_out.write(line)
f_out.write('\n\n')
subprocess.check_call(['pandoc', '-N', 'temp.md', '--output=mlxtend.pdf',
'--toc',
'--normalize', '--smart', '--latex-engine=xelatex',
'--toc-depth=4', '--highlight-style=pygments',
'--template=pdftemplate.tex'])
os.remove(md_out_path)