Created
September 9, 2009 14:05
-
-
Save tkf/183736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
- rst2latex : rst2latex | |
- latex2platex : convert using sed | |
- platex2pdf : compile .tex -> .dvi -> .pdf | |
""" | |
from distutils.cmd import Command | |
from distutils.errors import * | |
import os | |
def mkdir_if_not_exists(dir_path): | |
if not os.path.exists(dir_path): | |
print "make dir:", dir_path | |
os.mkdir(dir_path) | |
class rst2latex(Command): | |
description = ("Convert rst to latex using rst2latex") | |
user_options = [ | |
('rst-dir=', 'r', 'rst source dir.'), | |
('rst-ext=', 'e', 'rst file extention.'), | |
('rst2latex-command=', 'c', 'rst2latex command.'), | |
('rst2latex-options=', 'o', 'options for rst2html'), | |
('force','f','force convert'), | |
] | |
boolean_options = ['force'] | |
build_dir = "build" | |
build_latex_dir = os.path.join(build_dir,"latex") | |
def initialize_options(self): | |
self.rst_dir='.' | |
self.rst_ext='rst' | |
self.rst2latex_command='rst2latex' | |
self.rst2latex_options='' | |
self.force = False | |
def finalize_options(self): | |
self.rst_ext_len = len(self.rst_ext) | |
def tex_path(self, rst): | |
return os.path.join( | |
self.build_latex_dir, rst[:-self.rst_ext_len] + 'tex' ) | |
def r2l(self, rst): | |
rst_path = os.path.join(self.rst_dir ,rst) | |
tex_path = self.tex_path(rst) | |
cmd = "%s %s %s %s" % ( | |
self.rst2latex_command, self.rst2latex_options, | |
rst_path, tex_path ) | |
if ( not self.force and os.path.exists(tex_path) and | |
os.path.getmtime(rst_path) < os.path.getmtime(tex_path) ): | |
return | |
print '"%s" is modified.' % rst_path | |
print cmd | |
os.system(cmd) | |
def run(self): | |
mkdir_if_not_exists(self.build_dir) | |
mkdir_if_not_exists(self.build_latex_dir) | |
for rst in os.listdir(self.rst_dir): | |
if rst[-self.rst_ext_len:] == self.rst_ext: | |
self.r2l(rst) | |
class latex2platex(Command): | |
user_options = [ | |
('sed-file=', 's', 'batch file for sed'), | |
('sed-command=', None, 'sed command'), | |
('force','f','force convert'), | |
] | |
build_dir = "build" | |
build_latex_dir = os.path.join(build_dir,"latex") | |
build_platex_dir = os.path.join(build_dir,"platex") | |
def initialize_options(self): | |
self.sed_file='l2p.sed' | |
self.sed_command='sed' | |
self.force = False | |
def finalize_options(self): | |
if not os.path.isfile(self.sed_file): | |
raise DistutilsOptionError ( | |
'No such file "%s".' % self.sed_file) | |
def l2p(self,latex): | |
latex_path = os.path.join(self.build_latex_dir , latex) | |
platex_path = os.path.join(self.build_platex_dir, latex) | |
cmd = self.sed_command | |
cmd = cmd.replace("$ifile" ,latex_path ) | |
cmd = cmd.replace("$ofile" ,platex_path ) | |
cmd = cmd.replace("$script",self.sed_file) | |
if ( not self.force and os.path.exists(platex_path) and | |
os.path.getmtime(latex_path) < os.path.getmtime(platex_path) ): | |
return | |
print '"%s" is modified.' % latex_path | |
print cmd | |
os.system(cmd) | |
def run(self): | |
mkdir_if_not_exists(self.build_platex_dir) | |
for latex in os.listdir(self.build_latex_dir): | |
if latex[-3:] == "tex": | |
self.l2p(latex) | |
class platex2pdf(Command): | |
user_options = [ | |
('dvipdfmx=', None, ''), | |
('platex=', None, ''), | |
('force','f','force convert'), | |
] | |
build_dir = "build" | |
build_platex_dir = os.path.join(build_dir,"platex") | |
build_pdf_dir = os.path.join(build_dir,"pdf") | |
def initialize_options(self): | |
self.dvipdfmx = 'dvipdfmx' | |
self.platex = 'platex' | |
self.force = False | |
def finalize_options(self): | |
pass | |
def latex_path(self, latex): | |
return os.path.join(self.build_platex_dir, latex) | |
def p2p(self,tex): | |
platex_path = os.path.abspath( | |
os.path.join(self.build_platex_dir, tex) ) | |
dvi_path = os.path.abspath( | |
os.path.join(self.build_pdf_dir, tex[:-3]+"dvi") ) | |
pdf_path = os.path.abspath( | |
os.path.join(self.build_pdf_dir, tex[:-3]+"pdf") ) | |
cwd = os.getcwd() | |
os.chdir(self.build_pdf_dir) | |
if ( self.force or not os.path.exists(dvi_path) or | |
os.path.getmtime(platex_path) > os.path.getmtime(dvi_path) ): | |
os.system(self.platex + ' ' + platex_path) | |
os.system(self.platex + ' ' + platex_path) | |
if ( self.force or not os.path.exists(pdf_path) or | |
os.path.getmtime(dvi_path) > os.path.getmtime(pdf_path) ): | |
os.system(self.dvipdfmx + ' ' + dvi_path) | |
os.chdir(cwd) | |
def run(self): | |
mkdir_if_not_exists(self.build_pdf_dir) | |
for tex in os.listdir(self.build_platex_dir): | |
if tex[-3:] == "tex": | |
self.p2p(tex) | |
from distutils.core import setup | |
setup(cmdclass=dict( | |
rst2latex = rst2latex, | |
latex2platex = latex2platex, | |
platex2pdf = platex2pdf, | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment