Path: blob/main/site/source/get_api_items.py
4150 views
#!/usr/bin/env python31# Copyright 2015 The Emscripten Authors. All rights reserved.2# Emscripten is available under two separate licenses, the MIT license and the3# University of Illinois/NCSA Open Source License. Both these licenses can be4# found in the LICENSE file.56#7# This script gets all the API items defined in the emscripten documentation.8# These can then be used for automated adding of cross links in other scripts.9# It writes api_items.py which has function that is imported into get-wiki.py10#1112import optparse13import os14import re15import sys1617# the directory, relative to \source for the API reference18api_reference_directory = './docs/api_reference/'1920# name to write API items to. Note, this is used by the get-wiki.py script, so21# if you change here, change everywhere.22api_item_filename = 'api_items.py'2324api_reference_items = {}252627def parseFiles():28"""Parse api-reference files to extract the code items.29"""3031def addapiitems(matchobj):32# print 'matcobj0: %s' % matchobj.group(0)33# print 'matcobj1: %s' % matchobj.group(1)34# print 'matcobj2: %s' % matchobj.group(2)35# print 'matcobj3: %s' % matchobj.group(3)36# print 'matcobj4: %s' % matchobj.group(4)3738lang = matchobj.group(2)39data_type = matchobj.group(3)40if data_type == 'function':41data_type = 'func'42api_item = matchobj.group(4)43api_item = api_item.strip()44api_item = api_item.split('(')[0]45try:46api_item = api_item.split(' ')[1]47except IndexError:48pass4950# print lang51# print data_type52# print api_item5354api_reference_items[api_item] = ':%s:%s:`%s`' % (lang, data_type, api_item)55# Add additional index for functions declared as func() rather than just func56if data_type == 'func':57api_item_index = api_item + '()'58api_reference_items[api_item_index] = ':%s:%s:`%s`' % (lang, data_type, api_item)5960# print api_reference_items[api_item]6162for file in os.listdir(api_reference_directory):63if file.endswith(".rst"):64filepath = api_reference_directory + file65print(file)66# open file67with open(filepath) as infile:68for line in infile:69# parse line for API items70re.sub(r'^\.\.\s+((\w+)\:(\w+)\:\:(.*))', addapiitems, line)717273def exportItems():74"""Export the API items into form for use in another script.75"""76with open(api_item_filename, 'w') as infile:77# write function lead in78infile.write("# Auto-generated file (see get_api_items.py)\n\ndef get_mapped_items():\n mapped_wiki_inline_code = dict()\n")7980for key, value in sorted(api_reference_items.items()):81# Write out each API item to add82infile.write(" mapped_wiki_inline_code['%s'] = '%s'\n" % (key, value))8384# write the return function85infile.write(" return mapped_wiki_inline_code\n")868788def main():89parser = optparse.OptionParser(usage="Usage: %prog [options] version")90parser.add_option("-s", "--siteapi", dest="siteapi", default="http://www.developer.nokia.com/Community/Wiki/api.php", help="Location of API")91(options, args) = parser.parse_args()92# print 'Site: %s' % options.siteapi93parseFiles()94exportItems()95return 0969798if __name__ == '__main__':99sys.exit(main())100101102