qutebrowser/scripts/generate_doc.py

299 lines
10 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
2014-06-02 15:04:25 +02:00
import cgi
import shutil
2014-05-28 16:48:19 +02:00
import inspect
import subprocess
from collections import Counter
from tempfile import mkstemp
2014-05-28 16:48:19 +02:00
sys.path.insert(0, os.getcwd())
2014-07-17 21:35:27 +02:00
# We import qutebrowser.app so all @cmdutils-register decorators are run.
import qutebrowser.app # pylint: disable=unused-import
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-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
State = enum('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 = {}
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()
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)
def _get_cmd_syntax(name, cmd):
2014-07-17 21:35:27 +02:00
"""Get the command syntax for a command."""
# pylint: disable=no-member
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-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]
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-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-06-02 15:05:17 +02:00
item = "* +{}+: {}".format(arg, ' '.join(desc))
2014-05-28 18:00:47 +02:00
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_header(f):
2014-07-17 21:35:27 +02:00
"""Generate an asciidoc header."""
f.write('= qutebrowser manpage\n')
f.write('Florian Bruhin <mail@qutebrowser.org>\n')
f.write(':toc:\n')
f.write(':homepage: http://www.qutebrowser.org/\n')
2014-05-29 00:57:30 +02:00
def generate_commands(f):
2014-07-17 21:35:27 +02:00
"""Generate the complete commands section."""
f.write('\n')
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")
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(cgi.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
def regenerate_authors(filename):
"""Re-generate the authors inside README based on the commits made."""
commits = subprocess.check_output(['git', 'log', '--format=%aN'])
cnt = Counter(commits.decode('utf-8').splitlines())
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)
for author in sorted(cnt, key=lambda k: cnt[k]):
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-17 21:35:27 +02:00
with _open_file('doc/qutebrowser.asciidoc') as fobj:
generate_header(fobj)
generate_settings(fobj)
generate_commands(fobj)
regenerate_authors('README.asciidoc')