forked from p12tic/cppreference-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
executable file
·128 lines (102 loc) · 3.93 KB
/
Copy pathpreprocess.py
File metadata and controls
executable file
·128 lines (102 loc) · 3.93 KB
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
#!/usr/bin/env python3
# Copyright (C) 2011, 2012 Povilas Kanapickas <povilas@radix.lt>
#
# This file is part of cppreference-doc
#
# This program 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.
#
# This program 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 this program. If not, see http://www.gnu.org/licenses/.
import fnmatch
import re
import os
# copy the source tree
os.system('rm -rf output/reference')
os.system('mkdir -p output/reference')
os.system('cp -rt output/reference reference/*')
# rearrange the archive {root} here is output/reference
# before
# {root}/en.cppreference.com/w/ : html
# {root}/en.cppreference.com/mwiki/ : data
# {root}/en.cppreference.com/ : data
# ... (other languages)
# {root}/upload.cppreference.com/mwiki/ : data
# after
# {root}/common/ : all common data
# {root}/en/ : html for en
# ... (other languages)
data_path = "output/reference/common"
os.system('mkdir ' + data_path)
os.system('mv output/reference/upload.cppreference.com/mwiki/* ' + data_path)
os.system('rm -r output/reference/upload.cppreference.com/')
for lang in ["en"]:
path = "output/reference/" + lang + ".cppreference.com/"
src_html_path = path + "w/"
src_data_path = path + "mwiki/"
html_path = "output/reference/" + lang
if (os.path.isdir(src_html_path)):
os.system('mv ' + src_html_path + ' ' + html_path)
if (os.path.isdir(src_data_path)):
# the skin files should be the same for all languages thus we
# can merge everything
os.system('cp -rl ' + src_data_path + '/* ' + data_path)
os.system('rm -r ' + src_data_path)
# also copy the custom fonts
os.system('cp ' + path + 'DejaVuSansMonoCondensed60.ttf ' +
path + 'DejaVuSansMonoCondensed75.ttf ' + data_path)
# remove what's left
os.system('rm -r '+ path)
# find all html and css files
html_files = []
css_files = []
for root, dirnames, filenames in os.walk('output/reference/'):
for filename in fnmatch.filter(filenames, '*.html'):
html_files.append(os.path.join(root, filename))
for filename in fnmatch.filter(filenames, '*.css'):
css_files.append(os.path.join(root, filename))
#
r1 = re.compile('<!-- Added by HTTrack -->.*?<!-- \/Added by HTTrack -->')
r2 = re.compile('<!-- Mirrored from .*?-->')
#temporary fix
r3 = re.compile('<style[^<]*?<[^<]*?MediaWiki:Geshi\.css[^<]*?<\/style>', re.MULTILINE)
# clean the html files
for fn in html_files:
f = open(fn, "r")
text = f.read()
f.close()
text = r1.sub('', text);
text = r2.sub('', text);
text = r3.sub('', text);
f = open(fn, "w")
f.write(text)
f.close()
tmpfile = fn + '.tmp';
os.system('xsltproc --novalid --html --encoding UTF-8 preprocess.xsl "' + fn + '" > "' + tmpfile + '"')
os.system('mv "' + tmpfile + '" "' + fn + '"')
# append css modifications to the css files
f = open("preprocess-css.css", "r")
css_app = f.read()
f.close()
for fn in css_files:
f = open(fn, "r")
text = f.read()
f.close()
text = text.replace('../DejaVuSansMonoCondensed60.ttf', 'DejaVuSansMonoCondensed60.ttf')
text = text.replace('../DejaVuSansMonoCondensed75.ttf', 'DejaVuSansMonoCondensed75.ttf')
if (re.search('DejaVuSansMonoCondensed60', text)):
# assume this is minified MediaWiki:Common.css
# append the modifications
text += css_app
# QT Help viewer doesn't understand nth-child
text = text.replace('nth-child(1)', 'first-child')
f = open(fn, "w")
f.write(text)
f.close()