forked from p12tic/cppreference-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index2ddg.py
executable file
·490 lines (399 loc) · 15.8 KB
/
index2ddg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python3
'''
Copyright (C) 2013 Povilas Kanapickas <[email protected]>
This file is part of cppreference-doc
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
'''
import sys, json, os, sys, re, fnmatch
import lxml.etree as e
import lxml.html as html
from index_transform import IndexTransform
from xml_utils import xml_escape
from build_link_map import build_link_map
from ddg_parse_html import get_declarations, get_short_description, DdgException
if len(sys.argv) != 3 and not (len(sys.argv) > 2 and sys.argv[2] == 'debug'):
print('''Please provide the file name of the index as the first argument
and the file name of the output as the second ''')
sys.exit(1)
MAX_CODE_LINES = 6
# If a the second argument is 'debug', the program switches to debug mode and
# prints everything to stdout. If the third argument is provided, the program
# processes only the identifiers that match the provided string
debug = False
debug_ident = None
if len(sys.argv) > 2 and sys.argv[2] == 'debug':
debug = True
if len(sys.argv) > 3:
debug_ident = sys.argv[3]
# track the statistics of number of lines used by the entries
debug_num_lines = [0 for i in range(40)]
index_file = sys.argv[1]
output_file = sys.argv[2]
items = {}
# Entry types
# a class or struct
ITEM_TYPE_CLASS = 1
# a member function or free function
ITEM_TYPE_FUNCTION = 2
# a constructor
ITEM_TYPE_CONSTRUCTOR = 3
# a constructor that is described in the same page as the class
ITEM_TYPE_CONSTRUCTOR_INLINEMEM = 4
# a destructor
ITEM_TYPE_DESTRUCTOR = 5
# a destructor that is described in the same page as the class
ITEM_TYPE_DESTRUCTOR_INLINEMEM = 6
# a member function that is described in the same page as the class
ITEM_TYPE_FUNCTION_INLINEMEM = 7
# an enum
ITEM_TYPE_ENUM = 8
# a value of an enum
ITEM_TYPE_ENUM_CONST = 9
# a variable
ITEM_TYPE_VARIABLE = 10
# a member variable that is described in the same page as the containing class
ITEM_TYPE_VARIABLE_INLINEMEM = 11
def get_item_type(el):
if (el.tag == 'const' and el.getparent().tag == 'enum' and
el.get('link') == '.'):
return ITEM_TYPE_ENUM_CONST
if el.tag == 'function':
if el.get('link') == '.':
return ITEM_TYPE_FUNCTION_INLINEMEM
else:
return ITEM_TYPE_FUNCTION
if el.tag == 'variable':
if el.get('link') == '.':
return ITEM_TYPE_VARIABLE_INLINEMEM
else:
return ITEM_TYPE_VARIABLE
if el.tag == 'constructor':
if el.get('link') == '.':
return ITEM_TYPE_CONSTRUCTOR_INLINEMEM
else:
return ITEM_TYPE_CONSTRUCTOR
if el.tag == 'destructor':
if el.get('link') == '.':
return ITEM_TYPE_DESTRUCTOR_INLINEMEM
else:
return ITEM_TYPE_DESTRUCTOR
if el.tag == 'class':
return ITEM_TYPE_CLASS
if el.tag == 'enum':
return ITEM_TYPE_ENUM
return None # not recognized
class Index2DuckDuckGoList(IndexTransform):
def __init__(self):
super(Index2DuckDuckGoList, self).__init__(ignore_typedefs=True)
def process_item_hook(self, el, full_name, full_link):
global items
item_type = get_item_type(el)
if item_type:
if full_link in items:
items[full_link][full_name] = item_type
else:
items[full_link] = { full_name : item_type }
IndexTransform.process_item_hook(self, el, full_name, full_link)
# get a list of pages to analyze
tr = Index2DuckDuckGoList()
tr.transform(index_file)
# get a list of existing pages
html_files = []
for root, dirnames, filenames in os.walk('reference'):
for filename in fnmatch.filter(filenames, '*.html'):
html_files.append(os.path.join(root, filename))
# get a mapping between titles and pages
# linkmap = dict { title -> filename }
link_map = build_link_map('reference')
# create a list of processing instructions for each page
proc_ins = {}
for link in items:
if link in link_map.mapping:
fn = link_map.mapping[link]
if fn not in proc_ins:
proc_ins[fn] = { 'fn': fn, 'link': link, 'idents': {}}
for ident in items[link]:
proc_ins[fn]['idents'][ident] = { 'ident' : ident,
'type' : items[link][ident] }
# sort proc_ins to produce ordered output.txt
proc_ins = [ v for v in proc_ins.values() ]
proc_ins = sorted(proc_ins, key=lambda x: x['link'])
for page in proc_ins:
idents = page['idents']
idents = [ v for v in idents.values() ]
idents = sorted(idents, key=lambda x: x['ident'])
page['idents'] = idents
# process the files
# returns the unqualified name of an identifier
def get_name(ident):
if ident.find('(') != -1:
ident = re.sub('\(.*?\)', '', ident)
if ident.find('<') != -1:
ident = re.sub('\<.*?\>', '', ident)
qpos = ident.rfind('::')
if qpos != -1:
ident = ident[qpos+2:]
return ident
# returns the version number common to all declarations. Returns none if two
# declarations have different version numbers or if no version number is
# provided
def get_version(decls):
rv = None
for code,v in decls:
if v:
if rv == None:
rv = v
elif v != rv:
return None
return rv
def build_abstract(decls, desc):
line_limit = MAX_CODE_LINES
global debug_num_lines
num_lines = 0
limited = False
all_code = ''
for i,(code,ver) in enumerate(decls):
code = code.strip()
code = code.replace('<', '<').replace('>', '>')
code = '<pre><code>' + code + '</code></pre>'
code_num_lines = code.count('\n') + 1
# limit the number of code snippets to be included so that total number
# of lines is less than MAX_CODE_LINES. The limit becomes active only
# for the second and subsequent snippets.
first = True if i == 0 else False;
last = True if i == len(decls)-1 else False;
if not first:
if last:
if code_num_lines > line_limit:
limited = True
break
else:
if code_num_lines > line_limit - 1:
# -1 because we need to take into account
# < omitted declarations > message
limited = True
break
all_code += code
num_lines += 1
line_limit -= code_num_lines
if limited:
all_code += '<pre><code> < omitted declarations > </code></pre>'
# count the number of lines used
num_lines += all_code.count('\n')
if len(desc) > 110:
num_lines += 2
else:
num_lines += 1
if limited:
num_lines += 1
debug_num_lines[num_lines] += 1
if debug and num_lines >= 10:
print("# error : large number of lines: ")
print("# BEGIN ======")
print(all_code + desc)
print("# END ========")
return all_code + desc
''' Outputs additional redirects for an identifier.
Firstly, we replace '::' with spaces. Then we add two redirects: one with
unchanged text and another with '_' replaced with spaces. We strip one
level of namespace/class qualification and repeat the process with the
remaining text.
For constructors and destructors, we strip the function name and apply the
abovementioned algorithm, the only difference being that we append
(or prepend) 'constructor' or 'destructor' to the title of the redirect
respectively.
Each redirect has a 'priority', which is defined by the number of stripped
namespace/class qualifications from the entry that produced the redirect.
This is used to remove duplicate redirects. For each group of duplicate
redirects, we find the redirect with the highest priority (i.e. lowest
number of qualifications stripped) and remove all other redirects. If the
number of highest-priority redirects is more than one, then we remove all
redirects from the group altogether.
We don't add any redirects to specializations, overloads or operators.
'''
''' array of dict { 'title' -> redirect title,
'target' -> redirect target,
'priority' -> redirect priority as int
}
'''
redirects = []
def build_redirects(item_ident, item_type):
global redirects
for ch in [ '(', ')', '<', '>', 'operator' ]:
if ch in item_ident:
return
target = item_ident
parts = item_ident.split('::')
# -----
def do_parts(parts, prepend='', append=''):
global redirects
if prepend != '':
prepend = prepend + ' '
if append != '':
append = ' ' + append
p = 0
while p < len(parts):
redir1 = prepend + ' '.join(parts[p:]) + append
redir2 = prepend + ' '.join(x.replace('_',' ') for x in parts[p:]) + append
redir1 = redir1.replace(' ', ' ').replace(' ', ' ')
redir2 = redir2.replace(' ', ' ').replace(' ', ' ')
redirects.append({'title' : redir1, 'target' : target,
'priority' : p})
if redir1 != redir2:
redirects.append({'title' : redir2, 'target' : target,
'priority' : p})
p += 1
# -----
if item_type in [ ITEM_TYPE_CLASS,
ITEM_TYPE_FUNCTION,
ITEM_TYPE_FUNCTION_INLINEMEM,
ITEM_TYPE_VARIABLE,
ITEM_TYPE_VARIABLE_INLINEMEM,
ITEM_TYPE_ENUM,
ITEM_TYPE_ENUM_CONST ]:
do_parts(parts)
elif item_type in [ ITEM_TYPE_CONSTRUCTOR,
ITEM_TYPE_CONSTRUCTOR_INLINEMEM ]:
parts.pop()
do_parts(parts, prepend='constructor')
do_parts(parts, append='constructor')
elif item_type in [ ITEM_TYPE_DESTRUCTOR,
ITEM_TYPE_DESTRUCTOR_INLINEMEM ]:
parts.pop()
do_parts(parts, prepend='destructor')
do_parts(parts, append='destructor')
else:
pass # should not be here
def output_redirects():
global redirects
# convert to a convenient data structure
# dict { title -> dict { priority -> list ( targets ) } }
redir_map = {}
for r in redirects:
title = r['title']
target = r['target']
priority = r['priority']
if title not in redir_map:
redir_map[title] = {}
if priority not in redir_map[title]:
redir_map[title][priority] = []
redir_map[title][priority].append(target)
# get non-duplicate redirects
ok_redirects = [] # list ( dict { 'title' : title, 'target' : target })
for title in redir_map:
# priority decreases with increasing values
highest_prio = min(redir_map[title])
if len(redir_map[title][highest_prio]) == 1:
# not duplicate
target = redir_map[title][highest_prio][0]
ok_redirects.append({ 'title' : title, 'target' : target })
# sort the redirects
ok_redirects = sorted(ok_redirects, key=lambda x: x['title'])
# output
for r in ok_redirects:
# title
line = r['title'] + '\t'
# type
line += 'R\t'
# redirect
line += r['target'] + '\t'
# otheruses, categories, references, see_also, further_reading,
# external links, disambiguation, images, abstract, source url
line += '\t\t\t\t\t\t\t\t\t\t\n'
out.write(line)
if debug:
out = sys.stdout
else:
out = open(output_file, 'w')
#i=1
for page in proc_ins:
idents = page['idents']
link = page['link']
fn = page['fn']
if debug_ident:
ignore = True
for ident in idents:
if ident['ident'].find(debug_ident) != -1:
ignore = False
break
if ignore:
continue
#print(str(i) + '/' + str(len(proc_ins)) + ': ' + link)
#i+=1
root = e.parse('reference/'+fn, parser=html.HTMLParser())
for ident in idents:
item_ident = ident['ident']
item_type = ident['type']
# get the name by extracting the unqualified identifier
name = get_name(item_ident)
try:
debug_verbose = True if debug and debug_ident != None else False
if item_type == ITEM_TYPE_CLASS:
decls = get_declarations(root, name)
desc = get_short_description(root, get_version(decls), debug=debug_verbose)
abstract = build_abstract(decls, desc)
elif item_type in [ ITEM_TYPE_FUNCTION,
ITEM_TYPE_CONSTRUCTOR,
ITEM_TYPE_DESTRUCTOR ]:
decls = get_declarations(root, name)
desc = get_short_description(root, get_version(decls), debug=debug_verbose)
abstract = build_abstract(decls, desc)
elif item_type in [ ITEM_TYPE_FUNCTION_INLINEMEM,
ITEM_TYPE_CONSTRUCTOR_INLINEMEM,
ITEM_TYPE_DESTRUCTOR_INLINEMEM ]:
raise DdgException("INLINEMEM") # not implemented
''' Implementation notes:
* the declarations are possibly versioned
* declaration is selected from the member table
* the member table is found according to the identifier
(last part after :: is enough, hopefully)
'''
elif item_type in [ ITEM_TYPE_VARIABLE,
ITEM_TYPE_VARIABLE_INLINEMEM,
ITEM_TYPE_ENUM ]:
raise DdgException("ENUM") # not implemented
''' Implementation notes:
* the declarations are possibly versioned
'''
elif item_type == ITEM_TYPE_ENUM_CONST:
raise DdgException("ENUM_CONST") # not implemented
''' Implementation notes:
* the abstract will come from the const -> definition table,
which is always put before the first heading.
* the declaration will come from the dcl template. We need
to split the content at ';' and ',', then search for the
name of the enum. If we find duplicates, signal an error.
'''
# title
line = item_ident + '\t'
# type
line += 'A\t'
# redirect, otheruses, categories, references, see_also,
# further_reading, external links, disambiguation, images
line += '\t\t\t\t\t\t\t\t\t'
# abstract
abstract = abstract.replace('\n','\\n')
line += abstract + '\t'
# source url
line += 'http://en.cppreference.com/w/' + link + '\n'
out.write(line)
build_redirects(item_ident, item_type)
except DdgException as err:
if debug:
line = '# error (' + str(err) + "): " + link + ": " + item_ident + "\n"
out.write(line)
output_redirects()
if debug:
print('=============================')
print('Numbers of lines used:')
for i,l in enumerate(debug_num_lines):
print(str(i) + ': ' + str(l) + ' result(s)')