Skip to content

Instantly share code, notes, and snippets.

@xaizek
Created July 9, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaizek/f00e8099ab32246c85cc10c55b640cd2 to your computer and use it in GitHub Desktop.
Save xaizek/f00e8099ab32246c85cc10c55b640cd2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import xml.etree.ElementTree as ET
def dump_one_entry(name, kind, path, fragment):
if fragment != '':
path = "%s#%s" % (path, fragment)
print ("INSERT INTO searchIndex (name, type, path) "
"VALUES ('%s', '%s', '%s');") % (name, kind, path)
def dump_entries(entries):
for (name, kind, path, fragment) in entries:
dump_one_entry(name, kind, path, fragment)
def node_to_entry(node, kind, path_ending, prefix):
name = join(prefix, node.find('name').text)
refid = node.attrib['refid']
if prefix == '':
path = "%s%s" % (refid, path_ending)
fragment = ''
else:
pieces = refid.split('_')
path = "%s%s" % ('_'.join(pieces[:-1]), path_ending)
fragment = pieces[-1][1:]
return (name, kind, path, fragment)
def join(ns, name):
if ns == '':
return name
return ns + '::' + name
def dump_sqlite_index_script(node, entities, prefix=''):
for child in node:
if 'kind' not in child.attrib:
continue
childPrefix = prefix
if child.attrib['kind'] in entities:
docset_kind = entities[child.attrib['kind']]
dump_entries([node_to_entry(child, docset_kind, '.html', prefix)])
nameNode = child.find('name')
if nameNode is not None:
childPrefix = join(prefix, nameNode.text)
dump_sqlite_index_script(child, entities, childPrefix)
def print_usage():
print "Usage: extract_class_from_xml.py <path-to-index.xml>"
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Please provide path to the index.xml file"
print_usage()
exit(1)
entities = {
'class': 'cl',
'struct': 'Struct',
'function': 'func'
}
index_xml_file = sys.argv[1]
xml_root = ET.parse(index_xml_file).getroot()
print 'CREATE TABLE searchIndex('
print ' id INTEGER PRIMARY KEY,'
print ' name TEXT,'
print ' type TEXT,'
print ' path TEXT'
print ');'
print 'CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);'
print "BEGIN TRANSACTION;"
dump_sqlite_index_script(xml_root, entities)
print "COMMIT TRANSACTION;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment