#!/usr/bin/env python # Copyright 2014 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # Runs 'gn help' and various subhelps, and spits out html. # TODO: # - Handle numbered and dashed lists ->
.
# - Spit out other similar formats like wiki, markdown, whatever.
import cgi
import subprocess
import sys
def GetOutput(*args):
try:
return subprocess.check_output([sys.argv[1]] + list(args))
except subprocess.CalledProcessError:
return ''
def ParseTopLevel(out):
commands = []
output = []
for line in out.splitlines():
if line.startswith(' '):
command, sep, rest = line.partition(':')
command = command.strip()
is_option = command.startswith('-')
output_line = ['- ']
if not is_option:
commands.append(command)
output_line.append('')
output_line.append(cgi.escape(command))
if not is_option:
output_line.append('')
output_line.extend([sep + cgi.escape(rest) + '
'])
output.append(''.join(output_line))
else:
output.append('' + cgi.escape(line) + '
')
return commands, output
def ParseCommand(command, out):
first_line = True
got_example = False
output = []
for line in out.splitlines():
if first_line:
name, sep, rest = line.partition(':')
name = name.strip()
output.append('' +
cgi.escape(name + sep + rest) + '
')
first_line = False
else:
if line.startswith('Example'):
# Special subsection that's pre-formatted.
if got_example:
output.append('')
got_example = True
output.append('Example
')
output.append('')
elif not line.strip():
output.append('')
elif not line.startswith(' ') and line.endswith(':'):
# Subsection.
output.append('
' + cgi.escape(line[:-1]) + '
')
else:
output.append(cgi.escape(line))
if got_example:
output.append('
')
return output
def main():
if len(sys.argv) < 2:
print 'usage: help_as_html.py '
return 1
header = '''
GN
'''
footer = ''
commands, output = ParseTopLevel(GetOutput('help'))
for command in commands:
output += ParseCommand(command, GetOutput('help', command))
print header + '\n'.join(output) + footer
return 0
if __name__ == '__main__':
sys.exit(main())