File: rst.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 (89 lines) | stat: -rw-r--r-- 3,445 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
from .base import PwebFormatter

class PwebRstFormatter(PwebFormatter):
    def initformat(self):
        self.formatdict = dict(codestart='.. code:: %s\n',
                               codeend='\n\n',
                               outputstart='.. code::\n',
                               outputend='\n\n',
                               # rst has specific format (doctest) for term blocks
                               termstart='.. code:: %s\n',
                               termend='\n\n',
                               termindent='    ',
                               indent='    ',
                               figfmt='.png',
                               extension='rst',
                               width='15 cm',
                               doctype='rst')
        self.fig_mimetypes = ["image/png", "image/jpg"]
        self.mimetypes = ["text/restructuredtext"]
        self.file_ext = "rst"

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

        for fig in fignames:
            figstring += ('.. image:: %s\n   :width: %s\n\n' % (fig, width))

        if chunk['caption']:
            result += (".. figure:: %s\n"
                       "   :width: %s\n\n"
                       "   %s\n\n" % (fignames[0], width, caption))
        else:
            result += figstring
        return result



    def _indent(self, text):
        """Indent blocks for formats where indent is significant"""
        if not text.startswith("\n"):
            text = "\n" + text
        return text.replace('\n', '\n' + self.formatdict['indent'])

    def _termindent(self, text):
        return text.replace('\n', '\n' + self.formatdict['termindent'])

class PwebSphinxFormatter(PwebRstFormatter):
    def initformat(self):
        self.formatdict = dict(codestart='.. code-block:: %s\n',
                               codeend='\n\n',
                               outputstart='::\n',
                               outputend='\n\n',
                               # rst has specific format (doctest) for term blocks
                               termstart='.. code-block:: %s\n',
                               termend='\n\n',
                               termindent='    ',
                               indent='    ',
                               # Sphinx determines the figure format automatically
                               # for different output formats
                               figfmt='.*',
                               savedformats=['.png', '.pdf'],
                               extension='rst',
                               width='15 cm',
                               doctype='rst')
        self.fig_mimetypes = ["image/png", "image/jpg"]
        self.mimetypes = ["text/restructuredtext"]
        self.file_ext = "rst"

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

        for fig in fignames:
            figstring += ('.. image:: %s\n   :width: %s\n\n' % (fig, width))

        if chunk['caption']:
            result += (".. figure:: %s\n" \
                       "   :width: %s\n\n" \
                       "   %s\n\n" % (fignames[0], width, caption))
        else:
            result += figstring
        return result