本站無留言功能,有問題或發現錯誤,歡迎到twitter戳我,謝謝

我經常使用 markdown 格式做紀錄,包括我自己的 local gitit 或是這個 Blog。

除了紀錄之外,有時候需要將 Markdown 格式的文件轉成其他格式的檔案來閱讀。

譬如說寫完了文件,可以轉出簡單的 html 檔案傳給同事,或是公司要求的報告,先用 markdown 寫,轉成 html 之後再印成 pdf 檔。

除了要轉出 html 之外,總希望能有客制 css style,這樣生出來的 html 檔自己看起來比較爽,用起來也比較開心。稍微試了一下 brew 安裝的 markdown 工具,或是 node 上面的一些套件,總覺得有點不順手,或是一個超簡單的需求卻搞到很複雜。

最後決定快速地寫個很簡單的 python script 來處理這個問題,反正也不是第一次寫了,就順便貼在這裡

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
103
104
#!/usr/bin/env python3

"""
The MIT License (MIT)

Copyright (c) 2020 Julian Chu

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os.path
import sys

# pip3 install markdown
import markdown

template = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style>
@media screen {
body {
color: #EEE;
background-color: #111;
line-height: 1.5rem;
font-family: sans-serif;
font-weight: 300;
margin: auto;
max-width: 80rem;
line-height: 1.5rem;
}
h1, h2, h3, h4, h5 {
line-height: 1.6rem;
margin-top: 3rem;
}
li {
line-height: 1.6rem;
}
a {
color: #AAF;
}
}

@media print {
body {
background-color: white;
font-size: 12pt;
}

a:link, a:visited, a {
color: grey;
font-weight: bold;
text-decoration: underline;
word-wrap: break-word;
}
thead {
display: table-header-group;
}
}

</style>
</head>
<body>
%s
</body>
</html>
'''


def as_html(content):
return template % content


if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: %s file" % os.path.basename(sys.argv[0]))
sys.exit()

encoding = 'utf-8'

with open(sys.argv[1], mode="r", encoding=encoding) as file:
file_input = file.read()
file.close()

converted = markdown.markdown(file_input)
sys.stdout.write(as_html(converted))
sys.exit(0)

一看就知道是在幹什麼

  • 讀進指定的 markdown,轉成 html content
  • 把 content 塞進寫好的 template string
  • 成果寫到 stdout
1
2
3
$ pip3 install markdown
# save python file as md.py, add to $PATH
$ md.py INPUT.md > output.html
離線閱讀 Android Doc ← Prev Next → SS 守護者列傳 Guardian Heros 路線圖