Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/site/source/get_api_items.py
4150 views
1
#!/usr/bin/env python3
2
# Copyright 2015 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
#
8
# This script gets all the API items defined in the emscripten documentation.
9
# These can then be used for automated adding of cross links in other scripts.
10
# It writes api_items.py which has function that is imported into get-wiki.py
11
#
12
13
import optparse
14
import os
15
import re
16
import sys
17
18
# the directory, relative to \source for the API reference
19
api_reference_directory = './docs/api_reference/'
20
21
# name to write API items to. Note, this is used by the get-wiki.py script, so
22
# if you change here, change everywhere.
23
api_item_filename = 'api_items.py'
24
25
api_reference_items = {}
26
27
28
def parseFiles():
29
"""Parse api-reference files to extract the code items.
30
"""
31
32
def addapiitems(matchobj):
33
# print 'matcobj0: %s' % matchobj.group(0)
34
# print 'matcobj1: %s' % matchobj.group(1)
35
# print 'matcobj2: %s' % matchobj.group(2)
36
# print 'matcobj3: %s' % matchobj.group(3)
37
# print 'matcobj4: %s' % matchobj.group(4)
38
39
lang = matchobj.group(2)
40
data_type = matchobj.group(3)
41
if data_type == 'function':
42
data_type = 'func'
43
api_item = matchobj.group(4)
44
api_item = api_item.strip()
45
api_item = api_item.split('(')[0]
46
try:
47
api_item = api_item.split(' ')[1]
48
except IndexError:
49
pass
50
51
# print lang
52
# print data_type
53
# print api_item
54
55
api_reference_items[api_item] = ':%s:%s:`%s`' % (lang, data_type, api_item)
56
# Add additional index for functions declared as func() rather than just func
57
if data_type == 'func':
58
api_item_index = api_item + '()'
59
api_reference_items[api_item_index] = ':%s:%s:`%s`' % (lang, data_type, api_item)
60
61
# print api_reference_items[api_item]
62
63
for file in os.listdir(api_reference_directory):
64
if file.endswith(".rst"):
65
filepath = api_reference_directory + file
66
print(file)
67
# open file
68
with open(filepath) as infile:
69
for line in infile:
70
# parse line for API items
71
re.sub(r'^\.\.\s+((\w+)\:(\w+)\:\:(.*))', addapiitems, line)
72
73
74
def exportItems():
75
"""Export the API items into form for use in another script.
76
"""
77
with open(api_item_filename, 'w') as infile:
78
# write function lead in
79
infile.write("# Auto-generated file (see get_api_items.py)\n\ndef get_mapped_items():\n mapped_wiki_inline_code = dict()\n")
80
81
for key, value in sorted(api_reference_items.items()):
82
# Write out each API item to add
83
infile.write(" mapped_wiki_inline_code['%s'] = '%s'\n" % (key, value))
84
85
# write the return function
86
infile.write(" return mapped_wiki_inline_code\n")
87
88
89
def main():
90
parser = optparse.OptionParser(usage="Usage: %prog [options] version")
91
parser.add_option("-s", "--siteapi", dest="siteapi", default="http://www.developer.nokia.com/Community/Wiki/api.php", help="Location of API")
92
(options, args) = parser.parse_args()
93
# print 'Site: %s' % options.siteapi
94
parseFiles()
95
exportItems()
96
return 0
97
98
99
if __name__ == '__main__':
100
sys.exit(main())
101
102