Пример #1
0
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')
Пример #2
0
 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)