Skip to content

Commit d60ca8d

Browse files
committed
Initial import of the doc tools.
1 parent 6219bcc commit d60ca8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+18328
-0
lines changed

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PYTHON ?= python
2+
3+
export PYTHONPATH = $(shell echo "$$PYTHONPATH"):./sphinx
4+
5+
.PHONY: all check clean clean-pyc pylint reindent testserver
6+
7+
all: clean-pyc check
8+
9+
check:
10+
@$(PYTHON) utils/check_sources.py -i sphinx/style/jquery.js sphinx
11+
@$(PYTHON) utils/check_sources.py converter
12+
13+
clean: clean-pyc
14+
15+
clean-pyc:
16+
find . -name '*.pyc' -exec rm -f {} +
17+
find . -name '*.pyo' -exec rm -f {} +
18+
find . -name '*~' -exec rm -f {} +
19+
20+
pylint:
21+
@pylint --rcfile utils/pylintrc sphinx converter
22+
23+
reindent:
24+
@$(PYTHON) utils/reindent.py -r -B .

README

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
py-rest-doc
2+
===========
3+
4+
This sandbox project is about moving the official Python documentation
5+
to reStructuredText.
6+
7+
8+
What you need to know
9+
---------------------
10+
11+
This project uses Python 2.5 features, so you'll need a working Python
12+
2.5 setup.
13+
14+
If you want code highlighting, you need Pygments >= 0.8, easily
15+
installable from PyPI. Jinja, the template engine, is included as a
16+
SVN external.
17+
18+
For the rest of this document, let's assume that you have a Python
19+
checkout (you need the 2.6 line, i.e. the trunk) in ~/devel/python and
20+
this checkout in the current directory.
21+
22+
To convert the LaTeX doc to reST, you first have to apply the patch in
23+
``etc/inst.diff`` to the ``inst/inst.tex`` LaTeX file in the Python
24+
checkout::
25+
26+
patch -d ~/devel/python/Doc -p0 < etc/inst.diff
27+
28+
Then, create a target directory for the reST sources and run the
29+
converter script::
30+
31+
mkdir sources
32+
python convert.py ~/devel/python/Doc sources
33+
34+
This will convert all LaTeX sources to reST files in the ``sources``
35+
directory.
36+
37+
The ``sources`` directory contains a ``conf.py`` file which contains
38+
general configuration for the build process, such as the Python
39+
version that should be shown, or the date format for "last updated on"
40+
notes.
41+
42+
43+
Building the HTML version
44+
-------------------------
45+
46+
Then, create a target directory and run ::
47+
48+
mkdir build-html
49+
python sphinx-build.py -b html sources build-html
50+
51+
This will create HTML files in the ``build-html`` directory.
52+
53+
The ``build-html`` directory will also contain a ``.doctrees``
54+
directory, which caches pickles containing the docutils doctrees for
55+
all source files, as well as an ``environment.pickle`` file that
56+
collects all meta-information and data that's needed to
57+
cross-reference the sources and generate indices.
58+
59+
60+
Running the online (web) version
61+
--------------------------------
62+
63+
First, you need to build the source with the "web" builder::
64+
65+
mkdir build-web
66+
python sphinx-build.py -b web sources build-web
67+
68+
This will create files with pickled contents for the web application
69+
in the target directory.
70+
71+
Then, you can run ::
72+
73+
python sphinx-web.py build-web
74+
75+
which will start a webserver using wsgiref on ``localhost:3000``. The
76+
web application has a configuration file ``build-web/webconf.py``,
77+
where you can configure the server and port for the application as
78+
well as different other settings specific to the web app.
79+

TODO

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Global TODO
2+
===========
3+
4+
- discuss and debug comments system
5+
- write new Makefile, handle automatic version info and checkout
6+
- write a "printable" builder (export to latex, most probably)
7+
- discuss the default role
8+
- discuss lib -> ref section move
9+
- prepare for databases other than sqlite for comments
10+
- look at the old tools/ scripts, what functionality should be rewritten
11+
- add search via Xapian?
12+
- optionally have a contents tree view in the sidebar (AJAX based)?
13+

