1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2007 Edgewall Software
6
# This software is licensed as described in the file COPYING, which
7
# you should have received as part of this distribution. The terms
8
# are also available at http://babel.edgewall.org/wiki/License.
10
# This software consists of voluntary contributions made by many
11
# individuals. For the exact contribution history, see the revision
12
# history and logs, available at http://babel.edgewall.org/log/.
14
from distutils.cmd import Command
20
TOOLS_DIR = os.path.dirname(__file__)
23
class build_doc(Command):
24
description = 'generate the documentation'
27
"force regeneration even if no reStructuredText files have changed"),
28
('without-apidocs', None,
29
"whether to skip the generation of API documentaton"),
31
boolean_options = ['force', 'without-apidocs']
33
def initialize_options(self):
35
self.without_apidocs = False
37
def finalize_options(self):
41
from docutils.core import publish_cmdline
42
from docutils.nodes import raw
43
from docutils.parsers import rst
44
from genshi.input import HTMLParser
45
from genshi.template import TemplateLoader
47
docutils_conf = os.path.join(TOOLS_DIR, 'conf', 'docutils.ini')
48
epydoc_conf = os.path.join(TOOLS_DIR, 'conf', 'epydoc.ini')
51
from pygments import highlight
52
from pygments.lexers import get_lexer_by_name
53
from pygments.formatters import HtmlFormatter
55
def code_block(name, arguments, options, content, lineno,
56
content_offset, block_text, state, state_machine):
57
lexer = get_lexer_by_name(arguments[0])
58
html = highlight('\n'.join(content), lexer, HtmlFormatter())
59
return [raw('', html, format='html')]
60
code_block.arguments = (1, 0, 0)
61
code_block.options = {'language' : rst.directives.unchanged}
62
code_block.content = 1
63
rst.directives.register_directive('code-block', code_block)
65
print('Pygments not installed, syntax highlighting disabled')
67
loader = TemplateLoader(['doc', 'doc/common'], variable_lookup='strict')
68
for source in glob('doc/*.txt'):
69
dest = os.path.splitext(source)[0] + '.html'
70
if self.force or not os.path.exists(dest) or \
71
os.path.getmtime(dest) < os.path.getmtime(source):
72
print('building documentation file %s' % dest)
73
publish_cmdline(writer_name='html',
74
argv=['--config=%s' % docutils_conf, source,
78
html = HTMLParser(fileobj, encoding='utf-8')
79
template = loader.load('template.html')
80
output = template.generate(
82
project=self.distribution
83
).render('html', encoding='utf-8')
86
fileobj = open(dest, 'w')
92
if not self.without_apidocs:
94
from epydoc import cli
95
old_argv = sys.argv[1:]
97
'--config=%s' % epydoc_conf,
98
'--top=%s' % self.distribution.packages[0],
99
'--no-private', # epydoc bug, not read from config
102
] + self.distribution.packages
104
sys.argv[1:] = old_argv
107
print('epydoc not installed, skipping API documentation.')
110
class test_doc(Command):
111
description = 'test the code examples in the documentation'
114
def initialize_options(self):
117
def finalize_options(self):
121
for filename in glob('doc/*.txt'):
122
print('testing documentation file %s' % filename)
123
doctest.testfile(filename, False, optionflags=doctest.ELLIPSIS)