コード例 #1
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(ur'\begin{Verbatim}[commandchars=\\\{\}')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(u',numbers=left' +
                          (start and u',firstnumber=%d' % start or u'') +
                          (step and u',stepnumber=%d' % step or u''))
        if self.mathescape or self.texcomments:
            outfile.write(ur',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
        if self.verboptions:
            outfile.write(u',' + self.verboptions)
        outfile.write(u']\n')

        for ttype, value in tokensource:
            if ttype in Token.Comment:
                if self.texcomments:
                    # Try to guess comment starting lexeme and escape it ...
                    start = value[0:1]
                    for i in xrange(1, len(value)):
                        if start[0] != value[i]:
                            break
                        start += value[i]

                    value = value[len(start):]
                    start = escape_tex(start, self.commandprefix)

                    # ... but do not escape inside comment.
                    value = start + value
                elif self.mathescape:
                    # Only escape parts not inside a math environment.
                    parts = value.split('$')
                    in_math = False
                    for i, part in enumerate(parts):
                        if not in_math:
                            parts[i] = escape_tex(part, self.commandprefix)
                        in_math = not in_math
                    value = '$'.join(parts)
                else:
                    value = escape_tex(value, self.commandprefix)
            else:
                value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write(u'\\end{Verbatim}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                dict(docclass  = self.docclass,
                     preamble  = self.preamble,
                     title     = self.title,
                     encoding  = self.encoding or 'latin1',
                     styledefs = self.get_style_defs(),
                     code      = outfile.getvalue()))
コード例 #2
0
ファイル: latex.py プロジェクト: abevoelker/pygments.rb
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(',numbers=left' +
                          (start and ',firstnumber=%d' % start or '') +
                          (step and ',stepnumber=%d' % step or ''))
        if self.mathescape or self.texcomments:
            outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
        if self.verboptions:
            outfile.write(',' + self.verboptions)
        outfile.write(']\n')

        for ttype, value in tokensource:
            if ttype in Token.Comment:
                if self.texcomments:
                    # Try to guess comment starting lexeme and escape it ...
                    start = value[0:1]
                    for i in xrange(1, len(value)):
                        if start[0] != value[i]:
                            break
                        start += value[i]

                    value = value[len(start):]
                    start = escape_tex(start, self.commandprefix)

                    # ... but do not escape inside comment.
                    value = start + value
                elif self.mathescape:
                    # Only escape parts not inside a math environment.
                    parts = value.split('$')
                    in_math = False
                    for i, part in enumerate(parts):
                        if not in_math:
                            parts[i] = escape_tex(part, self.commandprefix)
                        in_math = not in_math
                    value = '$'.join(parts)
                else:
                    value = escape_tex(value, self.commandprefix)
            else:
                value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write('\\end{Verbatim}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                dict(docclass  = self.docclass,
                     preamble  = self.preamble,
                     title     = self.title,
                     encoding  = self.encoding or 'latin1',
                     styledefs = self.get_style_defs(),
                     code      = outfile.getvalue()))
コード例 #3
0
ファイル: html.py プロジェクト: Jenyay/outwiker
    def _wrap_tablelinenos(self, inner):
        dummyoutfile = StringIO()
        lncount = 0
        for t, line in inner:
            if t:
                lncount += 1
            dummyoutfile.write(line)

        fl = self.linenostart
        mw = len(str(lncount + fl - 1))
        sp = self.linenospecial
        st = self.linenostep
        la = self.lineanchors
        aln = self.anchorlinenos
        nocls = self.noclasses
        if sp:
            lines = []

            for i in range(fl, fl+lncount):
                if i % st == 0:
                    if i % sp == 0:
                        if aln:
                            lines.append('<a href="#%s-%d" class="special">%*d</a>' %
                                         (la, i, mw, i))
                        else:
                            lines.append('<span class="special">%*d</span>' % (mw, i))
                    else:
                        if aln:
                            lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
                        else:
                            lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)
        else:
            lines = []
            for i in range(fl, fl+lncount):
                if i % st == 0:
                    if aln:
                        lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
                    else:
                        lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)

        # in case you wonder about the seemingly redundant <div> here: since the
        # content in the other cell also is wrapped in a div, some browsers in
        # some configurations seem to mess up the formatting...
        if nocls:
            yield 0, ('<table class="%stable">' % self.cssclass +
                      '<tr><td><div class="linenodiv" '
                      'style="background-color: #f0f0f0; padding-right: 10px">'
                      '<pre style="line-height: 125%">' +
                      ls + '</pre></div></td><td class="code">')
        else:
            yield 0, ('<table class="%stable">' % self.cssclass +
                      '<tr><td class="linenos"><div class="linenodiv"><pre>' +
                      ls + '</pre></div></td><td class="code">')
        yield 0, dummyoutfile.getvalue()
        yield 0, '</td></tr></table>'
コード例 #4
0
ファイル: html.py プロジェクト: 0x008800/Sandbox
    def _wrap_tablelinenos(self, inner):
        dummyoutfile = StringIO()
        lncount = 0
        for t, line in inner:
            if t:
                lncount += 1
            dummyoutfile.write(line)

        fl = self.linenostart
        mw = len(str(lncount + fl - 1))
        sp = self.linenospecial
        st = self.linenostep
        la = self.lineanchors
        aln = self.anchorlinenos
        nocls = self.noclasses
        if sp:
            lines = []

            for i in range(fl, fl + lncount):
                if i % st == 0:
                    if i % sp == 0:
                        if aln:
                            lines.append(
                                '<a href="#%s-%d" class="special">%*d</a>' %
                                (la, i, mw, i))
                        else:
                            lines.append('<span class="special">%*d</span>' %
                                         (mw, i))
                    else:
                        if aln:
                            lines.append('<a href="#%s-%d">%*d</a>' %
                                         (la, i, mw, i))
                        else:
                            lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)
        else:
            lines = []
            for i in range(fl, fl + lncount):
                if i % st == 0:
                    if aln:
                        lines.append('<a href="#%s-%d">%*d</a>' %
                                     (la, i, mw, i))
                    else:
                        lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)

        # in case you wonder about the seemingly redundant <div> here: since the
        # content in the other cell also is wrapped in a div, some browsers in
        # some configurations seem to mess up the formatting...
        if nocls:
            yield 0, ('<table class="%stable">' % self.cssclass +
                      '<tr><td><div class="linenodiv" '
                      'style="background-color: #f0f0f0; padding-right: 10px">'
                      '<pre style="line-height: 125%">' + ls +
                      '</pre></div></td><td class="code">')
        else:
            yield 0, ('<table class="%stable">' % self.cssclass +
                      '<tr><td class="linenos"><div class="linenodiv"><pre>' +
                      ls + '</pre></div></td><td class="code">')
        yield 0, dummyoutfile.getvalue()
        yield 0, '</td></tr></table>'
コード例 #5
0
ファイル: latexlisting.py プロジェクト: hmphu/2be-extras
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(r'\begin{lstlisting}[language=,breaklines=true,escapeinside={(*@}{@*)},tabsize=4,framesep=0pt,xleftmargin=\FrameSep,xrightmargin=\FrameSep,frame=none,backgroundcolor=,fillcolor=')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(',numbers=left' +
                          (start and ',firstnumber=%d' % start or '') +
                          (step and ',stepnumber=%d' % step or ''))
        if self.mathescape:
            outfile.write(r',mathescape=true')
        if self.texcomments:
            outfile.write(r',texcl=true')
        # Switching to lstlisting broke these.
        if self.verboptions:
            outfile.write(',' + self.verboptions)
        outfile.write(']\n')

        for ttype, value in tokensource:
