File: subfig.py

package info (click to toggle)
plastex 3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,132 kB
  • sloc: python: 23,341; xml: 18,076; javascript: 7,755; ansic: 46; makefile: 40; sh: 26
file content (114 lines) | stat: -rw-r--r-- 3,412 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
"""
subfig package

TO-DO
- Options handling
- Only works with figure environment
- Lists of floats

"""

from plasTeX import Command
from plasTeX.Base.LaTeX.Floats import Float

def ProcessOptions(options, document):
    context = document.context
    context.newcounter('subfloat', resetby='figure', format='${subfloat.alph}')

class newsubfloat(Command):
    args = '[ options:dict ] name:str'
    def invoke(self, tex):
        Command.invoke(self, tex)
        c = self.ownerDocument.context
        name = self.attributes['name']
        options = self.attributes['options'] or {}

        # Create new subfloat class
        newclass = type(name, (subfloat,),
                                {'options':options,'counter':name})
        c.addGlobal(name, newclass)

        # Create new counter
        c.newcounter(name, resetby='figure', format='${%s.alph}' % name)

        # Create the float name macro
        c.newcommand(name+'name', 0, name)

class DeclareCaptionListOfFormat(Command):
    args = 'keyword code'

class subfloatname(Command):
    str = ''

class subfloat(Command):
    args = '[ toc ] [ caption ] self'
    counter = 'subfloat'
    options = {}

    def preParse(self, tex):
        """
        This is getting tricky.  The ContinuedFloat tells whether or not
        the counter should be incremented.  We save the lastvalue of the
        counter in the userdata so we can get it back here.

        """
        doc = self.ownerDocument
        c = doc.context

        if doc.userdata.getPath('packages/subfig/continued'):
            v = doc.userdata.getPath('packages/subfig/subfloats/%s/lastvalue' %
                                     self.tagName, 1)
            c.counters[self.counter].setcounter(v+1)
            doc.userdata.setPath('packages/subfig/continued', False)
        else:
            doc.userdata.setPath('packages/subfig/subfloats/%s/lastvalue' %
                                 self.tagName, c.counters[self.counter].value)

        return Command.preParse(self, tex)

    def invoke(self, tex):
        Command.invoke(self, tex)
        self.title = self.attributes['caption'] or self.attributes['toc']

    @property # type: ignore # mypy#4125
    def ref(self):
        """
        This is a bit crazy.  We have to override ref so that we can
        add some new functionality.  The value normally gotten by ref
        is now held in subref.  The ref value also contains the ref value
        of the parent float node.

        """
        # Find the parent float of this subfloat
        node = self.parentNode
        while node is not None and not(isinstance(node, Float)):
            node = node.parentNode
        parentFloat = node

        # Add the float number to the ref value
        doc = self.ownerDocument
        frag = doc.createDocumentFragment()
        frag.append(parentFloat.caption.ref)
        frag.append(self.subref)
        return frag

    @ref.setter
    def ref(self, value):
        self.subref = value

class subref(Command):
    args = '* label:idref'

class ContinuedFloat(Command):
    def invoke(self, tex):
        Command.invoke(self, tex)
        doc = self.ownerDocument
        c = doc.context
        c.counters['figure'].value -= 1
        doc.userdata.setPath('packages/subfig/continued', True)

class listsubcaptions(Command):
    pass

class captionsetup(Command):
    args = '[ type:str ] options:dict'