File: markdown.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 (144 lines) | stat: -rw-r--r-- 4,938 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from .base import PwebFormatter
import sys

class PwebPandocFormatter(PwebFormatter):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.file_ext = "md"
        self.mimetypes = ["text/markdown"]
        self.fig_mimetypes = ["image/png", "image/jpg", "application/svg+xml"]


    def initformat(self):
        self.formatdict = dict(codestart='```%s',
                               codeend='```\n\n',
                               outputstart='```',
                               outputend='```\n\n',
                               indent='',
                               termindent='',
                               figfmt='.png',
                               extension='md',
                               width = None,
                               doctype='pandoc')

    def make_figure_string(self, figname, width, label, caption = ""):
        figstring = "![%s](%s)" % (caption, figname)

        #Pandoc >= 1.16 supports figure width and id

        attributes = ""
        if label is not None:
            attributes += "#%s " % label
        if width is not None:
            attributes += "width=%s" % width
        if attributes != "":
            figstring += "{%s}" % attributes

        if caption == "":
            figstring += "\\"

        figstring += "\n"

        return figstring

    def formatfigure(self, chunk):

        fignames = chunk['figure']

        if chunk["caption"]:
            caption = chunk["caption"]
        else:
            caption = ""

        figstring = ""

        if chunk['caption'] and len(fignames) > 0:
            if len(fignames) > 1:
                print("INFO: Only including the first plot in a chunk when the caption is set")
            figstring = self.make_figure_string(fignames[0], chunk["width"], chunk["name"], caption)
            return figstring

        for fig in fignames:
            figstring += self.make_figure_string(fig, chunk["width"], chunk["name"])

        return figstring


class PwebLeanpubFormatter(PwebFormatter):
    def initformat(self):
        self.formatdict = dict(codestart='{line-numbers=off}\n~~~~~~~~',
                               codeend='~~~~~~~~\n\n',
                               outputstart='{line-numbers=off}\n~~~~~~~~',
                               outputend='~~~~~~~~\n\n',
                               indent='',
                               termindent='',
                               figfmt='.png',
                               extension='txt',
                               width='15 cm',
                               doctype='leanpub')
        self.file_ext = "md"
        self.mimetypes = ["text/markdown"]
        self.fig_mimetypes = ["image/png", "image/jpg", "application/svg+xml"]


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

        # print chunk["name"]

        if chunk['caption']:
            if fignames:
                result += '![%s](%s)\n' % (caption, fignames[0])
                if len(fignames) > 1:
                    for fig in fignames[1:]:
                        figstring += '![](%s)\n' % fig
                        sys.stderr.write("Warning, only the first figure gets a caption\n")
        else:
            for fig in fignames:
                figstring += '![](%s)\n' % fig
            result += figstring
        return result


class PwebSoftCoverFormatter(PwebLeanpubFormatter):
    def initformat(self):
        self.formatdict = dict(codestart='\n```python',
                               codeend='```\n\n',
                               outputstart='```\n',
                               outputend='```\n\n',
                               indent='',
                               termindent='',
                               figfmt='.png',
                               extension='md',
                               width='15cm',
                               doctype='softcover')
        self.file_ext = "md"
        self.mimetypes = ["text/markdown"]
        self.fig_mimetypes = ["image/png", "image/jpg", "application/svg+xml"]

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

        if chunk['caption']:
            if fignames:
                result += '![%s \\label{fig:%s}](%s)\n' % (caption, label, fignames[0])
                if len(fignames) > 1:
                    for fig in fignames[1:]:
                        figstring += '![](%s)\n' % fig
                        sys.stderr.write("Warning, only the first figure gets a caption\n")
        else:
            for fig in fignames:
                figstring += '![](%s)\n' % fig
            result += figstring
        return result