Skip to content

Commit d0f1c79

Browse files
committed
Transform: Move reusable components to separate folder
1 parent 1956fb7 commit d0f1c79

14 files changed

Lines changed: 456 additions & 335 deletions

index2autolinker.py

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -43,87 +43,8 @@
4343
'''
4444

4545
import argparse
46-
import sys
4746
import json
48-
49-
from index_transform import IndexTransform
50-
from xml_utils import xml_escape
51-
52-
def get_rel_name(full_name):
53-
pos = full_name.rfind("::")
54-
return full_name[pos+2:]
55-
56-
def is_group(el):
57-
curr_el = el
58-
while True:
59-
if curr_el.tag != 'class' and curr_el.tag != 'enum':
60-
return False
61-
curr_el = curr_el.getparent()
62-
if curr_el.tag == 'index':
63-
return True
64-
65-
def needs_entry_in_group(el):
66-
if el.tag == 'const': return True
67-
if el.tag == 'function': return True
68-
if el.tag == 'class': return True
69-
if el.tag == 'enum': return True
70-
if el.tag == 'variable': return True
71-
return False
72-
73-
class Index2AutolinkerGroups(IndexTransform):
74-
75-
def __init__(self):
76-
super(Index2AutolinkerGroups, self).__init__(ignore_typedefs = True)
77-
self.groups = {}
78-
self.curr_group = None
79-
80-
def process_item_hook(self, el, full_name, full_link):
81-
if is_group(el):
82-
saved_group = self.curr_group
83-
84-
self.groups[full_name] = {
85-
'name' : full_name,
86-
'base_url' : full_link,
87-
'urls' : ['']
88-
}
89-
self.curr_group = full_name
90-
IndexTransform.process_item_hook(self, el, full_name, full_link)
91-
self.curr_group = saved_group
92-
else:
93-
IndexTransform.process_item_hook(self, el, full_name, full_link)
94-
95-
if is_group(el.getparent()):
96-
base_url = self.groups[self.curr_group]['base_url']
97-
if full_link.find(base_url) == 0:
98-
rel_link = full_link[len(base_url):]
99-
if not rel_link in self.groups[self.curr_group]['urls']:
100-
self.groups[self.curr_group]['urls'].append(rel_link)
101-
# else: an error has occurred somewhere - ignore
102-
103-
class Index2AutolinkerLinks(IndexTransform):
104-
105-
def __init__(self):
106-
super(Index2AutolinkerLinks, self).__init__()
107-
self.links = []
108-
self.curr_group = None
109-
110-
def process_item_hook(self, el, full_name, full_link):
111-
self.links.append({ 'string' : full_name, 'target' : full_link })
112-
113-
if is_group(el):
114-
saved_group = self.curr_group
115-
self.curr_group = full_name
116-
IndexTransform.process_item_hook(self, el, full_name, full_link)
117-
self.curr_group = saved_group
118-
else:
119-
IndexTransform.process_item_hook(self, el, full_name, full_link)
120-
121-
if is_group(el.getparent()) and self.curr_group and needs_entry_in_group(el):
122-
123-
self.links.append({ 'string' : get_rel_name(full_name),
124-
'target' : full_link,
125-
'on_group' : self.curr_group
126-
})
47+
from index_transform.autolinker import *
12748

12849
def main():
12950
parser = argparse.ArgumentParser(prog='index2autolinker')

index2browser.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,8 @@
1818
along with this program. If not, see http://www.gnu.org/licenses/.
1919
'''
2020

21+
from index_transform.browser import *
2122
import argparse
22-
from index_transform import IndexTransform
23-
from xml_utils import xml_escape
24-
import sys
25-
26-
class Index2Browser(IndexTransform):
27-
28-
def __init__(self, out_file):
29-
super().__init__()
30-
self.out_file = out_file
31-
32-
def output_item(self, el, full_name, full_link):
33-
mark = ''
34-
if el.tag == 'const': mark = '(const)'
35-
elif el.tag == 'function': mark = '(function)'
36-
elif el.tag == 'constructor': mark = '(function)'
37-
elif el.tag == 'destructor': mark = '(function)'
38-
elif el.tag == 'class': mark = '(class)'
39-
elif el.tag == 'enum': mark = '(enum)'
40-
elif el.tag == 'variable': mark = '(variable)'
41-
elif el.tag == 'typedef': mark = '(typedef)'
42-
elif el.tag == 'specialization': mark = '(class)'
43-
elif el.tag == 'overload': mark = '(function)'
44-
45-
res = u''
46-
res += '<tt><b>' + xml_escape(full_name) + '</b></tt> [<span class="link">'
47-
res += '<a href="http://en.cppreference.com/w/' + xml_escape(full_link) + '">'
48-
res += full_link + '</a></span>] <span class="mark">' + mark + '</span>\n'
49-
return res
50-
51-
def process_item_hook(self, el, full_name, full_link):
52-
self.out_file.write('<li>' + self.output_item(el, full_name, full_link) + '<ul>')
53-
IndexTransform.process_item_hook(self, el, full_name, full_link)
54-
self.out_file.write('</ul></li>\n')
5523

5624
def main():
5725
parser = argparse.ArgumentParser(prog='index2browser')

index2ddg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import lxml.etree as e
2929
import lxml.html as html
3030

31-
from index_transform import IndexTransform
31+
from index_transform.common import IndexTransform
3232
from xml_utils import xml_escape
3333
from build_link_map import build_link_map
3434
from ddg_parse_html import get_declarations, get_short_description, DdgException

index2devhelp.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,9 @@
1818
along with this program. If not, see http://www.gnu.org/licenses/.
1919
'''
2020

21+
from index_transform.devhelp import *
2122
import argparse
22-
from index_transform import IndexTransform
2323
from xml_utils import xml_escape
24-
import sys
25-
26-
class Index2Devhelp(IndexTransform):
27-
28-
def __init__(self, out_file):
29-
super().__init__()
30-
self.out_file = out_file
31-
32-
def get_mark(self, el):
33-
if el.tag == 'const': return 'macro'
34-
elif el.tag == 'function': return 'function'
35-
elif el.tag == 'constructor': return 'function'
36-
elif el.tag == 'destructor': return 'function'
37-
elif el.tag == 'class': return 'class'
38-
elif el.tag == 'enum': return 'enum'
39-
elif el.tag == 'typedef': return 'typedef'
40-
elif el.tag == 'specialization': return 'class'
41-
elif el.tag == 'overload': return 'function'
42-
# devhelp does not support variables in its format
43-
elif el.tag == 'variable': return ''
44-
return ''
45-
46-
def process_item_hook(self, el, full_name, full_link):
47-
self.out_file.write('<keyword type="' + xml_escape(self.get_mark(el))
48-
+ '" name="' + xml_escape(full_name)
49-
+ '" link="' + xml_escape(full_link) + '"/>\n')
50-
IndexTransform.process_item_hook(self, el, full_name, full_link)
51-
5224

5325
def main():
5426
parser = argparse.ArgumentParser(prog='index2devhelp')

index2doxygen-tag.py

Lines changed: 2 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -17,158 +17,11 @@
1717
You should have received a copy of the GNU General Public License
1818
along with this program. If not, see http://www.gnu.org/licenses/.
1919
'''
20+
21+
from index_transform.doxygen_tag import *
2022
import argparse
21-
from index_transform import IndexTransform
22-
from xml_utils import xml_escape
2323
from link_map import LinkMap
24-
from functools import total_ordering
2524
from lxml import etree
26-
import sys
27-
28-
class ItemKind:
29-
VARIABLE = 0,
30-
FUNCTION = 1,
31-
CLASS = 2,
32-
TYPEDEF = 3,
33-
NAMESPACE = 4
34-
35-
@total_ordering
36-
class Item:
37-
38-
def __init__(self):
39-
self.name = ""
40-
self.full_name = ""
41-
self.kind = ItemKind.NAMESPACE
42-
self.link = ""
43-
self.members = {}
44-
45-
def __eq__(self, other):
46-
return self.full_name == other.full_name
47-
48-
def __lt__(self, other):
49-
return self.full_name < other.full_name
50-
51-
def add_to_map(ns_map, full_name, full_link, item_kind):
52-
names = full_name.split('::')
53-
parsed_names = []
54-
last_name = names.pop() # i.e. unqualified name
55-
curr_item = ns_map
56-
57-
for name in names:
58-
parsed_names.append(name)
59-
if name in curr_item.members:
60-
curr_item = curr_item.members[name]
61-
if curr_item.kind not in [ ItemKind.CLASS, ItemKind.NAMESPACE ]:
62-
print("ERROR: " + name + " in " + full_name +
63-
" is not a class or namespace")
64-
return
65-
else:
66-
# we add inexistent intermediate elements as namespaces and convert
67-
# them to classes later when class definitions are encountered
68-
new_item = Item()
69-
new_item.name = name
70-
new_item.full_name = "::".join(parsed_names)
71-
new_item.kind = ItemKind.NAMESPACE
72-
73-
curr_item.members[name] = new_item
74-
curr_item = new_item
75-
76-
if last_name in curr_item.members:
77-
curr_item = curr_item.members[last_name]
78-
if (item_kind == ItemKind.CLASS and
79-
curr_item.kind in [ ItemKind.CLASS, ItemKind.NAMESPACE ]):
80-
curr_item.kind = item_kind # fix namespaces that are actually classes
81-
curr_item.link = full_link
82-
else:
83-
print("ERROR: Duplicate element: " + full_name)
84-
else:
85-
new_item = Item()
86-
new_item.full_name = full_name
87-
new_item.name = last_name
88-
new_item.link = full_link
89-
new_item.kind = item_kind
90-
curr_item.members[last_name] = new_item
91-
curr_item = new_item
92-
return
93-
94-
def print_members(out_f, link_map, curr_item):
95-
for item in sorted(curr_item.members.values()):
96-
if link_map:
97-
link = link_map.get_dest(item.link)
98-
if link == None and item.kind != ItemKind.NAMESPACE:
99-
print("WARN: " + item.full_name + " contains invalid link")
100-
link = '404'
101-
else:
102-
link = item.link
103-
104-
if item.kind == ItemKind.VARIABLE:
105-
out_f.write(' <member kind="variable">\n' +
106-
' <type>T</type>\n' +
107-
' <name>' + xml_escape(item.name) + '</name>\n' +
108-
' <anchorfile>' + xml_escape(link) + '</anchorfile>\n' +
109-
' <anchor></anchor>\n' +
110-
' <arglist></arglist>\n' +
111-
' </member>\n')
112-
elif item.kind == ItemKind.FUNCTION:
113-
out_f.write(' <member kind="function">\n' +
114-
' <type>T</type>\n' +
115-
' <name>' + xml_escape(item.name) + '</name>\n' +
116-
' <anchorfile>' + xml_escape(link) + '</anchorfile>\n' +
117-
' <anchor></anchor>\n' +
118-
' <arglist>(T... args)</arglist>\n' +
119-
' </member>\n')
120-
elif item.kind == ItemKind.CLASS:
121-
out_f.write(' <class kind="class">' + xml_escape(item.full_name) + '</class>\n')
122-
elif item.kind == ItemKind.NAMESPACE:
123-
out_f.write(' <namespace>' + xml_escape(item.full_name) + '</namespace>\n')
124-
125-
def print_map_item(out_f, link_map, curr_item):
126-
item_kind_to_attr = { ItemKind.NAMESPACE : 'namespace',
127-
ItemKind.CLASS : 'class' }
128-
if curr_item.kind not in item_kind_to_attr:
129-
print('ERROR: only namespaces and classes can have members')
130-
return
131-
132-
out_f.write(' <compound kind="' + item_kind_to_attr[curr_item.kind] + '">\n' +
133-
' <name>' + xml_escape(curr_item.full_name) + '</name>\n' +
134-
' <filename>' + xml_escape(curr_item.link) + '</filename>\n')
135-
print_members(out_f, link_map, curr_item)
136-
out_f.write(' </compound>\n')
137-
138-
for item in sorted(curr_item.members.values()):
139-
if item.kind in [ ItemKind.NAMESPACE, ItemKind.CLASS ]:
140-
print_map_item(out_f, link_map, item)
141-
142-
def print_map(out_f, link_map, ns_map):
143-
for item in sorted(ns_map.members.values()):
144-
if item.kind in [ ItemKind.NAMESPACE, ItemKind.CLASS ]:
145-
print_map_item(out_f, link_map, item)
146-
else:
147-
print("WARN: " + item.full_name + " ignored")
148-
149-
class Index2Devhelp(IndexTransform):
150-
def __init__(self, ns_map):
151-
super().__init__()
152-
self.ns_map = ns_map
153-
154-
def get_item_kind(self, el):
155-
if el.tag == 'const': return None
156-
elif el.tag == 'function': return ItemKind.FUNCTION
157-
elif el.tag == 'constructor': return ItemKind.FUNCTION
158-
elif el.tag == 'destructor': return ItemKind.FUNCTION
159-
elif el.tag == 'class': return ItemKind.CLASS
160-
elif el.tag == 'enum': return 'enum'
161-
elif el.tag == 'typedef': return ItemKind.CLASS
162-
elif el.tag == 'specialization': return None
163-
elif el.tag == 'overload': return None
164-
elif el.tag == 'variable': return ItemKind.VARIABLE
165-
return None
166-
167-
def process_item_hook(self, el, full_name, full_link):
168-
item_kind = self.get_item_kind(el)
169-
if item_kind != None:
170-
add_to_map(self.ns_map, full_name, full_link, item_kind)
171-
IndexTransform.process_item_hook(self, el, full_name, full_link)
17225

