Skip to content

Commit 152f761

Browse files
committed
Initial commit of restbuilder.
0 parents  commit 152f761

File tree

13 files changed

+1216
-0
lines changed

13 files changed

+1216
-0
lines changed

CHANGES.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Changelog
2+
=========
3+
4+
restbuilder 0.1 (25 August 2013)
5+
--------------------------------
6+
* Code submitted to sphinx-contrib
7+
https://bitbucket.org/birkenfeld/sphinx-contrib
8+
* Released as sphinxcontrib-restbuilder
9+
* Added basic documentation
10+
* Unsupported/unknown tags are not printed, but send to log facility.
11+
12+
restbuilder (no version) (28 April 2012)
13+
-----------------------------------------
14+
* First release as port of a documentation generator in the NBT package
15+
https://github.com/twoolie/NBT/commit/eefbd26c422a0e5f3c89e84fabcfb951a11722b0

LICENSE.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2012-2013 by Freek Dijkstra <[email protected]>.
2+
Some rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include README.rst
2+
include LICENSE.txt
3+
include CHANGES.rst

README.rst

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.. -*- restructuredtext -*-
2+
3+
=======================
4+
README for reST Builder
5+
=======================
6+
7+
Sphinx_ extension to build reST (reStructuredText_) files.
8+
9+
This extension is in particular useful to use in combination with the autodoc
10+
extension to automatically generate documentation for use by any rst parser
11+
(such as the GitHub wiki).
12+
13+
In itself, the extension is fairly straightforward -- it takes the parsed reST
14+
file from Sphinx_ and outputs it as reST.
15+
16+
Requirements
17+
============
18+
19+
* Sphinx_ 1.0 or later
20+
* Python 2.6 or later
21+
22+
Installing
23+
==========
24+
25+
Using pip
26+
---------
27+
28+
pip install sphinxcontrib-restbuilder
29+
30+
Manual
31+
------
32+
33+
hg clone http://bitbucket.org/birkenfeld/sphinx-contrib
34+
cd sphinx-contrib/restbuilder
35+
python setup.py install
36+
37+
If you want to take a look and have a try, you can put the reST builder in
38+
an extension subdirectory, and adjust ``sys.path`` to tell Sphinx where to
39+
look for it:
40+
41+
- Add the extensions directory to the path in ``conf.py``. E.g.
42+
43+
sys.path.append(os.path.abspath('exts'))
44+
45+
Usage
46+
=====
47+
48+
- Set the builder as a extension in ``conf.py``:
49+
50+
extensions = ['sphinxcontrib.restbuilder']
51+
52+
- Run sphinx-build with target ``rst``:
53+
54+
sphinx-build -b rst -c . build/rst
55+
56+
Configuration
57+
=============
58+
59+
The following four configuration variables are defined by sphinxcontrib.restbuilder:
60+
61+
.. confval:: rst_file_suffix
62+
63+
This is the file name suffix for generated reST files. The default is
64+
``".rst"``.
65+
66+
.. confval:: rst_link_suffix
67+
68+
Suffix for generated links to reST files. The default is whatever
69+
:confval:`rst_file_suffix` is set to.
70+
71+
.. confval:: rst_file_transform
72+
73+
Function to translate a docname to a filename.
74+
By default, returns `docname` + :confval:`rst_file_suffix`.
75+
76+
.. confval:: rst_link_transform
77+
78+
Function to translate a docname to a (partial) URI.
79+
By default, returns `docname` + :confval:`rst_link_suffix`.
80+
81+
82+
Further Reading
83+
===============
84+
85+
.. _Sphinx: http://sphinx-doc.org/
86+
.. _`sphinx-contrib`: http://bitbucket.org/birkenfeld/sphinx-contrib
87+
.. _reStructuredText: http://docutils.sourceforge.net/rst.html
88+
89+
Feedback
90+
========
91+
92+
The reST builder is in a preliminary state. It's not (yet) widely used, so
93+
any feedback is particularly welcome.

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[egg_info]
2+
tag_build = dev
3+
tag_date = true
4+
5+
[aliases]
6+
release = egg_info -RDb ''

