| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
|
| 6 |
import markdown |
| 7 |
from mdx_gfm import GithubFlavoredMarkdownExtension |
| 8 |
|
| 9 |
import re |
| 10 |
|
| 11 |
import logging |
| 12 |
logging.basicConfig(format='%(levelname)s: %(message)s' ,stream=sys.stderr, level=logging.INFO) |
| 13 |
LOGGER = logging.getLogger('preprocess') |
| 14 |
|
| 15 |
hosted = False |
| 16 |
|
| 17 |
# sort the file order |
| 18 |
def sort_func(x): |
| 19 |
# place "papers/" at the end (like an appendix) |
| 20 |
try: |
| 21 |
x.index('%sdoc%s' % (os.path.sep, os.path.sep)) |
| 22 |
except ValueError: |
| 23 |
return 'z' |
| 24 |
|
| 25 |
# place readmes at the start of each section |
| 26 |
try: |
| 27 |
rm = x.index('README.md') |
| 28 |
return x[:rm] + '0' |
| 29 |
except ValueError: |
| 30 |
return x |
| 31 |
|
| 32 |
# make the links work in-pdf |
| 33 |
def fix_links(match): |
| 34 |
# images |
| 35 |
if os.path.splitext(match.group(2))[-1] == '.png': |
| 36 |
return '[%s](%s)' % ( |
| 37 |
match.group(1), |
| 38 |
match.group(2) |
| 39 |
) |
| 40 |
|
| 41 |
# preserve external urls |
| 42 |
elif match.group(2).startswith('http'): |
| 43 |
return match.group(0) |
| 44 |
|
| 45 |
elif match.group(2).endswith('README.md'): |
| 46 |
return '[%s](%s)' % ( |
| 47 |
match.group(1), |
| 48 |
match.group(2).replace('README.md','index.html') |
| 49 |
) |
| 50 |
|
| 51 |
# fix paths |
| 52 |
return '[%s](%s)' % ( |
| 53 |
match.group(1), |
| 54 |
match.group(2).replace('.md','.html') |
| 55 |
) |
| 56 |
|
| 57 |
def fix_headings(match): |
| 58 |
return '%s#' % ( |
| 59 |
match.group(1) |
| 60 |
) |
| 61 |
|
| 62 |
if __name__ == "__main__": |
| 63 |
# check whether hosted mode is on |
| 64 |
if len(sys.argv)>1: |
| 65 |
if sys.argv[1]=='hosted': |
| 66 |
hosted=True |
| 67 |
|
| 68 |
#-- first, prepare the file list --# |
| 69 |
file_list = [] |
| 70 |
for i in os.walk('../../doc'): |
| 71 |
base_dir, subfolders, files = i |
| 72 |
for file_ in filter(lambda x: x.lower().endswith('.md'), files): |
| 73 |
file_list.append(os.path.join(base_dir, file_)) |
| 74 |
|
| 75 |
#-- then, create the document --# |
| 76 |
html = '' |
| 77 |
|
| 78 |
# perform sort |
| 79 |
file_list.sort(key=sort_func) |
| 80 |
|
| 81 |
first = True |
| 82 |
|
| 83 |
for my_file in file_list: |
| 84 |
with open(my_file, 'r') as md: |
| 85 |
LOGGER.info("processing file %s" % my_file) |
| 86 |
data = md.read() |
| 87 |
|
| 88 |
# retrieve path |
| 89 |
pagePath = 'htmldoc' + os.path.sep + my_file[10:] |
| 90 |
pagePathH = re.sub(r'\.md$','.html',pagePath).replace("README.html","index.html") |
| 91 |
docDir = pagePath[:pagePath.rfind(os.path.sep)] |
| 92 |
LOGGER.info("path: %s" % pagePathH) |
| 93 |
|
| 94 |
if not os.path.exists(docDir): |
| 95 |
os.makedirs(docDir) |
| 96 |
|
| 97 |
# retrieve title |
| 98 |
pageTitle = data.partition('\n')[0].replace("# ","") |
| 99 |
|
| 100 |
# perform link fixing |
| 101 |
data = re.sub(r'\[(.+?)\]\((.+?)\)', fix_links, data) |
| 102 |
data = re.sub(r'^\s*(#+)', fix_headings, data, flags=re.MULTILINE) |
| 103 |
|
| 104 |
# convert |
| 105 |
html = ''' |
| 106 |
<!DOCTYPE html> |
| 107 |
<html> |
| 108 |
<head> |
| 109 |
<meta charset="utf-8"/> |
| 110 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| 111 |
<style> |
| 112 |
body { |
| 113 |
background-color: #222; |
| 114 |
color: #eee; |
| 115 |
font-family: sans-serif; |
| 116 |
} |
| 117 |
|
| 118 |
a { |
| 119 |
color: #3df; |
| 120 |
} |
| 121 |
|
| 122 |
a:visited { |
| 123 |
color: #fd3; |
| 124 |
} |
| 125 |
|
| 126 |
b { |
| 127 |
color: #fff; |
| 128 |
} |
| 129 |
|
| 130 |
h1 { |
| 131 |
text-align: center; |
| 132 |
} |
| 133 |
|
| 134 |
img { |
| 135 |
max-width: 100%%; |
| 136 |
} |
| 137 |
</style> |
| 138 |
<title>%s</title> |
| 139 |
</head> |
| 140 |
<body> |
| 141 |
%s |
| 142 |
</body> |
| 143 |
</html> |
| 144 |
''' % ( |
| 145 |
pageTitle, |
| 146 |
markdown.markdown(data, extensions=['nl2br', 'mdx_breakless_lists', GithubFlavoredMarkdownExtension()]) |
| 147 |
) |
| 148 |
|
| 149 |
with open(pagePathH, 'w') as ht: |
| 150 |
ht.write(html) |