diff --git a/_py/cmdline/pyrest.py b/_py/cmdline/pyrest.py deleted file mode 100755 index 490aa9d02..000000000 --- a/_py/cmdline/pyrest.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python -""" -invoke - - py.rest filename1.txt directory - -to generate html files from ReST. - -It is also possible to generate pdf files using the --topdf option. - -http://docutils.sourceforge.net/docs/user/rst/quickref.html - -""" - -import os, sys -import py - -if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()): - def log(msg): - print(msg) -else: - def log(msg): - pass - - -parser = py.std.optparse.OptionParser(usage=__doc__) -parser.add_option("--topdf", action="store_true", dest="topdf", default=False, - help="generate pdf files") -parser.add_option("--stylesheet", dest="stylesheet", default=None, - help="use specified latex style sheet") -parser.add_option("--debug", action="store_true", dest="debug", - default=False, - help="print debug output and don't delete files") - - -def main(): - try: - from _py.rest import directive, resthtml - from _py.rest.latex import process_rest_file, process_configfile - except ImportError: - e = sys.exc_info()[1] - print(str(e)) - sys.exit(1) - - (options, args) = parser.parse_args() - - if len(args) == 0: - filenames = [py.path.svnwc()] - else: - filenames = [py.path.svnwc(x) for x in args] - - if options.topdf: - directive.set_backend_and_register_directives("latex") - - for p in filenames: - if not p.check(): - log("path %s not found, ignoring" % p) - continue - def fil(p): - return p.check(fnmatch='*.txt', versioned=True) - def rec(p): - return p.check(dotfile=0) - if p.check(dir=1): - for x in p.visit(fil, rec): - resthtml.process(x) - elif p.check(file=1): - if p.ext == ".rst2pdfconfig": - directive.set_backend_and_register_directives("latex") - process_configfile(p, options.debug) - else: - if options.topdf: - cfg = p.new(ext=".rst2pdfconfig") - if cfg.check(): - print("using config file %s" % (cfg, )) - process_configfile(cfg, options.debug) - else: - process_rest_file(p.localpath, - options.stylesheet, - options.debug) - else: - resthtml.process(p) - diff --git a/_py/rest/__init__.py b/_py/rest/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/_py/rest/convert.py b/_py/rest/convert.py deleted file mode 100644 index 02ffc8ab5..000000000 --- a/_py/rest/convert.py +++ /dev/null @@ -1,163 +0,0 @@ -import py - -from _py.process.cmdexec import ExecutionFailed -# utility functions to convert between various formats - -format_to_dotargument = {"png": "png", - "eps": "ps", - "ps": "ps", - "pdf": "ps", - } - -def ps2eps(ps): - # XXX write a pure python version - if not py.path.local.sysfind("ps2epsi") and \ - not py.path.local.sysfind("ps2eps"): - raise SystemExit("neither ps2eps nor ps2epsi found") - try: - eps = ps.new(ext=".eps") - py.process.cmdexec('ps2epsi "%s" "%s"' % (ps, eps)) - except ExecutionFailed: - py.process.cmdexec('ps2eps -l -f "%s"' % ps) - -def ps2pdf(ps, compat_level="1.2"): - if not py.path.local.sysfind("gs"): - raise SystemExit("ERROR: gs not found") - pdf = ps.new(ext=".pdf") - options = dict(OPTIONS="-dSAFER -dCompatibilityLevel=%s" % compat_level, - infile=ps, outfile=pdf) - cmd = ('gs %(OPTIONS)s -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite ' - '"-sOutputFile=%(outfile)s" %(OPTIONS)s -c .setpdfwrite ' - '-f "%(infile)s"') % options - py.process.cmdexec(cmd) - return pdf - -def eps2pdf(eps): - # XXX write a pure python version - if not py.path.local.sysfind("epstopdf"): - raise SystemExit("ERROR: epstopdf not found") - py.process.cmdexec('epstopdf "%s"' % eps) - -def dvi2eps(dvi, dest=None): - if dest is None: - dest = eps.new(ext=".eps") - command = 'dvips -q -E -n 1 -D 600 -p 1 -o "%s" "%s"' % (dest, dvi) - if not py.path.local.sysfind("dvips"): - raise SystemExit("ERROR: dvips not found") - py.process.cmdexec(command) - -def convert_dot(fn, new_extension): - if not py.path.local.sysfind("dot"): - raise SystemExit("ERROR: dot not found") - result = fn.new(ext=new_extension) - print(result) - arg = "-T%s" % (format_to_dotargument[new_extension], ) - py.std.os.system('dot "%s" "%s" > "%s"' % (arg, fn, result)) - if new_extension == "eps": - ps = result.new(ext="ps") - result.move(ps) - ps2eps(ps) - ps.remove() - elif new_extension == "pdf": - # convert to eps file first, to get the bounding box right - eps = result.new(ext="eps") - ps = result.new(ext="ps") - result.move(ps) - ps2eps(ps) - eps2pdf(eps) - ps.remove() - eps.remove() - return result - - -class latexformula2png(object): - def __init__(self, formula, dest, temp=None): - self.formula = formula - try: - import Image - self.Image = Image - self.scale = 2 # create a larger image - self.upscale = 5 # create the image upscale times larger, then scale it down - except ImportError: - self.scale = 2 - self.upscale = 1 - self.Image = None - self.output_format = ('pngmono', 'pnggray', 'pngalpha')[2] - if temp is None: - temp = py.test.ensuretemp("latexformula") - self.temp = temp - self.latex = self.temp.join('formula.tex') - self.dvi = self.temp.join('formula.dvi') - self.eps = self.temp.join('formula.eps') - self.png = self.temp.join('formula.png') - self.saveas(dest) - - def saveas(self, dest): - self.gen_latex() - self.gen_dvi() - dvi2eps(self.dvi, self.eps) - self.gen_png() - self.scale_image() - self.png.copy(dest) - - def gen_latex(self): - self.latex.write (""" - \\documentclass{article} - \\pagestyle{empty} - \\begin{document} - - %s - \\pagebreak - - \\end{document} - """ % (self.formula)) - - def gen_dvi(self): - origdir = py.path.local() - self.temp.chdir() - py.process.cmdexec('latex "%s"' % (self.latex)) - origdir.chdir() - - def gen_png(self): - tempdir = py.path.local.mkdtemp() - - re_bbox = py.std.re.compile('%%BoundingBox:\s*(\d+) (\d+) (\d+) (\d+)') - eps = self.eps.read() - x1, y1, x2, y2 = [int(i) for i in re_bbox.search(eps).groups()] - X = x2 - x1 + 2 - Y = y2 - y1 + 2 - mx = -x1 - my = -y1 - ps = self.temp.join('temp.ps') - source = self.eps - ps.write(""" - 1 1 1 setrgbcolor - newpath - -1 -1 moveto - %(X)d -1 lineto - %(X)d %(Y)d lineto - -1 %(Y)d lineto - closepath - fill - %(mx)d %(my)d translate - 0 0 0 setrgbcolor - (%(source)s) run - - """ % locals()) - - sx = int((x2 - x1) * self.scale * self.upscale) - sy = int((y2 - y1) * self.scale * self.upscale) - res = 72 * self.scale * self.upscale - command = ('gs -q -g%dx%d -r%dx%d -sDEVICE=%s -sOutputFile="%s" ' - '-dNOPAUSE -dBATCH "%s"') % ( - sx, sy, res, res, self.output_format, self.png, ps) - py.process.cmdexec(command) - - def scale_image(self): - if self.Image is None: - return - image = self.Image.open(str(self.png)) - image.resize((image.size[0] / self.upscale, - image.size[1] / self.upscale), - self.Image.ANTIALIAS).save(str(self.png)) - diff --git a/_py/rest/directive.py b/_py/rest/directive.py deleted file mode 100644 index e80cf5e5b..000000000 --- a/_py/rest/directive.py +++ /dev/null @@ -1,115 +0,0 @@ -# XXX this file is messy since it tries to deal with several docutils versions -import py - -from _py.rest.convert import convert_dot, latexformula2png - -import sys -import docutils -from docutils import nodes -from docutils.parsers.rst import directives, states, roles -from docutils.parsers.rst.directives import images - -if hasattr(images, "image"): - directives_are_functions = True -else: - directives_are_functions = False - -try: - from docutils.utils import unescape # docutils version > 0.3.5 -except ImportError: - from docutils.parsers.rst.states import unescape # docutils 0.3.5 - -if not directives_are_functions: - ImageClass = images.Image - -else: - class ImageClass(object): - option_spec = images.image.options - def run(self): - return images.image('image', - self.arguments, - self.options, - self.content, - self.lineno, - self.content_offset, - self.block_text, - self.state, - self.state_machine) - - -backend_to_image_format = {"html": "png", "latex": "pdf"} - -class GraphvizDirective(ImageClass): - def convert(self, fn, path): - path = py.path.local(path).dirpath() - dot = path.join(fn) - result = convert_dot(dot, backend_to_image_format[_backend]) - return result.relto(path) - - def run(self): - newname = self.convert(self.arguments[0], - self.state.document.settings._source) - text = self.block_text.replace("graphviz", "image", 1) - self.block_text = text.replace(self.arguments[0], newname, 1) - self.name = 'image' - self.arguments = [newname] - return ImageClass.run(self) - - def old_interface(self): - def f(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - for arg in "name arguments options content lineno " \ - "content_offset block_text state state_machine".split(): - setattr(self, arg, locals()[arg]) - return self.run() - f.arguments = (1, 0, 1) - f.options = self.option_spec - return f - - -_backend = None -def set_backend_and_register_directives(backend): - #XXX this is only used to work around the inflexibility of docutils: - # a directive does not know the target format - global _backend - _backend = backend - if not directives_are_functions: - directives.register_directive("graphviz", GraphvizDirective) - else: - directives.register_directive("graphviz", - GraphvizDirective().old_interface()) - roles.register_canonical_role("latexformula", latexformula_role) - -def latexformula_role(name, rawtext, text, lineno, inliner, - options={}, content=[]): - if _backend == 'latex': - options['format'] = 'latex' - return roles.raw_role(name, rawtext, text, lineno, inliner, - options, content) - else: - # XXX: make the place of the image directory configurable - sourcedir = py.path.local(inliner.document.settings._source).dirpath() - imagedir = sourcedir.join("img") - if not imagedir.check(): - imagedir.mkdir() - # create halfway senseful imagename: - # use hash of formula + alphanumeric characters of it - # could - imagename = "%s_%s.png" % ( - hash(text), "".join([c for c in text if c.isalnum()])) - image = imagedir.join(imagename) - latexformula2png(unescape(text, True), image) - imagenode = nodes.image(image.relto(sourcedir), uri=image.relto(sourcedir)) - return [imagenode], [] -latexformula_role.content = True -latexformula_role.options = {} - -def register_linkrole(role_name, callback): - def source_role(name, rawtext, text, lineno, inliner, options={}, - content=[]): - text, target = callback(name, text) - reference_node = nodes.reference(rawtext, text, name=text, refuri=target) - return [reference_node], [] - source_role.content = True - source_role.options = {} - roles.register_canonical_role(role_name, source_role) diff --git a/_py/rest/latex.py b/_py/rest/latex.py deleted file mode 100644 index e8435ec69..000000000 --- a/_py/rest/latex.py +++ /dev/null @@ -1,154 +0,0 @@ -import py - -from _py.process.cmdexec import ExecutionFailed - -font_to_package = {"times": "times", "helvetica": "times", - "new century schoolbock": "newcent", "avant garde": "newcent", - "palatino": "palatino", - } -sans_serif_fonts = {"helvetica": True, - "avant garde": True, - } - - -def merge_files(pathlist, pagebreak=False): - if len(pathlist) == 1: - return pathlist[0].read() - sectnum = False - toc = False - result = [] - includes = {} - for path in pathlist: - lines = path.readlines() - for line in lines: - # prevent several table of contents - # and especially sectnum several times - if ".. contents::" in line: - if not toc: - toc = True - result.append(line) - elif ".. sectnum::" in line: - if not sectnum: - sectnum = True - result.append(line) - elif line.strip().startswith(".. include:: "): - #XXX slightly unsafe - inc = line.strip()[13:] - if inc not in includes: - includes[inc] = True - result.append(line) - else: - result.append(line) - if pagebreak: - result.append(".. raw:: latex \n\n \\newpage\n\n") - if pagebreak: - result.pop() #remove the last pagebreak again - return "".join(result) - -def create_stylesheet(options, path): - fill_in = {} - if "logo" in options: - fill_in["have_logo"] = "" - fill_in["logo"] = options["logo"] - else: - fill_in["have_logo"] = "%" - fill_in["logo"] = "" - if "font" in options: - font = options["font"].lower() - fill_in["font_package"] = font_to_package[font] - fill_in["specified_font"] = "" - fill_in["sans_serif"] = font not in sans_serif_fonts and "%" or "" - else: - fill_in["specified_font"] = "%" - fill_in["sans_serif"] = "%" - fill_in["font_package"] = "" - if 'toc_depth' in options: - fill_in["have_tocdepth"] = "" - fill_in["toc_depth"] = options["toc_depth"] - else: - fill_in["have_tocdepth"] = "%" - fill_in["toc_depth"] = "" - fill_in["heading"] = options.get("heading", "") - template_file = path.join("rest.sty.template") - if not template_file.check(): - template_file = py.path.local(__file__).dirpath("rest.sty.template") - return template_file.read() % fill_in - -def process_configfile(configfile, debug=False): - old = py.path.local() - py.path.local(configfile).dirpath().chdir() - configfile = py.path.local(configfile) - path = configfile.dirpath() - configfile_dic = {} - py.std.sys.path.insert(0, str(path)) - py.builtin.execfile(str(configfile), configfile_dic) - pagebreak = configfile_dic.get("pagebreak", False) - rest_sources = [py.path.local(p) - for p in configfile_dic['rest_sources']] - rest = configfile.new(ext='txt') - if len(rest_sources) > 1: - assert rest not in rest_sources - content = merge_files(rest_sources, pagebreak) - if len(rest_sources) > 1: - rest.write(content) - sty = configfile.new(ext='sty') - content = create_stylesheet(configfile_dic, path) - sty.write(content) - rest_options = None - if 'rest_options' in configfile_dic: - rest_options = configfile_dic['rest_options'] - process_rest_file(rest, sty.basename, debug, rest_options) - #cleanup: - if not debug: - sty.remove() - if rest not in rest_sources: - rest.remove() - old.chdir() - -def process_rest_file(restfile, stylesheet=None, debug=False, rest_options=None): - from docutils.core import publish_cmdline - if not py.path.local.sysfind("pdflatex"): - raise SystemExit("ERROR: pdflatex not found") - old = py.path.local() - f = py.path.local(restfile) - path = f.dirpath() - path.chdir() - pdf = f.new(ext="pdf") - if pdf.check(): - pdf.remove() - tex = f.new(ext="tex").basename - options = [f, "--input-encoding=latin-1", "--graphicx-option=auto", - "--traceback"] - if stylesheet is not None: - sty = path.join(stylesheet) - if sty.check(): - options.append('--stylesheet=%s' % (sty.relto(f.dirpath()), )) - options.append(f.new(basename=tex)) - options = map(str, options) - if rest_options is not None: - options.extend(rest_options) - publish_cmdline(writer_name='latex', argv=options) - i = 0 - while i < 10: # there should never be as many as five reruns, but to be sure - try: - latexoutput = py.process.cmdexec('pdflatex "%s"' % (tex, )) - except ExecutionFailed: - e = py.std.sys.exc_info()[1] - print("ERROR: pdflatex execution failed") - print("pdflatex stdout:") - print(e.out) - print("pdflatex stderr:") - print(e.err) - raise SystemExit - if debug: - print(latexoutput) - if py.std.re.search("LaTeX Warning:.*Rerun", latexoutput) is None: - break - i += 1 - - old.chdir() - #cleanup: - if not debug: - for ext in "log aux tex out".split(): - p = pdf.new(ext=ext) - p.remove() diff --git a/_py/rest/rest.sty.template b/_py/rest/rest.sty.template deleted file mode 100644 index 6275bf04d..000000000 --- a/_py/rest/rest.sty.template +++ /dev/null @@ -1,26 +0,0 @@ -\usepackage{fancyhdr} -\usepackage{lastpage} -\pagestyle{fancy} -\usepackage[pdftex]{graphicx} - -%(have_tocdepth)s\setcounter{tocdepth}{%(toc_depth)s} - -%(sans_serif)s\renewcommand{\familydefault}{\sfdefault} -%(specified_font)s\usepackage{%(font_package)s} -\lhead{ -\begin{tabular}{l} -\textbf{\Large %(heading)s}\tabularnewline -\thepage\ of \pageref{LastPage}, \today -\tabularnewline -\tabularnewline -\end{tabular} -} -\rhead{ -%(have_logo)s\includegraphics[height=4\baselineskip]{%(logo)s} -} -\cfoot{} -\addtolength{\headheight}{3\baselineskip} -\addtolength{\headheight}{0.61pt} -\setlength\parskip{\medskipamount} -\setlength\parindent{0pt} - diff --git a/_py/rest/resthtml.py b/_py/rest/resthtml.py deleted file mode 100644 index b6b68a0a6..000000000 --- a/_py/rest/resthtml.py +++ /dev/null @@ -1,87 +0,0 @@ -import py -import sys, os, traceback -import re - -if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()): - def log(msg): - print(msg) -else: - def log(msg): - pass - -def convert_rest_html(source, source_path, stylesheet=None, encoding='latin1'): - from _py.rest import directive - """ return html latin1-encoded document for the given input. - source a ReST-string - sourcepath where to look for includes (basically) - stylesheet path (to be used if any) - """ - from docutils.core import publish_string - directive.set_backend_and_register_directives("html") - kwargs = { - 'stylesheet' : stylesheet, - 'stylesheet_path': None, - 'traceback' : 1, - 'embed_stylesheet': 0, - 'output_encoding' : encoding, - #'halt' : 0, # 'info', - 'halt_level' : 2, - } - # docutils uses os.getcwd() :-( - source_path = os.path.abspath(str(source_path)) - prevdir = os.getcwd() - try: - #os.chdir(os.path.dirname(source_path)) - return publish_string(source, source_path, writer_name='html', - settings_overrides=kwargs) - finally: - os.chdir(prevdir) - -def process(txtpath, encoding='latin1'): - """ process a textfile """ - log("processing %s" % txtpath) - assert txtpath.check(ext='.txt') - if isinstance(txtpath, py.path.svnwc): - txtpath = txtpath.localpath - htmlpath = txtpath.new(ext='.html') - #svninfopath = txtpath.localpath.new(ext='.svninfo') - - style = txtpath.dirpath('style.css') - if style.check(): - stylesheet = style.basename - else: - stylesheet = None - content = unicode(txtpath.read(), encoding) - doc = convert_rest_html(content, txtpath, stylesheet=stylesheet, encoding=encoding) - htmlpath.open('wb').write(doc) - #log("wrote %r" % htmlpath) - #if txtpath.check(svnwc=1, versioned=1): - # info = txtpath.info() - # svninfopath.dump(info) - -if sys.version_info > (3, 0): - def _uni(s): return s -else: - def _uni(s): - return unicode(s) - -rex1 = re.compile(r'.*
(.*).*', re.MULTILINE | re.DOTALL) -rex2 = re.compile(r'.*