17326
def main():
17427
parser = argparse.ArgumentParser(prog='index2doxygen-tag')

index2highlight.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,8 @@
1818
along with this program. If not, see http://www.gnu.org/licenses/.
1919
'''
2020

21+
from index_transform.highlight import *
2122
import argparse
22-
from index_transform import IndexTransform
23-
import sys
24-
25-
class Index2Highlight(IndexTransform):
26-
def __init__(self, out_file):
27-
super().__init__()
28-
self.out_file = out_file
29-
30-
def check_is_member(self, el):
31-
if el.getparent().tag == 'index':
32-
return False
33-
if el.tag == 'function': return True
34-
elif el.tag == 'variable': return True
35-
elif el.tag == 'constructor': return True
36-
elif el.tag == 'destructor': return True
37-
return False
38-
39-
def process_item_hook(self, el, full_name, full_link):
40-
is_member = False
41-
if self.check_is_member(el): pass
42-
elif '<' in full_name: pass
43-
elif '>' in full_name: pass
44-
elif '(' in full_name: pass
45-
elif ')' in full_name: pass
46-
else:
47-
self.out_file.write(full_name + ' => ' + full_link + '\n')
48-
49-
IndexTransform.process_item_hook(self, el, full_name, full_link)
50-
51-
def inherits_worker(self, parent_name, pending, finished=list()):
52-
pass # do not walk the inheritance hierarchy
5323

5424
def main():
5525
parser = argparse.ArgumentParser(prog='index2highlight')

0 commit comments

Comments
 (0)