convert.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Convert the Python documentation to Sphinx
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
:copyright: 2007 by Georg Brandl.
7+
:license: Python license.
8+
"""
9+
10+
import sys
11+
import os
12+
13+
from converter import convert_dir
14+
15+
if __name__ == '__main__':
16+
try:
17+
rootdir = sys.argv[1]
18+
destdir = os.path.abspath(sys.argv[2])
19+
except IndexError:
20+
print "usage: convert.py docrootdir destdir"
21+
sys.exit()
22+
23+
assert os.path.isdir(os.path.join(rootdir, 'texinputs'))
24+
os.chdir(rootdir)
25+
convert_dir(destdir, *sys.argv[3:])

converter/__init__.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Documentation converter - high level functions
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
:copyright: 2007 by Georg Brandl.
7+
:license: Python license.
8+
"""
9+
10+
import sys
11+
import os
12+
import glob
13+
import shutil
14+
import codecs
15+
from os import path
16+
17+
from .tokenizer import Tokenizer
18+
from .latexparser import DocParser
19+
from .restwriter import RestWriter
20+
from .filenamemap import (fn_mapping, copyfiles_mapping, newfiles_mapping,
21+
rename_mapping, dirs_to_make, toctree_mapping,
22+
amendments_mapping)
23+
from .console import red, green
24+
25+
def convert_file(infile, outfile, doraise=True, splitchap=False,
26+
toctree=None, deflang=None, labelprefix=''):
27+
inf = codecs.open(infile, 'r', 'latin1')
28+
p = DocParser(Tokenizer(inf.read()).tokenize(), infile)
29+
if not splitchap:
30+
outf = codecs.open(outfile, 'w', 'utf-8')
31+
else:
32+
outf = None
33+
r = RestWriter(outf, splitchap, toctree, deflang, labelprefix)
34+
try:
35+
r.write_document(p.parse())
36+
if splitchap:
37+
for i, chapter in enumerate(r.chapters[1:]):
38+
coutf = codecs.open('%s/%d_%s' % (
39+
path.dirname(outfile), i+1, path.basename(outfile)),
40+
'w', 'utf-8')
41+
coutf.write(chapter.getvalue())
42+
coutf.close()
43+
else:
44+
outf.close()
45+
return 1, r.warnings
46+
except Exception, err:
47+
if doraise:
48+
raise
49+
return 0, str(err)
50+
51+
52+
def convert_dir(outdirname, *args):
53+
# make directories
54+
for dirname in dirs_to_make:
55+
try:
56+
os.mkdir(path.join(outdirname, dirname))
57+
except OSError:
58+
pass
59+
60+
# copy files (currently only non-tex includes)
61+
for oldfn, newfn in copyfiles_mapping.iteritems():
62+
newpathfn = path.join(outdirname, newfn)
63+
globfns = glob.glob(oldfn)
64+
if len(globfns) == 1 and not path.isdir(newpathfn):
65+
shutil.copyfile(globfns[0], newpathfn)
66+
else:
67+
for globfn in globfns:
68+
shutil.copyfile(globfn, path.join(newpathfn,
69+
path.basename(globfn)))
70+
71+
# convert tex files
72+
# "doc" is not converted. It must be rewritten anyway.
73+
for subdir in ('api', 'dist', 'ext', 'inst', 'commontex',
74+
'lib', 'mac', 'ref', 'tut', 'whatsnew'):
75+
if args and subdir not in args:
76+
continue
77+
if subdir not in fn_mapping:
78+
continue
79+
newsubdir = fn_mapping[subdir]['__newname__']
80+
deflang = fn_mapping[subdir].get('__defaulthighlightlang__')
81+
labelprefix = fn_mapping[subdir].get('__labelprefix__', '')
82+
for filename in sorted(os.listdir(subdir)):
83+
if not filename.endswith('.tex'):
84+
continue
85+
filename = filename[:-4] # strip extension
86+
newname = fn_mapping[subdir][filename]
87+
if newname is None:
88+
continue
89+
if newname.endswith(':split'):
90+
newname = newname[:-6]
91+
splitchap = True
92+
else:
93+
splitchap = False
94+
if '/' not in newname:
95+
outfilename = path.join(outdirname, newsubdir, newname + '.rst')
96+
else:
97+
outfilename = path.join(outdirname, newname + '.rst')
98+
toctree = toctree_mapping.get(path.join(subdir, filename))
99+
infilename = path.join(subdir, filename + '.tex')
100+
print green(infilename),
101+
success, state = convert_file(infilename, outfilename, False,
102+
splitchap, toctree, deflang, labelprefix)
103+
if not success:
104+
print red("ERROR:")
105+
print red(" " + state)
106+
else:
107+
if state:
108+
print "warnings:"
109+
for warning in state:
110+
print " " + warning
111+
112+
# rename files, e.g. splitted ones
113+
for oldfn, newfn in rename_mapping.iteritems():
114+
try:
115+
if newfn is None:
116+
os.unlink(path.join(outdirname, oldfn))
117+
else:
118+
os.rename(path.join(outdirname, oldfn),
119+
path.join(outdirname, newfn))
120+
except OSError, err:
121+
if err.errno == 2:
122+
continue
123+
raise
124+
125+
# copy new files
126+
srcdirname = path.join(path.dirname(__file__), 'newfiles')
127+
for fn, newfn in newfiles_mapping.iteritems():
128+
shutil.copyfile(path.join(srcdirname, fn),
129+
path.join(outdirname, newfn))
130+
131+
# make amendments
132+
for newfn, (pre, post) in amendments_mapping.iteritems():
133+
fn = path.join(outdirname, newfn)
134+
try:
135+
ft = open(fn).read()
136+
except Exception, err:
137+
print "Error making amendments to %s: %s" % (newfn, err)
138+
continue
139+
else:
140+
fw = open(fn, 'w')
141+
fw.write(pre)
142+
fw.write(ft)
143+
fw.write(post)
144+
fw.close()

