File: aafigure_directive.py

package info (click to toggle)
aafigure 0.5-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 332 kB
  • sloc: python: 1,697; xml: 247; makefile: 104
file content (98 lines) | stat: -rw-r--r-- 3,507 bytes parent folder | download | duplicates (5)
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
"""\
Implement aafigure directive for docutils.
"""

import os
#~ import cStringIO
from aafigure import aafigure

from docutils import nodes
from docutils.parsers.rst.directives import register_directive, flag

DEFAULT_FORMAT = 'svg'
#~ DEFAULT_FORMAT = 'png'

aafigure_counter = 0

def decode_color(color_string):
    if color_string[0] == '#':          # HTML like color syntax
        if len(color_string) == 4:      # #rgb format
            r,g,b = [int(c+c, 16) for c in color_string[1:]]
        elif len(color_string) == 7:      # #rrggbb format
            r,g,b = [int(color_string[n:n+2], 16) for n in range(1, len(color_string), 2)]
        else:
            raise ValueError('not a valid color: %r' % color_string)
    # XXX add a list of named colors
    return r, g, b


def AAFigureDirective(name, arguments, options, content, lineno,
                  content_offset, block_text, state, state_machine):
    text = '\n'.join(content)

    global aafigure_counter

    # ensure that options are present and initialized with defaults if not given
    if not options.has_key('background'): options['background'] = '#ffffff'
    if not options.has_key('foreground'): options['foreground'] = '#000000'
    if not options.has_key('fill'): options['fill'] = options['foreground'] # fill = fore by default
    if not options.has_key('scale'): options['scale'] = 1
    if not options.has_key('line_width'): options['line_width'] = 2
    if not options.has_key('format'): options['format'] = DEFAULT_FORMAT
    if not options.has_key('aspect'): options['aspect'] = 1
    if not options.has_key('proportional'): options['proportional'] = False
    if not options.has_key('name'):
        options['name'] = 'aafigure-%i' % aafigure_counter
        aafigure_counter += 1

    output_name = options['name'] + '.' + options['format'].lower()
    try:
        (visitor, output) = aafigure.render(text, output_name, options)
    except aafigure.UnsupportedFormatError, e:
        result = [state_machine.reporter.error(str(e),
            nodes.literal_block(block_text, block_text),
            line=lineno
        )]
    output.close()

    if options['format'] == 'svg':
        #~ svgout.visit(aaimg, xml_header = False)
        # insert data into html using a raw node
        attributes = {'format': 'html'}
        #~ # result = [nodes.raw('', '<embed src="%s" %s type="image/svg+xml"/>' % (
        result = [nodes.raw('', '<object type="image/svg+xml" data="%s" %s>'
                '</object>' % (output_name, visitor.get_size_attrs()),
                **attributes)]
        #~ result = [nodes.raw('', io.getvalue(), **attributes)]
    elif options['format'] == 'pdf':
        # Return a link.
        wrapper = nodes.generated()
        wrapper.append(nodes.reference('', '%s (PDF)' % options['name'],
            refuri=os.path.basename(output_name)
        ))
        result = [wrapper]
    else:
        # Return an image directive.
        image_options = {}
        image_options['uri'] = os.path.basename(output_name)
        result = [nodes.image(output_name, **image_options)]

    return result

AAFigureDirective.content = True
#~ AAFigureDirective.arguments = (1, 1, 1)
AAFigureDirective.options = {
    'scale': float,
    'line_width': float,
    'format': str,
    'name': str,
    'background': str,
    'foreground': str,
    'fill': str,
    'aspect': float,
    'textual': flag,
    'proportional': flag,
}

def register():
    register_directive('aafigure', AAFigureDirective)