-
Notifications
You must be signed in to change notification settings - Fork 5
/
vimdoc2html.py
executable file
·97 lines (84 loc) · 2.92 KB
/
vimdoc2html.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# Copyright (C) 2014 xaizek <[email protected]>
#
# This file is part of vimdoc2html.
#
# vimdoc2html is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# vimdoc2html is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vimdoc2html. If not, see <http://www.gnu.org/licenses/>.
"""\
Converts Vim documentation into HTML.
Output is written to <source file>.html.
As part of the process, tags file is created at the location of the source file.
"""
import argparse
import io
import os
import os.path as path
import subprocess
import vimd2h
TEMPLATE = u'''\
<html>
<head>
<title>{title}</title>
<style>{style}</style>
</head>
<body>
<pre>
{html}
</pre>
</body>
</html>\
'''
template = TEMPLATE
script_dir = path.dirname(path.realpath(__file__))
# parse command-line arguments
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-r', '--raw', dest='raw', action='store_true',
help="Don't wrap output into template")
parser.add_argument('-o', '--output',
help="output HTML file")
parser.add_argument('-t', '--template',
help="template file (overrides builtin template)")
parser.add_argument('vimdoc', nargs=1, help='Vim documentation file')
args = parser.parse_args()
raw_output = args.raw
src_filename = args.vimdoc[0]
src_dir = path.dirname(src_filename) or '.'
# generate tags file
subprocess.call([path.join(script_dir, 'helpztags'), src_dir])
tags_path = path.join(src_dir, 'tags')
css_path = path.join(script_dir, 'vimhelp.css')
html_path = args.output if args.output is not None else '%s.html' % src_filename
# read in all external files
with io.open(tags_path, 'r', encoding='utf-8') as tags_file:
tags = tags_file.read()
with io.open(src_filename, 'r', encoding='utf-8') as doc_file:
contents = doc_file.read()
with io.open(css_path, 'r', encoding='utf-8') as css_file:
style = css_file.read()
if args.template is not None:
with io.open(args.template, 'r', encoding='utf-8') as template_file:
template = template_file.read()
# produce formatted html
html = vimd2h.VimDoc2HTML(tags).to_html(contents)
# output result
with io.open(html_path, 'w', encoding='utf-8') as html_file:
if raw_output:
html_file.write(html)
else:
html_file.write(
template.format(title=path.basename(src_filename),
style=style,
html=html))