converter/console.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Console utils
4+
~~~~~~~~~~~~~
5+
6+
Format colored console output.
7+
8+
:copyright: 1998-2004 by the Gentoo Foundation.
9+
:copyright: 2006-2007 by Georg Brandl.
10+
:license: GNU GPL.
11+
"""
12+
13+
esc_seq = "\x1b["
14+
15+
codes = {}
16+
codes["reset"] = esc_seq + "39;49;00m"
17+
18+
codes["bold"] = esc_seq + "01m"
19+
codes["faint"] = esc_seq + "02m"
20+
codes["standout"] = esc_seq + "03m"
21+
codes["underline"] = esc_seq + "04m"
22+
codes["blink"] = esc_seq + "05m"
23+
codes["overline"] = esc_seq + "06m" # Who made this up? Seriously.
24+
25+
ansi_color_codes = []
26+
for x in xrange(30, 38):
27+
ansi_color_codes.append("%im" % x)
28+
ansi_color_codes.append("%i;01m" % x)
29+
30+
rgb_ansi_colors = [
31+
'0x000000', '0x555555', '0xAA0000', '0xFF5555',
32+
'0x00AA00', '0x55FF55', '0xAA5500', '0xFFFF55',
33+
'0x0000AA', '0x5555FF', '0xAA00AA', '0xFF55FF',
34+
'0x00AAAA', '0x55FFFF', '0xAAAAAA', '0xFFFFFF'
35+
]
36+
37+
for x in xrange(len(rgb_ansi_colors)):
38+
codes[rgb_ansi_colors[x]] = esc_seq + ansi_color_codes[x]
39+
40+
del x
41+
42+
codes["black"] = codes["0x000000"]
43+
codes["darkgray"] = codes["0x555555"]
44+
45+
codes["red"] = codes["0xFF5555"]
46+
codes["darkred"] = codes["0xAA0000"]
47+
48+
codes["green"] = codes["0x55FF55"]
49+
codes["darkgreen"] = codes["0x00AA00"]
50+
51+
codes["yellow"] = codes["0xFFFF55"]
52+
codes["brown"] = codes["0xAA5500"]
53+
54+
codes["blue"] = codes["0x5555FF"]
55+
codes["darkblue"] = codes["0x0000AA"]
56+
57+
codes["fuchsia"] = codes["0xFF55FF"]
58+
codes["purple"] = codes["0xAA00AA"]
59+
60+
codes["teal"] = codes["0x00AAAA"]
61+
codes["turquoise"] = codes["0x55FFFF"]
62+
63+
codes["white"] = codes["0xFFFFFF"]
64+
codes["lightgray"] = codes["0xAAAAAA"]
65+
66+
codes["darkteal"] = codes["turquoise"]
67+
codes["darkyellow"] = codes["brown"]
68+
codes["fuscia"] = codes["fuchsia"]
69+
codes["white"] = codes["bold"]
70+
71+
def nocolor():
72+
"turn off colorization"
73+
for code in codes:
74+
codes[code] = ""
75+
76+
def reset_color():
77+
return codes["reset"]
78+
79+
def colorize(color_key, text):
80+
return codes[color_key] + text + codes["reset"]
81+
82+
functions_colors = [
83+
"bold", "white", "teal", "turquoise", "darkteal",
84+
"fuscia", "fuchsia", "purple", "blue", "darkblue",
85+
"green", "darkgreen", "yellow", "brown",
86+
"darkyellow", "red", "darkred"
87+
]
88+
89+
def create_color_func(color_key):
90+
"""
91+
Return a function that formats its argument in the given color.
92+
"""
93+
def derived_func(text):
94+
return colorize(color_key, text)
95+
return derived_func
96+
97+
ns = locals()
98+
for c in functions_colors:
99+
ns[c] = create_color_func(c)
100+
101+
del c, ns

0 commit comments

Comments
 (0)