setup.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from setuptools import setup, find_packages
4+
5+
long_desc = '''
6+
Sphinx_ extension to build reST (reStructuredText_) files.
7+
8+
This extension is in particular useful to use in combination with the autodoc
9+
extension to automatically generate documentation for use by any rst parser
10+
(such as the GitHub wiki).
11+
12+
In itself, the extension is fairly straightforward -- it takes the parsed reST
13+
file from Sphinx_ and outputs it as reST.
14+
15+
.. _Sphinx: http://sphinx-doc.org/
16+
.. _reStructuredText: http://docutils.sourceforge.net/rst.html
17+
'''
18+
19+
requires = ['Sphinx>=1.0']
20+
21+
setup(
22+
name='sphinxcontrib-restbuilder',
23+
version='0.1',
24+
url='http://bitbucket.org/birkenfeld/sphinx-contrib',
25+
download_url='http://pypi.python.org/pypi/sphinxcontrib-restbuilder',
26+
license='BSD', # 2-clause
27+
author='Freek Dijkstra',
28+
author_email='[email protected]',
29+
description='Sphinx extension to output reST files.',
30+
long_description=long_desc,
31+
zip_safe=False,
32+
classifiers=[
33+
'Development Status :: 4 - Beta',
34+
'Environment :: Console',
35+
'Environment :: Web Environment',
36+
'Intended Audience :: Developers',
37+
'License :: OSI Approved :: BSD License',
38+
'Operating System :: OS Independent',
39+
'Programming Language :: Python',
40+
'Topic :: Documentation',
41+
'Topic :: Utilities',
42+
],
43+
platforms='any',
44+
packages=find_packages(),
45+
include_package_data=True,
46+
install_requires=requires,
47+
namespace_packages=['sphinxcontrib'],
48+
)

sphinxcontrib/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
sphinxcontrib
4+
~~~~~~~~~~~~~
5+
6+
This package is a namespace package that contains all extensions
7+
distributed in the ``sphinx-contrib`` distribution.
8+
9+
:copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
10+
:license: BSD, see LICENSE for details.
11+
"""
12+
13+
__import__('pkg_resources').declare_namespace(__name__)
14+

sphinxcontrib/builders/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
sphinxcontrib.builders
4+
~~~~~~~~~~~~~~~~~~~~~~
5+
6+
Custom docutils builders.
7+
"""

