|
| 1 | +#!/usr/bin/env python3 |
| 2 | +''' |
| 3 | + Copyright (C) 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 | +''' This script produces a definition file for AutoLinker extension |
| 22 | +
|
| 23 | + The definitions are written in JSON in the following format: |
| 24 | +
|
| 25 | + { |
| 26 | + "groups" : [ |
| 27 | + { |
| 28 | + name : "string", |
| 29 | + OPTIONAL base_url : "string", END |
| 30 | + urls : [ "url", "url", ... ], |
| 31 | + }, |
| 32 | + ... |
| 33 | + ], |
| 34 | + "links" : [ |
| 35 | + { |
| 36 | + string : "string", |
| 37 | + EITHER on_group : "name" OR on_page : "url" END |
| 38 | + target : "url", |
| 39 | + }, |
| 40 | + ... |
| 41 | + ], |
| 42 | + } |
| 43 | +''' |
| 44 | + |
| 45 | +import sys |
| 46 | +import json |
| 47 | + |
| 48 | +from index_transform import IndexTransform, xml_escape |
| 49 | + |
| 50 | +if len(sys.argv) != 3: |
| 51 | + print('''Please provide the file name of the index as the first argument |
| 52 | + and the file name of the destination as the second ''') |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | +out_f = open(sys.argv[2], 'w') |
| 56 | + |
| 57 | +groups = {} |
| 58 | +links = [] |
| 59 | + |
| 60 | +def get_rel_name(full_name): |
| 61 | + pos = full_name.rfind("::") |
| 62 | + return full_name[pos+2:] |
| 63 | + |
| 64 | + |
| 65 | +def is_group(el): |
| 66 | + curr_el = el |
| 67 | + while True: |
| 68 | + if curr_el.tag != 'class' and curr_el.tag != 'enum': |
| 69 | + return False |
| 70 | + curr_el = curr_el.getparent() |
| 71 | + if curr_el.tag == 'index': |
| 72 | + return True |
| 73 | + |
| 74 | +def needs_entry_in_group(el): |
| 75 | + if el.tag == 'const': return True |
| 76 | + if el.tag == 'function': return True |
| 77 | + if el.tag == 'class': return True |
| 78 | + if el.tag == 'enum': return True |
| 79 | + return False |
| 80 | + |
| 81 | +class Index2AutolinkerGroups(IndexTransform): |
| 82 | + |
| 83 | + def __init__(self): |
| 84 | + super(Index2AutolinkerGroups, self).__init__(ignore_typedefs = True) |
| 85 | + self.curr_group = None |
| 86 | + |
| 87 | + def process_item_hook(self, el, full_name, full_link): |
| 88 | + global groups |
| 89 | + if is_group(el): |
| 90 | + saved_group = self.curr_group |
| 91 | + |
| 92 | + groups[full_name] = { |
| 93 | + 'name' : full_name, |
| 94 | + 'base_url' : full_link, |
| 95 | + 'urls' : [''] |
| 96 | + } |
| 97 | + self.curr_group = full_name |
| 98 | + IndexTransform.process_item_hook(self, el, full_name, full_link) |
| 99 | + self.curr_group = saved_group |
| 100 | + else: |
| 101 | + IndexTransform.process_item_hook(self, el, full_name, full_link) |
| 102 | + |
| 103 | + if is_group(el.getparent()): |
| 104 | + base_url = groups[self.curr_group]['base_url'] |
| 105 | + if full_link.find(base_url) == 0: |
| 106 | + rel_link = full_link[len(base_url):] |
| 107 | + if not rel_link in groups[self.curr_group]['urls']: |
| 108 | + groups[self.curr_group]['urls'].append(rel_link) |
| 109 | + # else: an error has occurred somewhere - ignore |
| 110 | + |
| 111 | +class Index2AutolinkerLinks(IndexTransform): |
| 112 | + |
| 113 | + def __init__(self): |
| 114 | + super(Index2AutolinkerLinks, self).__init__() |
| 115 | + self.curr_group = None |
| 116 | + |
| 117 | + def process_item_hook(self, el, full_name, full_link): |
| 118 | + global links |
| 119 | + links.append({ 'string' : full_name, 'target' : full_link }) |
| 120 | + |
| 121 | + if is_group(el): |
| 122 | + saved_group = self.curr_group |
| 123 | + self.curr_group = full_name |
| 124 | + IndexTransform.process_item_hook(self, el, full_name, full_link) |
| 125 | + self.curr_group = saved_group |
| 126 | + else: |
| 127 | + IndexTransform.process_item_hook(self, el, full_name, full_link) |
| 128 | + |
| 129 | + if is_group(el.getparent()) and self.curr_group and needs_entry_in_group(el): |
| 130 | + |
| 131 | + links.append({ 'string' : get_rel_name(full_name), |
| 132 | + 'target' : full_link, |
| 133 | + 'on_group' : self.curr_group |
| 134 | + }) |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +tr = Index2AutolinkerGroups() |
| 139 | +tr.transform(sys.argv[1]) |
| 140 | + |
| 141 | +tr = Index2AutolinkerLinks() |
| 142 | +tr.transform(sys.argv[1]) |
| 143 | + |
| 144 | +json_groups = [] |
| 145 | +for k in groups: |
| 146 | + json_groups.append(groups[k]) |
| 147 | + |
| 148 | +out_f.write(json.dumps({ 'groups' : json_groups, 'links' : links }, indent=1, |
| 149 | + sort_keys=True)) |
| 150 | +out_f.close() |
0 commit comments