forked from ruslo/hunter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert_wiki.py
129 lines (101 loc) · 4.16 KB
/
convert_wiki.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import subprocess
# hardcoded paths
HUNTER_DIR='..'
WIKI_DIR=os.path.join(HUNTER_DIR, '..', 'hunter.wiki')
PACKAGES_DIR=os.path.join(HUNTER_DIR, 'cmake/projects')
# get all wiki entries
wiki_filenames = [x for x in os.listdir(WIKI_DIR) if x.startswith('pkg.') and x.endswith('.md')]
wiki_entries = [x[4:-3] for x in wiki_filenames]
# get all hunter package entries
pkg_entries = [x for x in os.listdir(PACKAGES_DIR) if os.path.isdir(os.path.join(PACKAGES_DIR, x))]
pkg_entries_lower = [x.lower() for x in pkg_entries]
# packages both in hunter and wiki
pkg_match = [x for x in pkg_entries if x.lower() in wiki_entries]
# packages only in hunter
pkg_only_hunter = [x for x in pkg_entries if x not in pkg_match]
# packages only in wiki
pkg_only_wiki = [x for x in wiki_entries if x not in pkg_entries_lower]
# output directories
packages_dir = 'packages'
pkg_dir = 'packages/pkg'
tmp_dir = 'packages/tmp'
only_hunter_dir = 'packages/only_hunter'
only_wiki_dir = 'packages/only_wiki'
# create if not exist
for d in [packages_dir, pkg_dir, tmp_dir, only_hunter_dir, only_wiki_dir]:
if not os.path.exists(d):
os.mkdir(d)
# header for rst files
header_format_string = """.. spelling::
{}
.. _pkg.{}:
{}
{}
"""
template_string = """.. warning::
This page is a template and contains no real information.
Please send pull request with real description.
- `__FIXME__ Official <https://__FIXME__>`__
- `__FIXME__ Hunterized <https://github.com/hunter-packages/__FIXME__>`__
- `__FIXME__ Example <https://github.com/ruslo/hunter/blob/master/examples/__FIXME__/CMakeLists.txt>`__
- Available since `__FIXME__ vX.Y.Z <https://github.com/ruslo/hunter/releases/tag/vX.Y.Z>`__
- Added by `__FIXME__ <https://github.com/__FIXME__>`__ (`__FIXME__ pr-N <https://github.com/ruslo/hunter/pull/N>`__)
.. code-block:: cmake
hunter_add_package(__FIXME__)
find_package(__FIXME__ CONFIG REQUIRED)
target_link_libraries(foo __FIXME__::__FIXME__)
"""
def append_file(f, input_file):
with open(input_file, 'r') as tmp:
for line in tmp.readlines():
if line.startswith('.. code::'):
if line[9:] == ' yml\n':
line = '.. code-block::' + ' yaml\n'
else:
line = '.. code-block::' + line[9:]
f.write(line)
# convert matching entries
for entry in pkg_match:
source_md = os.path.join(WIKI_DIR, 'pkg.' + entry.lower() + '.md')
tmp_rst = os.path.join(tmp_dir, entry + '.rst')
target_rst = os.path.join(pkg_dir, entry + '.rst')
underscores = "=" * len(entry)
header = header_format_string.format(entry, entry, entry, underscores)
#print(header)
cmd = ['pandoc', source_md, '-o', tmp_rst]
print("cmd: ", cmd)
subprocess.call(cmd)
with open(target_rst, 'w') as f:
f.write(header)
append_file(f, tmp_rst)
# create dummy entries for packages only in hunter
for entry in pkg_only_hunter:
source_md = os.path.join(WIKI_DIR, 'pkg.' + entry.lower() + '.md')
tmp_rst = os.path.join(tmp_dir, entry + '.rst')
target_rst = os.path.join(only_hunter_dir, entry + '.rst')
underscores = "=" * len(entry)
header = header_format_string.format(entry, entry, entry, underscores)
#print(header)
with open(target_rst, 'w') as f:
f.write(header)
f.write(template_string)
# convert wiki entry, put in extra folder
for entry in pkg_only_wiki:
source_md = os.path.join(WIKI_DIR, 'pkg.' + entry + '.md')
tmp_rst = os.path.join(tmp_dir, entry + '.rst')
target_rst = os.path.join(only_wiki_dir, entry + '.rst')
underscores = "=" * len(entry)
header = header_format_string.format(entry, entry, entry, underscores)
#print(header)
cmd = ['pandoc', source_md, '-o', tmp_rst]
print("cmd: ", cmd)
subprocess.call(cmd)
with open(target_rst, 'w') as f:
f.write(header)
f.write("File only available in wiki.\n"
"Please merge with hunter-package entry\n\n")
append_file(f, tmp_rst)
print("pkg_match entries: ", len(pkg_match))
print("pkg_only_hunter entries: ", len(pkg_only_hunter))
print("pkg_only_wiki entries: ", len(pkg_only_wiki))