Moai.tmbundle / Support /

#!/usr/bin/env python
# Encoding: UTF-8

from __future__ import unicode_literals
from textwrap import dedent

import lxml.html
from plumbum import cli

def find_class_names(html_docs_path):
		Returns a list of comepletions passed from the html docs in the moai-sdk.
		Finds all Moai classnames.
		doc = lxml.html.parse(html_docs_path)
	except IOError as e:
		print e
	root = doc.getroot()
	return tuple(a.text for a in root.cssselect('a.el'))

def find_members(members_file_path):
		Returns a dictionary with members from all Moai classes.
		Each key is a member name and its value is a dictionary which keys are
		names of the classes that has the member in question. The keys are links
		to documentation for the member.
	doc = lxml.html.parse(members_file_path)
	root = doc.getroot()
	list_items = root.cssselect('.contents li')
	members = {}
	for li in list_items:
		name = li.text[:-5]  # Remove trailing whitespace, () and colon.
		members[name] = {}
		for a in li.getchildren():
			members[name][a.text] = a.attrib['href']
	return members

def generate_tmpreferences(completions):
		Generates a string with the completions in .tmPreferences format.
	# Sort completions alphabetically
	completions = sorted(completions)
	# Add single quotes and commas
	completions_str = ','.join("'{0}'".format(name) for name in completions)
	# Render in Apple's Old Style ASCII Plist format
	tmPreferences = dedent('''
				completions = ({0});
	return tmPreferences

class CLIApp(cli.Application):
		This might not be strictly necessary but aren't cli toolkits lovely?
	PROGNAME = 'moai_helper'
	VERSION = '0.1'
	DESCRIPTION = 'Generates completions in .tmPreferences format for TextMate.'
	def main(self, html_docs_dir):
		classes_file = 'classes.html'
		classes_path = '{0}/{1}'.format(html_docs_dir, classes_file)
		functions_file = 'functions.html'
		functions_path = '{0}/{1}'.format(html_docs_dir, functions_file)
		class_names = find_class_names(classes_path)
		function_info = find_members(functions_path)
		tmpreferences = generate_tmpreferences(class_names)
		print tmpreferences
		from pprint import pprint

if __name__ == '__main__':