qutebrowser/scripts/generate_manpage.py

181 lines
5.7 KiB
Python
Raw Normal View History

2014-05-28 16:48:19 +02:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Generate asciidoc source for qutebrowser based on docstrings."""
import os
import sys
import inspect
sys.path.insert(0, os.getcwd())
import qutebrowser.app
import qutebrowser.commands.utils as cmdutils
2014-05-28 19:12:12 +02:00
import qutebrowser.config.configdata as configdata
2014-05-28 16:48:19 +02:00
from qutebrowser.utils.usertypes import enum
2014-05-28 16:56:10 +02:00
def parse_docstring(func):
2014-05-28 16:48:19 +02:00
"""Generates documentation based on a docstring of a command handler.
The docstring needs to follow the format described in HACKING.
2014-05-28 16:56:10 +02:00
Args:
func: The function to generate the docstring for.
Return:
A (short_desc, long_desc, arg_descs) tuple.
2014-05-28 16:48:19 +02:00
"""
State = enum('short', 'desc', 'arg_start', 'arg_inside')
doc = inspect.getdoc(func)
lines = doc.splitlines()
cur_state = State.short
short_desc = []
long_desc = []
arg_descs = {}
cur_arg_name = None
for line in lines:
if cur_state == State.short:
if not line:
cur_state = State.desc
else:
short_desc.append(line.strip())
elif cur_state == State.desc:
if line.startswith('Args:'):
cur_state = State.arg_start
elif line.strip():
long_desc.append(line.strip())
elif cur_state == State.arg_start:
cur_arg_name, argdesc = line.split(':', maxsplit=1)
cur_arg_name = cur_arg_name.strip()
arg_descs[cur_arg_name] = [argdesc.strip()]
cur_state = State.arg_inside
elif cur_state == State.arg_inside:
if not line:
break
elif line[4:].startswith(' '):
arg_descs[cur_arg_name].append(line.strip())
else:
cur_arg_name, argdesc = line.split(':', maxsplit=1)
cur_arg_name = cur_arg_name.strip()
arg_descs[cur_arg_name] = [argdesc.strip()]
2014-05-28 16:56:10 +02:00
return (short_desc, long_desc, arg_descs)
2014-05-28 17:47:11 +02:00
def get_cmd_syntax(name, cmd):
words = []
argspec = inspect.getfullargspec(cmd.handler)
2014-05-28 18:00:47 +02:00
if argspec.defaults is not None:
defaults = dict(zip(reversed(argspec.args), reversed(list(argspec.defaults))))
else:
defaults = {}
2014-05-28 17:47:11 +02:00
words.append(name)
minargs, maxargs = cmd.nargs
i = 1
for arg in argspec.args:
if arg in ['self', 'count']:
continue
if minargs is not None and i <= minargs:
words.append('_<{}>_'.format(arg))
elif maxargs is None or i <= maxargs:
words.append('_[<{}>]_'.format(arg))
i += 1
2014-05-28 18:00:47 +02:00
return (' '.join(words), defaults)
2014-05-28 17:47:11 +02:00
def get_command_doc(name, cmd):
2014-05-28 16:48:19 +02:00
output = ['==== {}'.format(name)]
2014-05-28 18:00:47 +02:00
syntax, defaults = get_cmd_syntax(name, cmd)
2014-05-28 17:47:11 +02:00
output.append('+:{}+'.format(syntax))
output.append("")
short_desc, long_desc, arg_descs = parse_docstring(cmd.handler)
2014-05-28 16:48:19 +02:00
output.append(' '.join(short_desc))
output.append("")
output.append(' '.join(long_desc))
if arg_descs:
output.append("")
for arg, desc in arg_descs.items():
2014-05-28 18:00:47 +02:00
item = "* +_{}_+: {}".format(arg, ' '.join(desc))
if arg in defaults:
item += " (default: +{}+)".format(defaults[arg])
output.append(item)
2014-05-28 16:48:19 +02:00
output.append("")
output.append("")
return '\n'.join(output)
def generate_commands():
2014-05-28 22:39:02 +02:00
print()
2014-05-28 16:48:19 +02:00
print("== Commands")
2014-05-28 18:33:17 +02:00
normal_cmds = []
hidden_cmds = []
2014-05-28 16:48:19 +02:00
for name, cmd in cmdutils.cmd_dict.items():
2014-05-28 18:33:17 +02:00
if cmd.hide:
hidden_cmds.append((name, cmd))
else:
normal_cmds.append((name, cmd))
normal_cmds.sort()
hidden_cmds.sort()
2014-05-28 22:39:02 +02:00
print()
2014-05-28 18:33:17 +02:00
print("=== Normal commands")
for name, cmd in normal_cmds:
print(get_command_doc(name, cmd))
2014-05-28 22:39:02 +02:00
print()
2014-05-28 18:33:17 +02:00
print("=== Hidden commands")
for name, cmd in hidden_cmds:
2014-05-28 17:47:11 +02:00
print(get_command_doc(name, cmd))
2014-05-28 16:48:19 +02:00
2014-05-28 19:12:12 +02:00
def generate_settings():
2014-05-28 22:39:02 +02:00
print()
2014-05-28 19:12:12 +02:00
print("== Settings")
for sectname, sect in configdata.DATA.items():
print()
print("=== {}".format(sectname))
print(configdata.SECTION_DESC[sectname])
if not getattr(sect, 'descriptions'):
pass
else:
for optname, option in sect.items():
print()
print("==== {}".format(optname))
print(sect.descriptions[optname])
print()
valid_values = option.typ.valid_values
if valid_values is not None:
print("Valid values:")
print()
for val in valid_values:
try:
desc = valid_values.descriptions[val]
2014-05-28 22:39:02 +02:00
print(" * +{}+: {}".format(val, desc))
2014-05-28 19:12:12 +02:00
except KeyError:
2014-05-28 22:39:02 +02:00
print(" * +{}+".format(val))
print()
if option.default:
print("Default: +pass:[{}]+".format(option.default))
else:
print("Default: empty")
2014-05-28 19:12:12 +02:00
generate_settings()
2014-05-28 16:48:19 +02:00
generate_commands()