#            if ttype in Token.Comment:
#                if self.texcomments:
#                    # Try to guess comment starting lexeme and escape it ...
#                    start = value[0:1]
#                    for i in xrange(1, len(value)):
#                        if start[0] != value[i]:
#                            break
#                        start += value[i]
#
#                    value = value[len(start):]
#                    start = escape_tex(start, self.commandprefix)
#
#                    # ... but do not escape inside comment.
#                    value = start + value
#                elif self.mathescape:
#                    # Only escape parts not inside a math environment.
#                    parts = value.split('$')
#                    in_math = False
#                    for i, part in enumerate(parts):
#                        if not in_math:
#                            parts[i] = escape_tex(part, self.commandprefix)
#                        in_math = not in_math
#                    value = '$'.join(parts)
#                else:
#                    value = escape_tex(value, self.commandprefix)
#            else:
#                value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("(*@\\%s{%s}{%s}@*)" % (cp, styleval, escape_tex(line)))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("(*@\\%s{%s}{%s}@*)" % (cp, styleval, escape_tex(spl[-1])))
            else:
                outfile.write(value)

        outfile.write('\\end{lstlisting}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                dict(docclass  = self.docclass,
                     preamble  = self.preamble,
                     title     = self.title,
                     encoding  = self.encoding or 'utf-8',
                     styledefs = self.get_style_defs(),
                     code      = outfile.getvalue()))
コード例 #6
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(u'{\\tt')

        for ttype, value in tokensource:
            if ttype in Token.Comment:
                if self.texcomments:
                    # Try to guess comment starting lexeme and escape it ...
                    start = value[0:1]
                    for i in xrange(1, len(value)):
                        if start[0] != value[i]:
                            break
                        start += value[i]

                    value = value[len(start):]
                    start = escape_tex(start, cp)

                    # ... but do not escape inside comment.
                    value = start + value
                elif self.mathescape:
                    # Only escape parts not inside a math environment.
                    parts = value.split('$')
                    in_math = False
                    for i, part in enumerate(parts):
                        if not in_math:
                            parts[i] = escape_tex(part, cp)
                        in_math = not in_math
                    value = '$'.join(parts)
                elif self.escapeinside:
                    text = value
                    value = ''
                    while text:
                        a, sep1, text = text.partition(self.left)
                        if sep1:
                            b, sep2, text = text.partition(self.right)
                            if sep2:
                                value += escape_tex(a, cp) + b
                            else:
                                value += escape_tex(a + sep1 + b, cp)
                        else:
                            value += escape_tex(a, cp)
                else:
                    value = escape_tex(value, cp)
            elif ttype not in Token.Escape:
                value = escape_tex(value, cp)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = list(reversed(styles))[0]
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("\\%s%s{%s}" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("\\%s%s{%s}" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write(u'}\n')

        if self.full:
            encoding = self.encoding or 'utf8'
            # map known existings encodings from LaTeX distribution
            encoding = {
                'utf_8': 'utf8',
                'latin_1': 'latin1',
                'iso_8859_1': 'latin1',
            }.get(encoding.replace('-', '_'), encoding)
            realoutfile.write(DOC_TEMPLATE %
                dict(docclass  = self.docclass,
                     preamble  = self.preamble,
                     title     = self.title,
                     encoding  = encoding,
                     styledefs = self.get_style_defs(),
                     code      = outfile.getvalue()))
コード例 #7
0
    def _wrap_tablelinenos(self, inner):
        dummyoutfile = StringIO()
        lncount = 0
        for t, line in inner:
            if t:
                lncount += 1
                dummyoutfile.write(line)

        fl = self.linenostart
        mw = len(str(lncount + fl - 1))
        sp = self.linenospecial
        st = self.linenostep
        la = self.lineanchors
        aln = self.anchorlinenos
        nocls = self.noclasses
        if sp:
            lines = []

            for i in range(fl, fl+lncount):
                if i % st == 0:
                    if i % sp == 0:
                        if aln:
                            lines.append('<a href="#%s-%d" class="special">%*d</a>' %
                                         (la, i, mw, i))
                        else:
                            lines.append('<span class="special">%*d</span>' % (mw, i))
                    else:
                        if aln:
                            lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
                        else:
                            lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)
        else:
            lines = []
            for i in range(fl, fl+lncount):
                if i % st == 0:
                    if aln:
                        lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
                    else:
                        lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)

        yield 0, ('<table class="%stable" data-filename="%s">' % (self.cssclass, self.filename))
        lineno = fl
        for lineno_html, code in itertools.izip(ls.split('\n'), dummyoutfile.getvalue().split('\n')):
            if nocls:
                yield 0, ('<tr><td><pre style="line-height: 125%">' +
                          lineno_html + '</pre></td><td class="code">' +
                          self._wrap_code_line(code) + '</td></tr>')
            else:
                yield 0, ('<tr><td class="linenos"><pre>' +
                          lineno_html + '</pre></td><td class="code">' +
                          self._wrap_code_line(code) + '</td></tr>')
            if lineno in self.comments:
                yield 0, self.render_diff_comment(lineno, self.comments[lineno])

            lineno += 1
        yield 0, '</table>'
コード例 #8
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(u'\\begin{' + self.envname +
                      u'}[commandchars=\\\\\\{\\}')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(u',numbers=left' +
                          (start and u',firstnumber=%d' % start or u'') +
                          (step and u',stepnumber=%d' % step or u''))
        if self.mathescape or self.texcomments or self.escapeinside:
            outfile.write(
                u',codes={\\catcode`\\$=3\\catcode`\\^=7\\catcode`\\_=8}')
        if self.verboptions:
            outfile.write(u',' + self.verboptions)
        outfile.write(u']\n')

        for ttype, value in tokensource:
            if ttype in Token.Comment:
                if self.texcomments:
                    # Try to guess comment starting lexeme and escape it ...
                    start = value[0:1]
                    for i in xrange(1, len(value)):
                        if start[0] != value[i]:
                            break
                        start += value[i]

                    value = value[len(start):]
                    start = escape_tex(start, cp)

                    # ... but do not escape inside comment.
                    value = start + value
                elif self.mathescape:
                    # Only escape parts not inside a math environment.
                    parts = value.split('$')
                    in_math = False
                    for i, part in enumerate(parts):
                        if not in_math:
                            parts[i] = escape_tex(part, cp)
                        in_math = not in_math
                    value = '$'.join(parts)
                elif self.escapeinside:
                    text = value
                    value = ''
                    while text:
                        a, sep1, text = text.partition(self.left)
                        if sep1:
                            b, sep2, text = text.partition(self.right)
                            if sep2:
                                value += escape_tex(a, cp) + b
                            else:
                                value += escape_tex(a + sep1 + b, cp)
                        else:
                            value += escape_tex(a, cp)
                else:
                    value = escape_tex(value, cp)
            elif ttype not in Token.Escape:
                value = escape_tex(value, cp)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write(u'\\end{' + self.envname + u'}\n')

        if self.full:
            encoding = self.encoding or 'utf8'
            # map known existings encodings from LaTeX distribution
            encoding = {
                'utf_8': 'utf8',
                'latin_1': 'latin1',
                'iso_8859_1': 'latin1',
            }.get(encoding.replace('-', '_'), encoding)
            realoutfile.write(DOC_TEMPLATE %
                              dict(docclass=self.docclass,
                                   preamble=self.preamble,
                                   title=self.title,
                                   encoding=encoding,
                                   styledefs=self.get_style_defs(),
                                   code=outfile.getvalue()))
コード例 #9
0
ファイル: code.py プロジェクト: yuyier/BlogBackendProject
    def _wrap_tablelinenos(self, inner):
        dummyoutfile = StringIO()
        lncount = 0
        for t, line in inner:
            if t:
                lncount += 1
            dummyoutfile.write(line)

        fl = self.linenostart  # 行号起始
        mw = len(str(lncount + fl - 1))  # 最大行号宽度
        sp = self.linenospecial  # 特殊行间隔数
        st = self.linenostep  # 行号间隔数
        la = self.lineanchors  # 行锚点名称
        aln = self.anchorlinenos  # 如果设置为True,会给行号添加一个a标签,和`linenos` and `lineanchors`联合使用
        nocls = self.noclasses  # 是否是无class模式
        if sp:
            lines = []

            for i in range(fl, fl + lncount):
                if i % st == 0:
                    if i % sp == 0:
                        # 特殊行号
                        if aln:
                            # 锚点
                            lines.append(
                                '<a href="#%s-%d" class="special">%*d</a>' %
                                (la, i, mw, i))
                        else:
                            lines.append('<span class="special">%*d</span>' %
                                         (mw, i))
                    else:
                        if aln:
                            # 锚点
                            lines.append('<a href="#%s-%d">%*d</a>' %
                                         (la, i, mw, i))
                        else:
                            lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)
        else:
            lines = []
            for i in range(fl, fl + lncount):
                if i % st == 0:
                    if aln:
                        lines.append('<a href="#%s-%d">%*d</a>' %
                                     (la, i, mw, i))
                    else:
                        lines.append('%*d' % (mw, i))
                else:
                    lines.append('')
            ls = '\n'.join(lines)

        # in case you wonder about the seemingly redundant <div> here: since the
        # content in the other cell also is wrapped in a div, some browsers in
        # some configurations seem to mess up the formatting...

        style = []
        if nocls:
            style.append('background-color: #f0f0f0')
            style.append('padding-right: 10px')

        clazz = []
        if self.cssclass:
            clazz.append('%stable' % self.cssclass)

        # 自动换行
        if self.linefeed:
            if not self.nocls:
                clazz.append('linefeed')

        clazz = ' '.join(clazz)
        style = '; '.join(style)

        if nocls:
            yield 0, ('<table class="%s">' % clazz +
                      '<tr><td><div class="linenodiv" '
                      'style="%s">' + style +
                      '<pre style="line-height: 125%">' + ls +
                      '</pre></div></td><td class="code">')
        else:
            yield 0, ('<table class="%s">' % clazz +
                      '<tr><td class="linenos"><div class="linenodiv"><pre>' +
                      ls + '</pre></div></td><td class="code">')
        yield 0, dummyoutfile.getvalue()
        yield 0, '</td></tr></table>'
コード例 #10
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(r'\begin{Verbatim}[commandchars=@\[\]')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(',numbers=left' +
                          (start and ',firstnumber=%d' % start or '') +
                          (step and ',stepnumber=%d' % step or ''))
        if self.verboptions:
            outfile.write(',' + self.verboptions)
        outfile.write(']\n')

        for ttype, value in tokensource:
            value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("@%s[%s][%s]" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("@%s[%s][%s]" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write('\\end{Verbatim}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                              dict(docclass=self.docclass,
                                   preamble=self.preamble,
                                   title=self.title,
                                   encoding=self.encoding or 'latin1',
                                   styledefs=self.get_style_defs(),
                                   code=outfile.getvalue()))
コード例 #11
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(
            r'\begin{lstlisting}[language=,breaklines=true,escapeinside={(*@}{@*)},tabsize=4,framesep=0pt,xleftmargin=\FrameSep,xrightmargin=\FrameSep,frame=none,backgroundcolor=,fillcolor='
        )
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(',numbers=left' +
                          (start and ',firstnumber=%d' % start or '') +
                          (step and ',stepnumber=%d' % step or ''))
        if self.mathescape:
            outfile.write(r',mathescape=true')
        if self.texcomments:
            outfile.write(r',texcl=true')
        # Switching to lstlisting broke these.
        if self.verboptions:
            outfile.write(',' + self.verboptions)
        outfile.write(']\n')

        for ttype, value in tokensource:
            #            if ttype in Token.Comment:
            #                if self.texcomments:
            #                    # Try to guess comment starting lexeme and escape it ...
            #                    start = value[0:1]
            #                    for i in xrange(1, len(value)):
            #                        if start[0] != value[i]:
            #                            break
            #                        start += value[i]
            #
            #                    value = value[len(start):]
            #                    start = escape_tex(start, self.commandprefix)
            #
            #                    # ... but do not escape inside comment.
            #                    value = start + value
            #                elif self.mathescape:
            #                    # Only escape parts not inside a math environment.
            #                    parts = value.split('$')
            #                    in_math = False
            #                    for i, part in enumerate(parts):
            #                        if not in_math:
            #                            parts[i] = escape_tex(part, self.commandprefix)
            #                        in_math = not in_math
            #                    value = '$'.join(parts)
            #                else:
            #                    value = escape_tex(value, self.commandprefix)
            #            else:
            #                value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("(*@\\%s{%s}{%s}@*)" %
                                      (cp, styleval, escape_tex(line)))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("(*@\\%s{%s}{%s}@*)" %
                                  (cp, styleval, escape_tex(spl[-1])))
            else:
                outfile.write(value)

        outfile.write('\\end{lstlisting}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                              dict(docclass=self.docclass,
                                   preamble=self.preamble,
                                   title=self.title,
                                   encoding=self.encoding or 'utf-8',
                                   styledefs=self.get_style_defs(),
                                   code=outfile.getvalue()))
コード例 #12
0
ファイル: latex.py プロジェクト: Arachnid/bloggart
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        outfile.write(r'\begin{Verbatim}[commandchars=@\[\]')
        if self.linenos:
            start, step = self.linenostart, self.linenostep
            outfile.write(',numbers=left' +
                          (start and ',firstnumber=%d' % start or '') +
                          (step and ',stepnumber=%d' % step or ''))
        if self.verboptions:
            outfile.write(',' + self.verboptions)
        outfile.write(']\n')

        for ttype, value in tokensource:
            value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("@%s[%s][%s]" % (cp, styleval, line))
                    outfile.write('\n')
                if spl[-1]:
                    outfile.write("@%s[%s][%s]" % (cp, styleval, spl[-1]))
            else:
                outfile.write(value)

        outfile.write('\\end{Verbatim}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                dict(docclass  = self.docclass,
                     preamble  = self.preamble,
                     title     = self.title,
                     encoding  = self.encoding or 'latin1',
                     styledefs = self.get_style_defs(),
                     code      = outfile.getvalue()))
コード例 #13
0
    def format_unencoded(self, tokensource, outfile):
        # TODO: add support for background colors
        t2n = self.ttype2name
        cp = self.commandprefix

        if self.full:
            realoutfile = outfile
            outfile = StringIO()

        # TB: Added support for "inline" feature
        if not self.inline:
            # TB: Added support for custom Verbatim environment definition.
            # outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
            outfile.write(r'\begin{' + self.verbenvironment +
                          r'}[commandchars=\\\{\}')

            if self.linenos:
                start, step = self.linenostart, self.linenostep
                outfile.write(',numbers=left' +
                              (start and ',firstnumber=%d' % start or '') +
                              (step and ',stepnumber=%d' % step or ''))
            if self.mathescape or self.texcomments:
                outfile.write(
                    r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
            if self.verboptions:
                outfile.write(',' + self.verboptions)
            outfile.write(']')
            if self.saveverbatimname != "":
                outfile.write('{' + self.saveverbatimname + '}')
            outfile.write('\n')

        # TB: Added functionality to skip formatting content between
        # Comment.Invisible.Begin and Comment.Invisible.End tokens.
        # If self.hidebydefault, then assume the file begins with a
        # Comment.Invisible.Begin token.
        skiptoken = self.hidebydefault
        # TB: Added functionality to reindent the output such that the
        # first line has no indentation.  The way this works is that
        # we measure the whitespace prefix on the first line we output
        # and store it in 'initialindent'.  On all subsequent lines,
        # we try to find 'initialindent' at the start of the line and
        # remove it.  If we can't find 'initialindent' at the start of
        # the line, we simply output the line as is.
        reindent = self.reindent
        initialindent = ''
        find_next_indent = self.reindent
        newline = False
        # TB: Boolean flag to track whether or not we've produced any
        # lines of output.  The Verbatim environment gets unhappy if
        # it has no contents, so if we don't output anything else,
        # output a newline.
        wrotelines = False

        for ttype, value in tokensource:
            if ttype in Token.Comment.Invisible.End:
                skiptoken = False
                continue
            if ttype in Token.Comment.Invisible.Begin:
                skiptoken = True
                continue
            if skiptoken:
                continue

            if reindent:
                # Get the indentation of the first line
                if find_next_indent:
                    if ttype is Token.Text:
                        indent_match = re.match(r'^[ \t\f\v]+',
                                                value)  # Ignore newlines
                        if indent_match is not None:
                            initialindent = indent_match.group(0)
                            value = value[indent_match.end():]
                    find_next_indent = False
                    newline = False

                # Insert any newlines at beginning of value
                newline_match = re.match(r'^\n+', value)
                if newline_match is not None:
                    outfile.write(newline_match.group(0))
                    newline = True
                    value = value[newline_match.end():]

                # If we're on a new line, remove indentation
                elif newline:
                    if ttype is Token.Text:
                        indent_match = re.match('^(' + initialindent + ')',
                                                value)
                        if indent_match is not None:
                            value = value[indent_match.end():]
                    # Indentation does not contain newlines, so we're not on a new line anymore
                    newline = False

            if ttype in Token.Comment and ttype not in Token.Comment.PureTeX:

                # TB: These two lines to remove invisible comment
                # characters.
                if ttype in Token.Comment.Invisible:
                    continue

                elif self.texcomments:
                    # # TB: The following code formats "Pure TeX"
                    # # comments correctly.
                    # if ttype not in Token.Comment.PureTeX:

                    #     # Try to guess comment starting lexeme and escape it ...
                    #     start = value[0:1]
                    #     for i in xrange(1, len(value)):
                    #         if start[0] != value[i]:
                    #             break
                    #         start += value[i]

                    #     value = value[len(start):]
                    #     start = escape_tex(start, self.commandprefix)

                    #     # ... but do not escape inside comment.
                    #     value = start + value

                    # Try to guess comment starting lexeme and escape it ...
                    start = value[0:1]
                    for i in xrange(1, len(value)):
                        if start[0] != value[i]:
                            break
                        start += value[i]

                    value = value[len(start):]
                    start = escape_tex(start, self.commandprefix)

                    # ... but do not escape inside comment.
                    value = start + value
                elif self.mathescape:
                    # Only escape parts not inside a math environment.
                    parts = value.split('$')
                    in_math = False
                    for i, part in enumerate(parts):
                        if not in_math:
                            parts[i] = escape_tex(part, self.commandprefix)
                        in_math = not in_math
                    value = '$'.join(parts)
                else:
                    value = escape_tex(value, self.commandprefix)
            elif ttype not in Token.Comment.PureTeX:
                value = escape_tex(value, self.commandprefix)
            styles = []
            while ttype is not Token:
                try:
                    styles.append(t2n[ttype])
                except KeyError:
                    # not in current style
                    styles.append(_get_ttype_name(ttype))
                ttype = ttype.parent
            styleval = '+'.join(reversed(styles))
            if styleval:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
                    newline = True
                    outfile.write('\n')
                    wrotelines = True
                if spl[-1]:
                    outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
                    wrotelines = True

            else:
                spl = value.split('\n')
                for line in spl[:-1]:
                    if line:
                        # Attempt to remove indentation
                        if reindent and newline:
                            indent_match = re.match('^(' + initialindent + ')',
                                                    line)
                            if indent_match is not None:
                                line = line[indent_match.end():]
                        outfile.write(line)
                    newline = True
                    outfile.write('\n')
                    wrotelines = True
                if spl[-1]:
                    line = spl[-1]
                    # Attempt to remove indentation
                    if reindent and newline:
                        indent_match = re.match('^(' + initialindent + ')',
                                                line)
                        if indent_match is not None:
                            line = line[indent_match.end():]
                    outfile.write(line)
                    wrotelines = True
                    newline = False

        if not self.inline:
            if not wrotelines:
                outfile.write('\n')
            # TB: Adding support for custom Verbatim environments
            # outfile.write('\\end{Verbatim}\n')
            outfile.write('\\end{' + self.verbenvironment + '}\n')

        if self.full:
            realoutfile.write(DOC_TEMPLATE %
                              dict(docclass=self.docclass,
                                   preamble=self.preamble,
                                   title=self.title,
                                   encoding=self.encoding or 'latin1',
                                   styledefs=self.get_style_defs(),
                                   code=outfile.getvalue()))