Exemplo n.º 1
0
    def __call__(self, text_output):
        reg = r''.join([
            r'(?P<block>(?P<prefix>[:/\\A-Z\w\d\.-]*?)[/\\]--obj[/\\]',
            r'(?P<mastername>[A-Z\d\w\.-]+)\.obj)'
        ])
        chainre = re.compile(reg)

        def formatwarning(filename, line, message):
            marker = u"--->!!---> "
            warning = marker + ':'.join([filename, line, message])

            prefix = ""
            for m in chainre.finditer(filename):
                f = os.path.join(prefix, m.group("prefix"),
                                 m.group("mastername"))
                warning += '\n' + marker + f + ':0: source file'
                prefix += os.path.join(prefix, m.group("block"))
            return warning

        def formatcommonwarning(message):
            marker = u"--->##---> "
            warning = marker + message
            return warning

        outputlines = []
        for match in self.output_re.finditer(text_output):
            groups = match.groupdict()
            if groups['dummy']:
                dummy = ut.unicodeanyway(groups['dummy'])
                outputlines.append(u"" + dummy)
            else:
                if groups['file']:
                    file_ = groups['file']
                    line_ = groups['line']
                    message_ = groups['message']
                    realpath = os.path.abspath(os.path.join(self.path, file_))
                    warning = formatwarning(realpath, line_, message_)
                if groups['pyfile']:
                    realpath = groups['pyfile']
                    line_ = groups['pyline']
                    message_ = groups['pymessage']
                    warning = formatwarning(realpath, line_, message_)
                if groups['commonerror']:
                    commonerror = groups['commonerror']
                    warning = None
                    if self.suppress_warnings_re.match(commonerror):
                        outputlines.append(u"" + commonerror)
                    else:
                        warning = formatcommonwarning(commonerror)
                if warning:
                    outputlines.append(warning)
                    self.warnings.append(warning)
        return '\n'.join(outputlines)
Exemplo n.º 2
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')
Exemplo n.º 3
0
 def __call__(self, tex_output):
     outputlines = []
     for match in self.output_re.finditer(tex_output):
         groups = match.groupdict()
         if groups['dummy']:
             dummy = ut.unicodeanyway(groups['dummy'])
             outputlines.append(u"~~ " + dummy)
         else:
             realpath = os.path.abspath(
                 os.path.join(self.path, groups['file']))
             outputlines += [
                 u"--->!!---> " +
                 ':'.join([realpath, groups['line'], groups['message']])
             ]
     return '\n'.join(outputlines)