def extract_tex(self, target, source, env): """ Сканирование TEX-файла, в поиске зависимостей и всяких полезностей. """ deps = [] node = source[0] def relativizefile(relfile): """ return absolute path to file """ newrelfile = relfile.replace(r"\finkdir", path) newrelfile = newrelfile.replace(r"\projectpath", env.project_path) if os.path.commonprefix([newrelfile, env.project_path]) == "": newrelfile = os.path.join(path, newrelfile) return newrelfile contents = node.get_contents() path, filename = os.path.split(node.abspath) for rule in env.project_db["include_commands"]: formatrule = rule["file"] wantedext = "" if "ext" in rule: wantedext = rule["ext"] re_ = rule["re"] if formatrule == "": formatrule = r"%(relfile)s" for include in re_.finditer(contents): g = { 'projectpath': env.project_path, 'currentfilename': filename, 'currentpath': path, 'currentfile': filename, } idx = re_.groupindex.keys() for symname in idx: g[symname] = include.group(symname) relfile = formatrule % g if os.path.splitext(relfile)[1] != wantedext: relfile += wantedext relfile = relativizefile(relfile) relfile = os.path.realpath(relfile) deps.append(relfile) strdeps = [] for dep in deps: if type(dep) == type(""): strdeps.append(dep) else: for node in dep: strdeps.append(node.abspath) mystr = '\n'.join(strdeps) ut.string2file(mystr, target[0].abspath)
def extract_algorithms(ps_infile, env): """ Вытаскиваем части кода-алгоритмы """ import pygments from pygments.lexers import get_lexer_by_name from pygments.formatters import LatexFormatter algorithm_regexp = re.compile( r"(?ms)\#ALGORITHM\s+(?P<name>[a-zA-z-0-9]+)\s*(?P<code>.+?)\s*\#ENDALGORITHM") hideline_regexps = [re.compile(r"(?m)^.*\#hide *\n"), re.compile(r"(?m)\n.*\#hide *") ] ls = ut.file2string(ps_infile) for algorithm in algorithm_regexp.finditer(ls): algfilename = lib.get_target(ps_infile, algorithm.group('name')+".py") texfilename = lib.get_target(ps_infile, algorithm.group('name')+".tex") #lf = open(algfilename, 'w') code = algorithm.group('code') for r in hideline_regexps: code = re.sub(r, "", code) #code = lib.utf8anyway(code) #lf.write(code) #lf.close() #tempblock = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix()) #ls = ''.join([env.project_db['paths']['python'], # r'\scripts\pygmentize -f latex -l python ', # ' -o "%(tempblock)s" "%(algfilename)s" ' % vars() ]) #os.system(ls) lexer = get_lexer_by_name('python') code = ut.unicodeanyway(code) latex_tokens = pygments.lex(code, lexer) # sblock = ut.file2string(tempblock) # from pygments.formatters import LatexFormatter latex_formatter = LatexFormatter(texcomments = True) latex = pygments.format(latex_tokens, latex_formatter) stexblock = r""" \documentclass{minimal} \usepackage{xecyr} \XeTeXdefaultencoding "utf-8" \XeTeXinputencoding "utf-8" \defaultfontfeatures{Mapping=tex-text} \setmonofont{Consolas} \usepackage{color} \usepackage{fancyvrb} \usepackage[russian,english]{babel} """ + latex_formatter.get_style_defs() + r""" \begin{document} """ + latex + r""" \end{document} """ ut.string2file(stexblock, texfilename, encoding='utf-8')
def meta2deps(self, target, source, env): strs = [] for src in source: srcstr = src.get_contents().replace('\r', '') strs.append(srcstr.strip()) srcstrs = srcstr.split('\n') for str in srcstrs: depsfile = lib.get_target(str, self.DEPS_FILE) if os.path.exists(depsfile): depsfilestr = ut.file2string(depsfile).strip() if depsfilestr: strs.append(depsfilestr) mystr = '\n'.join(strs) ut.string2file(mystr, target[0].abspath)
def pdfbeamlatex(target, source, env): """ PDFLatex-обработка для слайдов. """ filename = str(source[0].abspath) (path, nameext) = os.path.split(filename) (name, ext) = os.path.splitext(nameext) outpdf = os.path.join(path, "--obj", name) + ".pdf" outpdfsync = os.path.join(path, "--obj", name) + ".synctex" outlog = os.path.join(path, "--obj", name) + ".log" pdfsync = os.path.join(path, name) + ".synctex" texlog = os.path.join(path, name) + ".log" curdir = os.getcwd() beamdir = path os.chdir(path) ut.createdir("--obj") command = ''.join([ r'xelatex -synctex=-1 -file-line-error-style -output-directory="--obj" ', ' -interaction nonstopmode "', nameext, '"']) print os.environ["PATH"] print command #texfilter = tx.TeXFilter(path) cutefilter = mf.CuteFilter(path) out= "Mock:" + command out = ut.get_prog_output(command) out = cutefilter(out) print out.encode("utf8") env.warnings += cutefilter.warnings ut.string2file("\n".join(env.warnings), filename + ".warnings") outbcffile = os.path.join(path, "--obj", name) + ".bcf" if os.path.exists(outbcffile): os.chdir('--obj') command = os.path.join(env.project_db['paths']['tex'], 'biber "%(outbcffile)s"' % vars()) os.system(command) os.chdir('..') mkidx.makeindex(env, filename) shutil.copyfile(outpdf, target[0].abspath) if os.path.exists(outpdfsync): shutil.copy(outpdfsync, pdfsync) if os.path.exists(outlog): shutil.copy(outlog, texlog) os.chdir(curdir)
def register_pdf(self, filename): if os.path.sep not in filename: filename = os.path.join(self.env.GetLaunchDir(), filename) pathname = os.path.splitext(os.path.abspath(filename))[0] path, name = os.path.split(pathname) target = os.path.realpath(pathname + ".pdf") source = os.path.realpath(pathname + ".tex") metafile = lib.get_target(source, self.META_FILE) depsfile = lib.get_target(source, self.DEPS_FILE) cmd = self.env.Command(depsfile, metafile, self.meta2deps) cmd = self.env.Command(metafile, source, self.extract_meta) cmd = self.env.Command(target, [source, depsfile], actions.pdfbeamlatex) auxfile = os.path.join(path, '--obj', name + '.aux') if not os.path.exists(auxfile): ut.createdir(os.path.split(auxfile)[0]) ut.string2file("", auxfile) self.env.Depends(cmd, auxfile) self.env.Precious(target)
def print_statements(self, statements, frametitle, prefix="", graph="digraph", ext="dot", reset=True, beforelaststatement=""): graph_body = "" graph_str = "" frame = 0 if reset: self.reset_tex() for s in statements: if s.strip().startswith("\\"): self.print_tex(s.encode("windows-utf8")) else: frame += 1 if frame == len(statements): graph_body += beforelaststatement graph_body_print = graph_body + beforelaststatement + s graph_body += s graph_str = """ %(graph)s G{ %(graph_body_print)s } """ % vars() graphname = self.dotprefix + prefix + '-%02d.%s' % (frame, ext) ut.string2file(graph_str, graphname) tex_str = ur""" \begin{frame} \frametitle{%(frametitle)s} \begin{center} \localPDF[height=0.9\paperheight, width=.95\paperwidth, keepaspectratio]{--obj/dot%(prefix)s-%(frame)02d.%(ext)s.obj/--obj/obj.svg.obj/--obj/obj} \end{center} \end{frame} """ % vars() self.print_tex(tex_str.encode("utf-8")) graphname = self.dotprefix + '-last.%s' % ext ut.string2file(graph_str, graphname)
def dummy(self, target, source, env): ut.string2file("", target[0].abspath)
def print_lines_to_tex(self): tex = "\n".join(self.lines) texdoc = self.tex_template % {'tex': tex} filename = self.texprefix + ".tex" ut.string2file(texdoc, filename)