Example stylesheets for syntax highlight of code snippets.
[docutils/kirr.git] / sandbox / py-rest-doc / converter / __init__.py
blob82b90e5e0cdda37cfd121bfaf906d86705b6fb3a
1 # -*- coding: utf-8 -*-
2 """
3 Documentation converter - high level functions
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 :copyright: 2007 by Georg Brandl.
7 :license: Python license.
8 """
10 import sys
11 import os
12 import glob
13 import shutil
14 import codecs
15 from os import path
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
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)
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
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)))
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
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
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))
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()