sphinxcontrib/builders/rst.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
sphinxcontrib.builders.rst
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
ReST Sphinx builder.
7+
8+
:copyright: Copyright 2012-2013 by Freek Dijkstra.
9+
:license: BSD, see LICENSE.txt for details.
10+
"""
11+
12+
from __future__ import (print_function, unicode_literals, absolute_import)
13+
14+
import codecs
15+
from os import path
16+
17+
from docutils.io import StringOutput
18+
19+
from sphinx.builders import Builder
20+
from sphinx.util.osutil import ensuredir, os_path, SEP
21+
from ..writers.rst import RstWriter
22+
23+
24+
# Clone of relative_uri() sphinx.util.osutil, with bug-fixes
25+
# since the original code had a few errors.
26+
# This was fixed in Sphinx 1.2b.
27+
def relative_uri(base, to):
28+
"""Return a relative URL from ``base`` to ``to``."""
29+
if to.startswith(SEP):
30+
return to
31+
b2 = base.split(SEP)
32+
t2 = to.split(SEP)
33+
# remove common segments (except the last segment)
34+
for x, y in zip(b2[:-1], t2[:-1]):
35+
if x != y:
36+
break
37+
b2.pop(0)
38+
t2.pop(0)
39+
if b2 == t2:
40+
# Special case: relative_uri('f/index.html','f/index.html')
41+
# returns '', not 'index.html'
42+
return ''
43+
if len(b2) == 1 and t2 == ['']:
44+
# Special case: relative_uri('f/index.html','f/') should
45+
# return './', not ''
46+
return '.' + SEP
47+
return ('..' + SEP) * (len(b2)-1) + SEP.join(t2)
48+
49+
50+
class RstBuilder(Builder):
51+
name = 'rst'
52+
format = 'rst'
53+
file_suffix = '.rst'
54+
link_suffix = None # defaults to file_suffix
55+
56+
def init(self):
57+
"""Load necessary templates and perform initialization."""
58+
if self.config.rst_file_suffix is not None:
59+
self.file_suffix = self.config.rst_file_suffix
60+
if self.config.rst_link_suffix is not None:
61+
self.link_suffix = self.config.rst_link_suffix
62+
elif self.link_suffix == None:
63+
self.link_suffix = self.file_suffix
64+
65+
# Function to convert the docname to a reST file name.
66+
def file_transform(docname):
67+
return docname + self.file_suffix
68+
# Function to convert the docname to a relative URI.
69+
def link_transform(docname):
70+
return docname + self.link_suffix
71+
72+
if self.config.rst_file_transform != None:
73+
self.file_transform = self.config.rst_file_transform
74+
else:
75+
self.file_transform = file_transform
76+
if self.config.rst_link_transform != None:
77+
self.link_transform = self.config.rst_link_transform
78+
else:
79+
self.link_transform = link_transform
80+
81+
def get_outdated_docs(self):
82+
"""
83+
Return an iterable of input files that are outdated.
84+
"""
85+
# This method is taken from TextBuilder.get_outdated_docs()
86+
# with minor changes to support :confval:`rst_file_transform`.
87+
for docname in self.env.found_docs:
88+
if docname not in self.env.all_docs:
89+
yield docname
90+
continue
91+
sourcename = path.join(self.env.srcdir, docname +
92+
self.env.source_suffix)
93+
targetname = path.join(self.outdir, self.file_transform(docname))
94+
print (sourcename, targetname)
95+
96+
try:
97+
targetmtime = path.getmtime(targetname)
98+
except Exception:
99+
targetmtime = 0
100+
try:
101+
srcmtime = path.getmtime(sourcename)
102+
if srcmtime > targetmtime:
103+
yield docname
104+
except EnvironmentError:
105+
# source doesn't exist anymore
106+
pass
107+
108+
def get_target_uri(self, docname, typ=None):
109+
return self.link_transform(docname)
110+
111+
def get_relative_uri(self, from_, to, typ=None):
112+
"""
113+
Return a relative URI between two source filenames.
114+
"""
115+
# This is slightly different from Builder.get_relative_uri,
116+
# as it contains a small bug (which was fixed in Sphinx 1.2).
117+
return relative_uri(self.get_target_uri(from_),
118+
self.get_target_uri(to, typ))
119+
120+
def prepare_writing(self, docnames):
121+
self.writer = RstWriter(self)
122+
123+
def write_doc(self, docname, doctree):
124+
# This method is taken from TextBuilder.write_doc()
125+
# with minor changes to support :confval:`rst_file_transform`.
126+
destination = StringOutput(encoding='utf-8')
127+
# print "write(%s,%s)" % (type(doctree), type(destination))
128+
129+
self.writer.write(doctree, destination)
130+
outfilename = path.join(self.outdir, self.file_transform(docname))
131+
# print "write(%s,%s) -> %s" % (type(doctree), type(destination), outfilename)
132+
ensuredir(path.dirname(outfilename))
133+
try:
134+
f = codecs.open(outfilename, 'w', 'utf-8')
135+
try:
136+
f.write(self.writer.output)
137+
finally:
138+
f.close()
139+
except (IOError, OSError), err:
140+
self.warn("error writing file %s: %s" % (outfilename, err))
141+
142+
def finish(self):
143+
pass

sphinxcontrib/restbuilder.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
sphinxcontrib.restbuilder
4+
=========================
5+
6+
Sphinx extension to output reST files.
7+
8+
.. moduleauthor:: Freek Dijkstra <[email protected]>
9+
10+
:copyright: Copyright 2012-2013 by Freek Dijkstra.
11+
:license: BSD, see LICENSE.txt for details.
12+
"""
13+
14+
from __future__ import (print_function, unicode_literals, absolute_import)
15+
16+
from sphinx.builders import Builder
17+
from .builders.rst import RstBuilder
18+
from .writers.rst import RstWriter
19+
20+
21+
22+
def setup(app):
23+
app.require_sphinx('1.0')
24+
app.add_builder(RstBuilder)
25+
app.add_config_value('rst_file_suffix', ".rst", False)
26+
"""This is the file name suffix for reST files"""
27+
app.add_config_value('rst_link_suffix', None, False)
28+
"""The is the suffix used in internal links. By default, takes the same value as rst_file_suffix"""
29+
app.add_config_value('rst_file_transform', None, False)
30+
"""Function to translate a docname to a filename. By default, returns docname + rst_file_suffix."""
31+
app.add_config_value('rst_link_transform', None, False)
32+
"""Function to translate a docname to a (partial) URI. By default, returns docname + rst_link_suffix."""

0 commit comments

Comments
 (0)