-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-to-html.py
46 lines (39 loc) · 1.29 KB
/
markdown-to-html.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
''' markdown-to-html.py -- convert a Markdown file to an HTML page'''
# The HTML page will require 'github-markdown.css' from
# https://github.com/sindresorhus/github-markdown-css
#
# The Markdown file must contain a top-level heading
from pathlib import Path
from sys import argv
from markdown import markdown
from html import escape
import re
markdown_text = Path(argv[1]).read_text()
html_file = Path(argv[2])
html = markdown(markdown_text, extensions=['extra', 'toc'])
title = escape(re.search(r'^# +(.+) *$', markdown_text, re.MULTILINE).group(1))
html_file.write_text('''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="language" content="en">
<title>%s</title>
<!-- See https://github.com/sindresorhus/github-markdown-css -->
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<link rel="stylesheet" href="github-markdown.css">
<style>
body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
</style>
</head>
<body>
<article class="markdown-body">
%s
</article>
</body>
</html>''' % (title, html))