Skip to content

Commit f54d934

Browse files
author
p12
committed
Devhelp: rewrite the title-location mapping in Python
1 parent 2ca9f95 commit f54d934

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

Makefile

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CLEANFILES= \
4444
qch-help-project.xml \
4545
qch-files.xml \
4646
devhelp-index.xml \
47-
devhelp-files.xml
47+
link-map.xml
4848

4949
clean:
5050
rm -rf $(CLEANFILES)
@@ -84,14 +84,8 @@ cppreference-doc-en.devhelp2: output
8484
xsltproc --stringparam book-base $(docdir)/html \
8585
index2devhelp.xsl index-functions-cpp.xml > devhelp-index.xml
8686

87-
#fix links in the .devhelp2 index
88-
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><files>" > "devhelp-files.xml"
89-
pushd "output" > /dev/null; \
90-
find . -iname "*.html" \
91-
-exec ../fix_devhelp-links.sh '{}' \; ; \
92-
popd > /dev/null
93-
94-
echo "</files>" >> "devhelp-files.xml"
87+
#create mapping between filename and real title
88+
./build_link_map.py
9589

9690
xsltproc fix_devhelp-links.xsl devhelp-index.xml > cppreference-doc-en.devhelp2
9791

build_link_map.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright (C) 2012 p12 <tir5c3@yahoo.co.uk>
4+
#
5+
# This file is part of cppreference-doc
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see http://www.gnu.org/licenses/.
19+
20+
# This file examines all html files in the output directory and writes
21+
# filename -> title mapping to a xml file.
22+
23+
import fnmatch
24+
import re
25+
import os
26+
27+
# find all html files
28+
html_files = []
29+
for root, dirnames, filenames in os.walk('output'):
30+
for filename in fnmatch.filter(filenames, '*.html'):
31+
html_files.append(os.path.join(root, filename))
32+
33+
# create an xml file containing mapping between page title and actual location
34+
out = open('link-map.xml', 'w')
35+
out.write('<?xml version="1.0" encoding="UTF-8"?><files>\n')
36+
37+
for fn in html_files:
38+
f = open(fn, "r")
39+
text = f.read()
40+
f.close()
41+
42+
m = re.search('<script>[^<]*mw\.config\.set([^<]*wgPageName[^<]*)</script>', text)
43+
if not m:
44+
continue
45+
text = m.group(1)
46+
text = re.sub('\s*', '', text)
47+
m = re.search('"wgPageName":"([^"]*)"', text)
48+
if not m:
49+
continue
50+
51+
title = m.group(1)
52+
53+
target = os.path.relpath(os.path.abspath(fn), os.path.abspath('output'))
54+
out.write(' <file from="' + title + '" to="' + target + '" />\n')
55+
56+
out.write('</files>')
57+
out.close()

fix_devhelp-links.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

fix_devhelp-links.xsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Copyright (C) 2012 p12 <tir5c3@yahoo.co.uk>
44
55
This file is part of cppreference-doc
6-
6+
77
This program is free software: you can redistribute it and/or modify
88
it under the terms of the GNU General Public License as published by
99
the Free Software Foundation, either version 3 of the License, or
@@ -26,7 +26,7 @@
2626

2727
<xsl:output indent="yes"/>
2828

29-
<xsl:variable name="mapping" select="document('devhelp-files.xml')/files/*"/>
29+
<xsl:variable name="mapping" select="document('link-map.xml')/files/*"/>
3030

3131
<xsl:template match="node()|@*">
3232
<xsl:copy>

0 commit comments

Comments
 (0)