~ubuntu-branches/ubuntu/trusty/python-babel/trusty

« back to all changes in this revision

Viewing changes to doc/common/doctools.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-10-28 10:11:31 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131028101131-zwbmm8sc29iemmlr
Tags: 1.3-2ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/rules: Run the testsuite during builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Copyright (C) 2007 Edgewall Software
4
 
# All rights reserved.
5
 
#
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.
9
 
#
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/.
13
 
 
14
 
from distutils.cmd import Command
15
 
import doctest
16
 
from glob import glob
17
 
import os
18
 
import sys
19
 
 
20
 
TOOLS_DIR = os.path.dirname(__file__)
21
 
 
22
 
 
23
 
class build_doc(Command):
24
 
    description = 'generate the documentation'
25
 
    user_options = [
26
 
        ('force', None,
27
 
         "force regeneration even if no reStructuredText files have changed"),
28
 
        ('without-apidocs', None,
29
 
         "whether to skip the generation of API documentaton"),
30
 
    ]
31
 
    boolean_options = ['force', 'without-apidocs']
32
 
 
33
 
    def initialize_options(self):
34
 
        self.force = False
35
 
        self.without_apidocs = False
36
 
 
37
 
    def finalize_options(self):
38
 
        pass
39
 
 
40
 
    def run(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
46
 
 
47
 
        docutils_conf = os.path.join(TOOLS_DIR, 'conf', 'docutils.ini')
48
 
        epydoc_conf = os.path.join(TOOLS_DIR, 'conf', 'epydoc.ini')
49
 
 
50
 
        try:
51
 
            from pygments import highlight
52
 
            from pygments.lexers import get_lexer_by_name
53
 
            from pygments.formatters import HtmlFormatter
54
 
 
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)
64
 
        except ImportError:
65
 
            print('Pygments not installed, syntax highlighting disabled')
66
 
 
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,
75
 
                                      dest])
76
 
                fileobj = open(dest)
77
 
                try:
78
 
                    html = HTMLParser(fileobj, encoding='utf-8')
79
 
                    template = loader.load('template.html')
80
 
                    output = template.generate(
81
 
                        html=html,
82
 
                        project=self.distribution
83
 
                    ).render('html', encoding='utf-8')
84
 
                finally:
85
 
                    fileobj.close()
86
 
                fileobj = open(dest, 'w')
87
 
                try:
88
 
                    fileobj.write(output)
89
 
                finally:
90
 
                    fileobj.close()
91
 
 
92
 
        if not self.without_apidocs:
93
 
            try:
94
 
                from epydoc import cli
95
 
                old_argv = sys.argv[1:]
96
 
                sys.argv[1:] = [
97
 
                    '--config=%s' % epydoc_conf,
98
 
                    '--top=%s' % self.distribution.packages[0],
99
 
                    '--no-private', # epydoc bug, not read from config
100
 
                    '--simple-term',
101
 
                    '--verbose'
102
 
                ] + self.distribution.packages
103
 
                cli.cli()
104
 
                sys.argv[1:] = old_argv
105
 
 
106
 
            except ImportError:
107
 
                print('epydoc not installed, skipping API documentation.')
108
 
 
109
 
 
110
 
class test_doc(Command):
111
 
    description = 'test the code examples in the documentation'
112
 
    user_options = []
113
 
 
114
 
    def initialize_options(self):
115
 
        pass
116
 
 
117
 
    def finalize_options(self):
118
 
        pass
119
 
 
120
 
    def run(self):
121
 
        for filename in glob('doc/*.txt'):
122
 
            print('testing documentation file %s' % filename)
123
 
            doctest.testfile(filename, False, optionflags=doctest.ELLIPSIS)