|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | + Copyright (C) 2012-2013 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 | + |
| 21 | +import lxml.etree as e |
| 22 | +import sys |
| 23 | + |
| 24 | +if len(sys.argv) != 3: |
| 25 | + print '''Please provide the following 2 argument: |
| 26 | + * the file name of the source file |
| 27 | + * the file name of the destination file |
| 28 | +''' |
| 29 | + |
| 30 | +in_fn = sys.argv[1] |
| 31 | +out_fn = sys.argv[2] |
| 32 | + |
| 33 | +map_file = open('link-map.xml', 'r') |
| 34 | +root = e.XML(map_file.read()) |
| 35 | +el_files = root.xpath('/files/*') |
| 36 | + |
| 37 | +mapping = dict() |
| 38 | + |
| 39 | +for el_file in el_files: |
| 40 | + fn_from = el_file.get('from') |
| 41 | + fn_to = el_file.get('to') |
| 42 | + mapping[fn_from] = fn_to |
| 43 | + |
| 44 | +in_f = open(in_fn, 'r') |
| 45 | +root = e.XML(in_f.read()) |
| 46 | +in_f.close() |
| 47 | + |
| 48 | +el_mod = root.xpath('//*[@link]') |
| 49 | +for el in el_mod: |
| 50 | + link = el.get('link') |
| 51 | + try: |
| 52 | + link = mapping[link] |
| 53 | + except: |
| 54 | + print 'Could not find ' + link + ' in mapping' |
| 55 | + link = '404' |
| 56 | + el.set('link', link) |
| 57 | + |
| 58 | +out_f = open(out_fn, 'w') |
| 59 | +out_f.write(e.tostring(root, pretty_print=True)) |
| 60 | +out_f.close() |
| 61 | + |
0 commit comments