Skip to content

Commit 68c5c31

Browse files
committed
Transform: Extract link map functionality into a reusable class
1 parent 8a9fda4 commit 68c5c31

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

build_link_map.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import lxml.etree as e
2525
import re
2626
import os
27+
from link_map import LinkMap
2728

2829
# returns a dict { title -> filename }.
2930
# directory - either 'output/reference' or 'reference'
@@ -34,7 +35,7 @@ def build_link_map(directory):
3435
for filename in fnmatch.filter(filenames, '*.html'):
3536
html_files.append(os.path.join(root, filename))
3637

37-
link_map = {}
38+
link_map = LinkMap()
3839

3940
for fn in html_files:
4041
f = open(fn, "r")
@@ -53,24 +54,14 @@ def build_link_map(directory):
5354
title = m.group(1)
5455

5556
target = os.path.relpath(os.path.abspath(fn), os.path.abspath(directory))
56-
link_map[title] = target
57+
link_map.add_link(title, target)
5758
return link_map
5859

5960
def main():
6061
link_map = build_link_map('output/reference')
6162

6263
# create an xml file containing mapping between page title and actual location
63-
root = e.Element('files')
64-
65-
for key in link_map:
66-
file_el = e.SubElement(root, 'file')
67-
file_el.set('from', key)
68-
file_el.set('to', link_map[key])
69-
70-
out = open('output/link-map.xml', 'w')
71-
out.write('<?xml version="1.0" encoding="UTF-8"?>')
72-
out.write(e.tostring(root, encoding=str, pretty_print=True))
73-
out.close()
64+
link_map.write('output/link-map.xml')
7465

7566
if __name__ == "__main__":
7667
main()

fix_devhelp-links.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'''
2020

2121
import lxml.etree as e
22+
from link_map import LinkMap
2223
import sys
2324

2425
if len(sys.argv) != 3:
@@ -30,27 +31,19 @@
3031
in_fn = sys.argv[1]
3132
out_fn = sys.argv[2]
3233

33-
root = e.parse('output/link-map.xml')
34-
el_files = root.xpath('/files/*')
35-
36-
mapping = dict()
37-
38-
for el_file in el_files:
39-
fn_from = el_file.get('from')
40-
fn_to = el_file.get('to')
41-
mapping[fn_from] = fn_to
34+
mapping = LinkMap()
35+
mapping.read('output/link-map.xml')
4236

4337
root = e.parse(in_fn)
4438

4539
el_mod = root.xpath('//*[@link]')
4640
for el in el_mod:
4741
link = el.get('link')
48-
try:
49-
link = mapping[link]
50-
except:
42+
target = mapping.get_dest(link)
43+
if target == None:
5144
print('Could not find ' + link + ' in mapping')
52-
link = '404'
53-
el.set('link', link)
45+
target = '404'
46+
el.set('link', target)
5447

5548
out_f = open(out_fn, 'w')
5649
out_f.write(e.tostring(root, encoding='unicode', pretty_print=True))

link_map.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
'''
3+
Copyright (C) 2012-2014 Povilas Kanapickas <[email protected]>
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+
21+
import lxml.etree as e
22+
23+
class LinkMap:
24+
def __init__(self):
25+
self.mapping = dict()
26+
27+
def read(self, fn):
28+
root = e.parse(fn)
29+
el_files = root.xpath('/files/*')
30+
31+
self.mapping = dict()
32+
33+
for el_file in el_files:
34+
fn_from = el_file.get('from')
35+
fn_to = el_file.get('to')
36+
self.mapping[fn_from] = fn_to
37+
38+
def write(self, fn):
39+
root = e.Element('files')
40+
41+
for key in self.mapping:
42+
file_el = e.SubElement(root, 'file')
43+
file_el.set('from', key)
44+
file_el.set('to', self.mapping[key])
45+
46+
out = open(fn, 'w')
47+
out.write('<?xml version="1.0" encoding="UTF-8"?>')
48+
out.write(e.tostring(root, encoding=str, pretty_print=True))
49+
out.close()
50+
51+
# Returns None on failure
52+
def get_dest(self, target):
53+
if target in self.mapping:
54+
return self.mapping[target]
55+
return None
56+
57+
def add_link(self, title, target):
58+
self.mapping[title] = target

0 commit comments

Comments
 (0)