1 # -*- coding: utf-8 -*-
3 Documentation converter - high level functions
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 :copyright: 2007 by Georg Brandl.
7 :license: Python license.
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
,
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
)
30 outf
= codecs
.open(outfile
, 'w', 'utf-8')
33 r
= RestWriter(outf
, splitchap
, toctree
, deflang
, labelprefix
)
35 r
.write_document(p
.parse())
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
)),
41 coutf
.write(chapter
.getvalue())
46 except Exception, err
:
52 def convert_dir(outdirname
, *args
):
54 for dirname
in dirs_to_make
:
56 os
.mkdir(path
.join(outdirname
, dirname
))
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
)
67 for globfn
in globfns
:
68 shutil
.copyfile(globfn
, path
.join(newpathfn
,
69 path
.basename(globfn
)))
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
:
77 if subdir
not in fn_mapping
:
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'):
85 filename
= filename
[:-4] # strip extension
86 newname
= fn_mapping
[subdir
][filename
]
89 if newname
.endswith(':split'):
90 newname
= newname
[:-6]
94 if '/' not in newname
:
95 outfilename
= path
.join(outdirname
, newsubdir
, newname
+ '.rst')
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
)
105 print red(" " + state
)
109 for warning
in state
:
112 # rename files, e.g. splitted ones
113 for oldfn
, newfn
in rename_mapping
.iteritems():
116 os
.unlink(path
.join(outdirname
, oldfn
))
118 os
.rename(path
.join(outdirname
, oldfn
),
119 path
.join(outdirname
, newfn
))
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
))
132 for newfn
, (pre
, post
) in amendments_mapping
.iteritems():
133 fn
= path
.join(outdirname
, newfn
)
136 except Exception, err
:
137 print "Error making amendments to %s: %s" % (newfn
, err
)