qutebrowser/scripts/generate_doc.py

478 lines
17 KiB
Python
Raw Normal View History

2014-08-02 21:13:14 +02:00
#!/usr/bin/python3
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-08-02 21:13:14 +02:00
2014-05-28 16:48:19 +02:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-05-28 16:48:19 +02:00
# 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 re
2014-05-28 16:48:19 +02:00
import os
import sys
import html
import shutil
2014-05-28 16:48:19 +02:00
import inspect
import subprocess
from collections import Counter, OrderedDict
from tempfile import mkstemp
2014-05-28 16:48:19 +02:00
sys.path.insert(0, os.getcwd())
2014-07-18 07:48:33 +02:00
import qutebrowser
2014-07-17 21:35:27 +02:00
# We import qutebrowser.app so all @cmdutils-register decorators are run.
2014-08-04 03:47:09 +02:00
import qutebrowser.app
2014-05-28 16:48:19 +02:00
import qutebrowser.commands.utils as cmdutils
2014-05-28 19:12:12 +02:00
import qutebrowser.config.configdata as configdata
2014-07-24 00:38:23 +02:00
import qutebrowser.qutebrowser as qutequtebrowser
2014-05-28 16:48:19 +02:00
from qutebrowser.utils.usertypes import enum
2014-07-17 21:35:27 +02:00
def _open_file(name, mode='w'):
"""Open a file with a preset newline/encoding mode."""
return open(name, mode, newline='\n', encoding='utf-8')
2014-05-28 16:48:19 +02:00
2014-07-17 21:35:27 +02:00
def _parse_docstring(func): # noqa
"""Generate documentation based on a docstring of a command handler.
2014-05-28 16:48:19 +02:00
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
"""
2014-07-17 21:35:27 +02:00
# pylint: disable=too-many-branches
2014-07-28 21:09:49 +02:00
State = enum('State', 'short', 'desc', # pylint: disable=invalid-name
'desc_hidden', 'arg_start', 'arg_inside', 'misc')
2014-05-28 16:48:19 +02:00
doc = inspect.getdoc(func)
lines = doc.splitlines()
cur_state = State.short
short_desc = []
long_desc = []
arg_descs = OrderedDict()
2014-05-28 16:48:19 +02:00
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.startswith('Emit:') or line.startswith('Raise:'):
cur_state = State.misc
elif line.strip() == '//':
cur_state = State.desc_hidden
2014-05-28 16:48:19 +02:00
elif line.strip():
long_desc.append(line.strip())
elif cur_state == State.misc:
if line.startswith('Args:'):
cur_state = State.arg_start
else:
pass
elif cur_state == State.desc_hidden:
if line.startswith('Args:'):
cur_state = State.arg_start
2014-05-28 16:48:19 +02:00
elif cur_state == State.arg_start:
cur_arg_name, argdesc = line.split(':', maxsplit=1)
cur_arg_name = cur_arg_name.strip().lstrip('*')
2014-05-28 16:48:19 +02:00
arg_descs[cur_arg_name] = [argdesc.strip()]
cur_state = State.arg_inside
elif cur_state == State.arg_inside:
if re.match('^[A-Z][a-z]+:$', line):
if not arg_descs[cur_arg_name][-1].strip():
arg_descs[cur_arg_name] = arg_descs[cur_arg_name][:-1]
break
elif not line.strip():
arg_descs[cur_arg_name].append('\n\n')
2014-05-28 16:48:19 +02:00
elif line[4:].startswith(' '):
arg_descs[cur_arg_name].append(line.strip() + '\n')
2014-05-28 16:48:19 +02:00
else:
cur_arg_name, argdesc = line.split(':', maxsplit=1)
cur_arg_name = cur_arg_name.strip().lstrip('*')
2014-05-28 16:48:19 +02:00
arg_descs[cur_arg_name] = [argdesc.strip()]
2014-05-28 16:56:10 +02:00
return (short_desc, long_desc, arg_descs)
def _get_cmd_syntax(name, cmd):
2014-07-17 21:35:27 +02:00
"""Get the command syntax for a command."""
2014-05-28 17:47:11 +02:00
words = []
argspec = inspect.getfullargspec(cmd.handler)
2014-05-28 18:00:47 +02:00
if argspec.defaults is not None:
2014-07-17 21:35:27 +02:00
defaults = dict(zip(reversed(argspec.args),
reversed(list(argspec.defaults))))
2014-05-28 18:00:47 +02:00
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:
2014-06-02 15:05:17 +02:00
words.append('<{}>'.format(arg))
2014-05-28 17:47:11 +02:00
elif maxargs is None or i <= maxargs:
2014-06-02 15:05:17 +02:00
words.append('[<{}>]'.format(arg))
2014-05-28 17:47:11 +02:00
i += 1
2014-08-02 23:03:35 +02:00
if argspec.varargs is not None:
words.append('[<{name}> [...]]'.format(name=argspec.varargs))
2014-05-28 18:00:47 +02:00
return (' '.join(words), defaults)
2014-05-28 17:47:11 +02:00
def _get_command_quickref(cmds):
2014-07-17 21:35:27 +02:00
"""Generate the command quick reference."""
out = []
2014-05-29 21:38:06 +02:00
out.append('[options="header",width="75%",cols="25%,75%"]')
out.append('|==============')
out.append('|Command|Description')
for name, cmd in cmds:
desc = inspect.getdoc(cmd.handler).splitlines()[0]
2014-05-29 21:08:22 +02:00
out.append('|<<cmd-{},{}>>|{}'.format(name, name, desc))
out.append('|==============')
return '\n'.join(out)
def _get_setting_quickref():
2014-07-17 21:35:27 +02:00
"""Generate the settings quick reference."""
out = []
for sectname, sect in configdata.DATA.items():
if not getattr(sect, 'descriptions'):
continue
out.append(".Quick reference for section ``{}''".format(sectname))
2014-05-29 21:38:06 +02:00
out.append('[options="header",width="75%",cols="25%,75%"]')
out.append('|==============')
out.append('|Setting|Description')
2014-07-17 21:35:27 +02:00
for optname, _option in sect.items():
desc = sect.descriptions[optname].splitlines()[0]
2014-05-29 21:08:22 +02:00
out.append('|<<setting-{}-{},{}>>|{}'.format(
sectname, optname, optname, desc))
out.append('|==============')
return '\n'.join(out)
def _get_command_doc(name, cmd):
2014-07-17 21:35:27 +02:00
"""Generate the documentation for a command."""
2014-05-29 21:08:22 +02:00
output = ['[[cmd-{}]]'.format(name)]
output += ['==== {}'.format(name)]
syntax, defaults = _get_cmd_syntax(name, cmd)
2014-08-03 01:00:25 +02:00
if syntax != name:
output.append('Syntax: +:{}+'.format(syntax))
2014-05-28 17:47:11 +02:00
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():
text = ' '.join(desc).splitlines()
2014-08-02 23:57:34 +02:00
firstline = text[0].replace(', or None', '')
item = "* +{}+: {}".format(arg, firstline)
2014-05-28 18:00:47 +02:00
if arg in defaults:
2014-08-02 23:57:34 +02:00
val = defaults[arg]
if val is None:
item += " (optional)\n"
else:
item += " (default: +{}+)\n".format(defaults[arg])
item += '\n'.join(text[1:])
2014-05-28 18:00:47 +02:00
output.append(item)
2014-05-28 16:48:19 +02:00
output.append("")
output.append("")
return '\n'.join(output)
2014-07-24 00:38:23 +02:00
def _get_action_metavar(action):
"""Get the metavar to display for an argparse action."""
if action.metavar is not None:
return "'{}'".format(action.metavar)
elif action.choices is not None:
choices = ','.join(map(str, action.choices))
return "'{{{}}}'".format(choices)
else:
return "'{}'".format(action.dest.upper())
def _format_action_args(action):
"""Get an argument string based on an argparse action."""
if action.nargs is None:
return _get_action_metavar(action)
elif action.nargs == '?':
return '[{}]'.format(_get_action_metavar(action))
elif action.nargs == '*':
return '[{mv} [{mv} ...]]'.format(mv=_get_action_metavar(action))
elif action.nargs == '+':
return '{mv} [{mv} ...]'.format(mv=_get_action_metavar(action))
elif action.nargs == '...':
return '...'
else:
return ' '.join([_get_action_metavar(action)] * action.nargs)
def _format_action(action):
"""Get an invocation string/help from an argparse action."""
if not action.option_strings:
invocation = '*{}*::'.format(_get_action_metavar(action))
else:
parts = []
if action.nargs == 0:
# Doesn't take a value, so the syntax is -s, --long
parts += ['*{}*'.format(s) for s in action.option_strings]
else:
# Takes a value, so the syntax is -s ARGS or --long ARGS.
args_string = _format_action_args(action)
for opt in action.option_strings:
parts.append('*{}* {}'.format(opt, args_string))
invocation = ', '.join(parts) + '::'
return '{}\n {}\n\n'.format(invocation, action.help)
2014-07-18 07:48:33 +02:00
def generate_manpage_header(f):
"""Generate an asciidoc header for the manpage."""
2014-07-24 01:51:23 +02:00
f.write("// DO NOT EDIT THIS FILE BY HAND!\n")
f.write("// It is generated by `scripts/generate_doc.py`.\n")
f.write("// Most likely you'll need to rerun that script, or edit that "
"instead of this file.\n")
2014-07-18 07:48:33 +02:00
f.write('= qutebrowser(1)\n')
f.write(':doctype: manpage\n')
f.write(':man source: qutebrowser\n')
f.write(':man manual: qutebrowser manpage\n')
f.write(':toc:\n')
f.write(':homepage: http://www.qutebrowser.org/\n')
2014-07-18 07:48:33 +02:00
f.write('\n')
def generate_manpage_name(f):
"""Generate the NAME-section of the manpage."""
f.write('== NAME\n')
f.write('qutebrowser - {}\n'.format(qutebrowser.__description__))
f.write('\n')
def generate_manpage_synopsis(f):
2014-07-24 22:52:53 +02:00
"""Generate the SYNOPSIS-section of the manpage."""
2014-07-18 07:48:33 +02:00
f.write('== SYNOPSIS\n')
2014-07-24 00:38:23 +02:00
f.write("*qutebrowser* ['-OPTION' ['...']] [':COMMAND' ['...']] "
"['URL' ['...']]\n")
2014-07-18 07:48:33 +02:00
f.write('\n')
2014-05-29 00:57:30 +02:00
2014-07-24 01:51:23 +02:00
def generate_manpage_description(f):
2014-07-24 22:52:53 +02:00
"""Generate the DESCRIPTION-section of the manpage."""
2014-07-24 01:51:23 +02:00
f.write('== DESCRIPTION\n')
f.write("qutebrowser is a keyboard-focused browser with with a minimal "
"GUI. It's based on Python, PyQt5 and QtWebKit and free software, "
"licensed under the GPL.\n\n")
f.write("It was inspired by other browsers/addons like dwb and "
"Vimperator/Pentadactyl.\n\n")
2014-07-24 00:38:23 +02:00
def generate_manpage_options(f):
"""Generate the OPTIONS-section of the manpage from an argparse parser."""
2014-08-04 03:47:09 +02:00
# pylint: disable=protected-access
2014-07-24 00:38:23 +02:00
parser = qutequtebrowser.get_argparser()
f.write('== OPTIONS\n')
# positionals, optionals and user-defined groups
2014-08-04 03:47:09 +02:00
for group in parser._action_groups:
2014-07-24 01:51:23 +02:00
f.write('=== {}\n'.format(group.title))
if group.description is not None:
f.write(group.description + '\n')
2014-08-04 03:47:09 +02:00
for action in group._group_actions:
2014-07-24 00:38:23 +02:00
f.write(_format_action(action))
f.write('\n')
# epilog
if parser.epilog is not None:
f.write(parser.epilog)
f.write('\n')
2014-05-29 00:57:30 +02:00
2014-07-24 01:51:23 +02:00
def generate_commands(f):
2014-07-17 21:35:27 +02:00
"""Generate the complete commands section."""
f.write('\n')
2014-07-24 01:51:23 +02:00
f.write("== COMMANDS\n")
2014-05-28 18:33:17 +02:00
normal_cmds = []
hidden_cmds = []
debug_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))
elif cmd.debug:
debug_cmds.append((name, cmd))
2014-05-28 18:33:17 +02:00
else:
normal_cmds.append((name, cmd))
normal_cmds.sort()
hidden_cmds.sort()
debug_cmds.sort()
f.write("\n")
f.write("=== Normal commands\n")
f.write(".Quick reference\n")
f.write(_get_command_quickref(normal_cmds) + "\n")
2014-05-28 18:33:17 +02:00
for name, cmd in normal_cmds:
f.write(_get_command_doc(name, cmd) + "\n")
f.write("\n")
f.write("=== Hidden commands\n")
f.write(".Quick reference\n")
f.write(_get_command_quickref(hidden_cmds) + "\n")
2014-05-28 18:33:17 +02:00
for name, cmd in hidden_cmds:
f.write(_get_command_doc(name, cmd) + "\n")
f.write("\n")
f.write("=== Debugging commands\n")
f.write("These commands are mainly intended for debugging. They are "
"hidden if qutebrowser was started without the `--debug`-flag.\n")
f.write("\n")
f.write(".Quick reference\n")
f.write(_get_command_quickref(debug_cmds) + "\n")
for name, cmd in debug_cmds:
f.write(_get_command_doc(name, cmd) + "\n")
2014-05-28 16:48:19 +02:00
2014-05-28 19:12:12 +02:00
def generate_settings(f):
2014-07-17 21:35:27 +02:00
"""Generate the complete settings section."""
f.write("\n")
2014-07-24 01:51:23 +02:00
f.write("== SETTINGS\n")
f.write(_get_setting_quickref() + "\n")
2014-05-28 19:12:12 +02:00
for sectname, sect in configdata.DATA.items():
f.write("\n")
f.write("=== {}".format(sectname) + "\n")
f.write(configdata.SECTION_DESC[sectname] + "\n")
2014-05-28 19:12:12 +02:00
if not getattr(sect, 'descriptions'):
pass
else:
for optname, option in sect.items():
f.write("\n")
f.write('[[setting-{}-{}]]'.format(sectname, optname) + "\n")
f.write("==== {}".format(optname) + "\n")
f.write(sect.descriptions[optname] + "\n")
f.write("\n")
2014-05-28 19:12:12 +02:00
valid_values = option.typ.valid_values
if valid_values is not None:
f.write("Valid values:\n")
f.write("\n")
2014-05-28 19:12:12 +02:00
for val in valid_values:
try:
desc = valid_values.descriptions[val]
f.write(" * +{}+: {}".format(val, desc) + "\n")
2014-05-28 19:12:12 +02:00
except KeyError:
f.write(" * +{}+".format(val) + "\n")
f.write("\n")
2014-05-28 22:39:02 +02:00
if option.default:
f.write("Default: +pass:[{}]+\n".format(html.escape(
2014-06-02 15:04:25 +02:00
option.default)))
2014-05-28 22:39:02 +02:00
else:
f.write("Default: empty\n")
2014-05-28 19:12:12 +02:00
2014-07-24 01:51:23 +02:00
def _get_authors():
"""Get a list of authors based on git commit logs."""
commits = subprocess.check_output(['git', 'log', '--format=%aN'])
cnt = Counter(commits.decode('utf-8').splitlines())
2014-07-24 01:51:23 +02:00
return sorted(cnt, key=lambda k: cnt[k])
def generate_manpage_author(f):
"""Generate the manpage AUTHOR section."""
f.write("== AUTHOR\n")
f.write("Contributors, sorted by the number of commits in descending "
"order:\n\n")
for author in _get_authors():
f.write('* {}\n'.format(author))
f.write('\n')
def generate_manpage_bugs(f):
"""Generate the manpage BUGS section."""
f.write('== BUGS\n')
f.write("Bugs are tracked at two locations:\n\n")
f.write("* The link:BUGS[doc/BUGS] and link:TODO[doc/TODO] files shipped "
"with qutebrowser.\n")
f.write("* The Github issue tracker at https://github.com/The-Compiler/"
"qutebrowser/issues .\n\n")
f.write("If you found a bug or have a suggestion, either open a ticket "
"in the github issue tracker, or write a mail to the "
"https://lists.schokokeks.org/mailman/listinfo.cgi/qutebrowser["
"mailinglist] at mailto:qutebrowser@lists.qutebrowser.org[].\n\n")
def generate_manpage_copyright(f):
2014-07-24 22:52:53 +02:00
"""Generate the COPYRIGHT section of the manpage."""
2014-07-24 01:51:23 +02:00
f.write('== COPYRIGHT\n')
f.write("This program 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.\n\n")
f.write("This program 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.\n\n")
f.write("You should have received a copy of the GNU General Public "
"License along with this program. If not, see "
"<http://www.gnu.org/licenses/>.\n")
def generate_manpage_resources(f):
2014-07-24 22:52:53 +02:00
"""Generate the RESOURCES section of the manpage."""
2014-07-24 01:51:23 +02:00
f.write('== RESOURCES\n\n')
f.write("* Website: http://www.qutebrowser.org/\n")
f.write("* Mailinglist: mailto:qutebrowser@lists.qutebrowser.org[] / "
"https://lists.schokokeks.org/mailman/listinfo.cgi/qutebrowser\n")
f.write("* IRC: irc://irc.freenode.org/#qutebrowser[`#qutebrowser`] on "
"http://freenode.net/[Freenode]\n")
f.write("* Github: https://github.com/The-Compiler/qutebrowser\n\n")
def regenerate_authors(filename):
"""Re-generate the authors inside README based on the commits made."""
oshandle, tmpname = mkstemp()
with _open_file(filename, mode='r') as infile, \
_open_file(oshandle, mode='w') as temp:
ignore = False
for line in infile:
if line.strip() == '// QUTE_AUTHORS_START':
ignore = True
temp.write(line)
2014-07-24 01:51:23 +02:00
for author in _get_authors():
temp.write('* {}\n'.format(author))
elif line.strip() == '// QUTE_AUTHORS_END':
temp.write(line)
ignore = False
elif not ignore:
temp.write(line)
os.remove(filename)
shutil.move(tmpname, filename)
if __name__ == '__main__':
2014-07-18 07:48:33 +02:00
with _open_file('doc/qutebrowser.1.asciidoc') as fobj:
generate_manpage_header(fobj)
generate_manpage_name(fobj)
generate_manpage_synopsis(fobj)
2014-07-24 01:51:23 +02:00
generate_manpage_description(fobj)
2014-07-24 00:38:23 +02:00
generate_manpage_options(fobj)
2014-07-17 21:35:27 +02:00
generate_settings(fobj)
generate_commands(fobj)
2014-07-24 01:51:23 +02:00
generate_manpage_bugs(fobj)
generate_manpage_author(fobj)
generate_manpage_resources(fobj)
generate_manpage_copyright(fobj)
regenerate_authors('README.asciidoc')