Created
January 29, 2014 14:57
-
-
Save roman-kashitsyn/8689745 to your computer and use it in GitHub Desktop.
Generates a docset from cmake --help-html output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os | |
from lxml import etree | |
from contextlib import closing | |
import subprocess | |
import sqlite3 | |
import shutil | |
DIR = 'CMake.docset' | |
BASE = 'CMake.html' | |
CONTENTS = os.path.join(DIR, 'Contents') | |
RESOURCES = os.path.join(CONTENTS, 'Resources') | |
DOCUMENTS = os.path.join(RESOURCES, 'Documents') | |
NAME_MAPPING = { | |
'opt:': 'Option', | |
'command:': 'Command', | |
'prop_': 'Property', | |
'variable:': 'Variable', | |
'section_': 'Section', | |
'gen:': 'Entry', | |
'module:': 'Module', | |
} | |
def type_by_name(link_name): | |
for prefix, typename in NAME_MAPPING.items(): | |
if link_name.startswith(prefix): | |
return typename | |
def link_text(link): | |
n = link.find('../b/code') | |
if link.attrib['name'].startswith('section_'): | |
return link.tail | |
if not n is None: | |
return n.text | |
def main(): | |
if os.path.exists(DIR): | |
shutil.rmtree(DIR) | |
if not os.path.exists(DOCUMENTS): | |
os.makedirs(DOCUMENTS) | |
info = os.path.join(CONTENTS, 'Info.plist') | |
version = subprocess.check_output(['cmake', '--version']).split()[-1] | |
with open(info, 'w') as f: | |
f.write('''<?xml version="1.0" encoding="utf-8"?> | |
<plist version="1.0"> | |
<dict> | |
<key>CFBundleIdentifier</key> | |
<string>cmake</string> | |
<key>CFBundleName</key> | |
<string>CMake {0}</string> | |
<key>DocSetPlatformFamily</key> | |
<string>cmake</string> | |
</dict> | |
</plist>'''.format(version.strip())) | |
idx = os.path.join(RESOURCES, 'docSet.dsidx') | |
with closing(sqlite3.connect(idx)) as con: | |
c = con.cursor() | |
c.execute('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);') | |
c.execute('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);') | |
text = subprocess.check_output(['cmake', '--help-html']) | |
html = etree.HTML(text) | |
for link in html.iterfind('.//a'): | |
attr = link.attrib | |
if 'name' in attr: | |
link_name = attr['name'] | |
type_ = type_by_name(link_name) | |
if not type_: | |
continue | |
name = link_text(link) | |
anchor = '{0}#{1}'.format(BASE, link_name) | |
c.execute('INSERT INTO searchIndex (name, type, path) VALUES (?, ?, ?)', | |
(name, type_, anchor)) | |
with open(os.path.join(DOCUMENTS, BASE), 'w') as out: | |
out.write(text) | |
con.commit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment