File: tex.py

package info (click to toggle)
python-pweave 0.30.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,068 kB
  • sloc: python: 30,281; makefile: 167
file content (116 lines) | stat: -rw-r--r-- 4,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from .base import PwebFormatter
from nbconvert import filters

class PwebTexFormatter(PwebFormatter):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.mimetypes = ["text/latex"]
        self.fig_mimetypes = ["application/pdf", "image/png", "image/jpg"]
        self.file_ext = "tex"

    def initformat(self):
        self.formatdict = dict(codestart='\\begin{verbatim}',
                               codeend='\end{verbatim}\n',
                               outputstart='\\begin{verbatim}',
                               outputend='\end{verbatim}\n',
                               figfmt='.pdf',
                               width='\\linewidth',
                               doctype='tex')

    def formatfigure(self, chunk):
        fignames = chunk['figure']
        caption = chunk['caption']
        width = chunk['width']
        result = ""
        figstring = ""

        if chunk["f_env"] is not None:
            result += "\\begin{%s}\n" % chunk["f_env"]

        for fig in fignames:
            figstring += ("\\includegraphics[width= %s]{%s}\n" % (width, fig))

        # Figure environment
        if chunk['caption']:
            result += ("\\begin{figure}[%s]\n"
                       "\\center\n"
                       "%s"
                       "\\caption{%s}\n" % (chunk['f_pos'], figstring, caption))
            if 'name' in chunk:
                result += "\label{fig:%s}\n" % chunk['name']
            result += "\\end{figure}\n"

        else:
            result += figstring

        if chunk["f_env"] is not None:
            result += "\\end{%s}\n" % chunk["f_env"]

        return result


class PwebMintedFormatter(PwebTexFormatter):
    def initformat(self):
        self.formatdict = dict(
            codestart=r'\begin{minted}[mathescape, fontsize=\small, xleftmargin=0.5em]{%s}',
            codeend='\end{minted}\n',
            outputstart=r'\begin{minted}[fontsize=\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}',
            outputend='\end{minted}\n',
            termstart=r'\begin{minted}[fontsize=\footnotesize, xleftmargin=0.5em, mathescape]{%s}',
            termend='\end{minted}\n',
            figfmt='.pdf',
            extension='tex',
            width='\\linewidth',
            doctype='tex')
        self.file_ext = "tex"
        self.fig_mimetypes = ["application/pdf", "image/png", "image/jpg"]


class PwebTexPygmentsFormatter(PwebTexFormatter):
    def initformat(self):

        self.formatdict = dict(
            codestart="",
            codeend="",
            outputstart = "\n" + r"\begin{Verbatim}[commandchars=\\\{\},frame=leftline,fontsize=\small, xleftmargin=0.5em]",
            outputend = r"\end{Verbatim}" +"\n",
            #termstart= "\n" + r"\begin{Verbatim}[commandchars=\\\{\},frame=single,fontsize=\small, xleftmargin=0.5em]",
            #termend= r"\end{Verbatim}" +"\n",
            figfmt='.pdf',
            extension='tex',
            width='\\linewidth',
            doctype='tex')
        self.file_ext = "tex"
        self.fig_mimetypes = ["application/pdf", "image/png", "image/jpg"]

    def highlight_ansi_and_escape(self, text):
        return filters.ansi2latex(text)

    def format_codechunks(self, chunk):
        from pygments import highlight
        from IPython.lib.lexers import IPyLexer
        #from pygments.lexers import PythonLexer, TextLexer, PythonConsoleLexer
        from pygments.formatters import LatexFormatter

        chunk['content'] = highlight(chunk['content'], IPyLexer(),
                                     LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"))
        return PwebFormatter.format_codechunks(self, chunk)


class PwebTexPweaveFormatter(PwebTexFormatter):
    """User defined formatting for chunks in header using pweavecode, pweaveoutput and pweaveterm environments"""

    def initformat(self):
        self.formatdict = dict(
            codestart=r'\begin{pweavecode}',
            codeend='\end{pweavecode}\n',
            outputstart=r'\begin{pweaveout}',
            outputend='\end{pweaveout}\n',
            termstart=r'\begin{pweaveterm}',
            termend='\end{pweaveterm}\n',
            figfmt='.pdf',
            extension='tex',
            width='\\linewidth',
            doctype='tex')
        self.file_ext = "tex"