summaryrefslogtreecommitdiffstats
path: root/presentty/rst.py
blob: e96ff2108148369592b7ad9868071fce35e8da69 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# Copyright (C) 2015 James E. Blair <corvus@gnu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import docutils
import docutils.frontend
import docutils.parsers.rst
import docutils.nodes
import cStringIO as StringIO

import urwid

import slide
import transition as transition_mod
import image
import ansiparser
import text

try:
    import PIL
    import PIL.Image
except ImportError:
    PIL = None

DEFAULT_TRANSITION = 'dissolve'
DEFAULT_TRANSITION_DURATION = 0.4

class TextAccumulator(object):
    def __init__(self):
        self.text = []

    def append(self, text):
        self.text.append(text)

    def getFormattedText(self):
        return self.text

    wsre = re.compile('\s+')

    def getFlowedText(self):
        ret = []
        for part in self.text:
            if isinstance(part, tuple):
                ret.append((part[0], self.wsre.sub(u' ', part[1])))
            else:
                ret.append(self.wsre.sub(u' ', part))
        if not ret:
            return u''
        return ret

class UrwidTranslator(docutils.nodes.GenericNodeVisitor):
    transition_map = {'dissolve': transition_mod.DissolveTransition,
                      'cut': transition_mod.CutTransition,
                      'pan': transition_mod.PanTransition,
                      }

    def __init__(self, document, palette, hinter=None, basedir='.'):
        docutils.nodes.GenericNodeVisitor.__init__(self, document)
        self.program = []
        self.stack = []
        self.default_transition = self._make_transition(
            DEFAULT_TRANSITION,
            DEFAULT_TRANSITION_DURATION)
        self.transition = self.default_transition
        self.attr = []
        self.table_columns = []
        self.table_column = []
        self.progressives = []
        self.palette = palette
        self.hinter = hinter
        self.basedir = basedir
        self.slide = None
        self.default_hide_title = False
        self.hide_title = self.default_hide_title

    def _make_transition(self, name, duration):
        tr = self.transition_map[name]
        return tr(duration)

    def default_visit(self, node):
        """Override for generic, uniform traversals."""
        pass

    def default_departure(self, node):
        """Override for generic, uniform traversals."""
        pass

    def _append(self, node, widget, *args, **kw):
        if self.stack:
            if 'handout' in node.get('classes'):
                if self.handout_pile not in self.stack:
                    container = self.handout_pile
                else:
                    # If the handout pile is in the stack, then ignore
                    # this class -- it has probably needlessly been
                    # applied to something deeper in the stack.  The
                    # thing further up will end up in the handout.
                    container = self.stack[-1]
            else:
                container = self.stack[-1]
            container.contents.append((widget, container.options(*args, **kw)))

    def styled(self, style, text):
        if style in self.palette:
            return (self.palette[style], text)
        return text

    def visit_transition(self, node):
        name = node['name']
        duration = node.get('duration', DEFAULT_TRANSITION_DURATION)
        self.transition = self._make_transition(name, duration)

    def depart_transition(self, node):
        pass

    def visit_hidetitle(self, node):
        if self.slide:
            self.hide_title = True
        else:
            self.default_hide_title = True

    def depart_hidetitle(self, node):
        pass

    def visit_system_message(self, node):
        #print node.astext()
        raise docutils.nodes.SkipNode()

    def visit_section(self, node):
        self.hide_title = self.default_hide_title
        self.transition = self.default_transition
        title_pile = slide.SlidePile([])
        title_pad = slide.SlidePadding(title_pile,
                                       align='center', width='pack')

        main_pile = slide.SlidePile([])
        main_pad = slide.SlidePadding(main_pile, align='center', width='pack')
        outer_pile = slide.SlidePile([
            ('pack', title_pad),
            ('pack', main_pad),
        ])
        s = slide.UrwidSlide(u'', self.transition, outer_pile,
                             self.palette['_default'])
        self.slide = s
        self.stack.append(main_pile)
        self.title_pile = title_pile

        pile = slide.SlidePile([])
        s = slide.Handout(pile, self.palette['_default'])
        self.handout = s
        self.handout_pile = pile
        self.slide.handout = s

    def depart_section(self, node):
        self.slide.transition = self.transition
        if self.hide_title:
            self.title_pile.contents[:] = []
        self.program.append(self.slide)
        self.stack.pop()

    def visit_block_quote(self, node):
        self.stack.append(slide.SlidePile([]))

    def depart_block_quote(self, node):
        pile = self.stack.pop()
        pad = slide.SlidePadding(pile, left=2)
        self._append(node, pad, 'pack')

    def visit_list_item(self, node):
        self.stack.append(slide.SlidePile([]))

    def depart_list_item(self, node):
        pile = self.stack.pop()
        bullet = urwid.Text(u'* ')
        cols = slide.SlideColumns([])
        cols.contents.append((bullet, cols.options('pack')))
        cols.contents.append((pile, cols.options('weight', 1)))
        if self.progressives:
            cols = urwid.AttrMap(cols, self.palette['progressive'])
            self.progressives[-1].append(cols)
        self._append(node, cols, 'pack')

    def visit_tgroup(self, node):
        self.table_columns.append([])
        self.stack.append(slide.SlidePile([]))

    def visit_colspec(self, node):
        self.table_columns[-1].append(node['colwidth'])

    def visit_row(self, node):
        self.stack.append(slide.SlideColumns([], dividechars=1))
        self.table_column.append(0)

    def depart_row(self, node):
        self.table_column.pop()
        cols = self.stack.pop()
        self._append(node, cols, 'pack')

    def visit_thead(self, node):
        pass

    def depart_thead(self, node):
        cols = slide.SlideColumns([], dividechars=1)
        for width in self.table_columns[-1]:
            cols.contents.append((urwid.Text(u'='*width),
                                  cols.options('given', width)))
        self._append(node, cols, 'pack')

    def visit_entry(self, node):
        self.stack.append(slide.SlidePile([]))

    def depart_entry(self, node):
        colindex = self.table_column[-1]
        self.table_column[-1] = colindex + 1
        pile = self.stack.pop()
        self._append(node, pile, 'given', self.table_columns[-1][colindex])

    def depart_tgroup(self, node):
        self.table_columns.pop()
        pile = self.stack.pop()
        self._append(node, pile, 'pack')

    def visit_textelement(self, node):
        self.stack.append(TextAccumulator())

    visit_paragraph = visit_textelement

    def depart_paragraph(self, node):
        text = self.stack.pop()
        self._append(node, urwid.Text(text.getFlowedText()), 'pack')

    visit_literal_block = visit_textelement

    def depart_literal_block(self, node):
        text = self.stack.pop()
        text = urwid.Text(text.getFormattedText(), wrap='clip')
        pad = slide.SlidePadding(text, width='pack')
        self._append(node, pad, 'pack')

    visit_line = visit_textelement

    def depart_line(self, node):
        text = self.stack.pop()
        self._append(node, urwid.Text(text.getFormattedText(), wrap='clip'),
                     'pack')

    visit_title = visit_textelement

    def depart_title(self, node):
        text = self.stack.pop()
        self.slide.title = node.astext()
        widget = urwid.Text(self.styled('title', text.getFlowedText()),
                            align='center')
        self.title_pile.contents.append(
            (widget, self.title_pile.options('pack')))

    def visit_Text(self, node):
        pass

    def depart_Text(self, node):
        if self.stack and isinstance(self.stack[-1], TextAccumulator):
            if self.attr:
                t = (self.attr[-1], node.astext())
            else:
                t =  node.astext()
            self.stack[-1].append(t)

    def visit_emphasis(self, node):
        self.attr.append(self.palette['emphasis'])

    def depart_emphasis(self, node):
        self.attr.pop()

    def visit_inline(self, node):
        cls = node.get('classes')
        if not cls:
            raise docutils.nodes.SkipDeparture()
        cls = [x for x in cls if x != 'literal']
        for length in range(len(cls), 0, -1):
            clsname = '-'.join(cls[:length])
            if clsname in self.palette:
                self.attr.append(self.palette[clsname])
                return
        raise docutils.nodes.SkipDeparture()

    def depart_inline(self, node):
        self.attr.pop()

    def visit_image(self, node):
        if not PIL:
            return
        uri = node['uri']
        fn = os.path.join(self.basedir, uri)
        w = image.ANSIImage(fn, self.hinter)
        self._append(node, w, 'pack')

    def visit_ansi(self, node):
        interval = node.get('interval', 0.5)
        oneshot = node.get('oneshot', False)
        animation = slide.AnimatedText(interval, oneshot)
        for name in node['names']:
            p = ansiparser.ANSIParser()
            fn = os.path.join(self.basedir, name)
            data = unicode(open(fn).read(), 'utf8')
            text = p.parse(data)
            animation.addFrame(text)
        self.slide.animations.append(animation)
        self._append(node, animation, 'pack')

    def depart_ansi(self, node):
        pass

    def visit_figlet(self, node):
        figlet = text.FigletText(node['text'])
        self._append(node, figlet, 'pack')

    def depart_figlet(self, node):
        pass

    def visit_cowsay(self, node):
        cowsay = text.CowsayText(node['text'])
        self._append(node, cowsay, 'pack')

    def depart_cowsay(self, node):
        pass

    def visit_container(self, node):
        self.stack.append(slide.SlidePile([]))
        if 'progressive' in node.get('classes'):
            self.progressives.append(self.slide.progressives)
            self.slide.progressive_attr = self.palette['progressive']

    def depart_container(self, node):
        pile = self.stack.pop()
        self._append(node, pile, 'pack')
        if 'progressive' in node.get('classes'):
            self.progressives.pop()

class TransitionDirective(docutils.parsers.rst.Directive):
    required_arguments = 1
    option_spec = {'duration': float}
    has_content = False

    def run(self):
        args = {'name': self.arguments[0]}
        duration = self.options.get('duration')
        if duration:
            args['duration'] = duration
        node = transition(**args)
        return [node]

class ANSIDirective(docutils.parsers.rst.Directive):
    required_arguments = 1
    final_argument_whitespace = True
    option_spec = {'interval': float,
                   'oneshot': bool}
    has_content = False

    def run(self):
        args = {'names': self.arguments[0].split()}
        args.update(self.options)
        node = ansi(**args)
        return [node]

class FigletDirective(docutils.parsers.rst.Directive):
    required_arguments = 1
    has_content = False
    final_argument_whitespace = True

    def run(self):
        args = {'text': self.arguments[0]}
        node = figlet(**args)
        return [node]

class CowsayDirective(docutils.parsers.rst.Directive):
    required_arguments = 1
    has_content = False
    final_argument_whitespace = True

    def run(self):
        args = {'text': self.arguments[0]}
        node = cowsay(**args)
        return [node]

class HideTitleDirective(docutils.parsers.rst.Directive):
    has_content = False

    def run(self):
        node = hidetitle()
        return [node]

class transition(docutils.nodes.Special, docutils.nodes.Invisible,
                 docutils.nodes.Element):
    pass

class ansi(docutils.nodes.General, docutils.nodes.Inline,
           docutils.nodes.Element):
    pass

class figlet(docutils.nodes.General, docutils.nodes.Inline,
             docutils.nodes.Element):
    pass

class cowsay(docutils.nodes.General, docutils.nodes.Inline,
             docutils.nodes.Element):
    pass

class hidetitle(docutils.nodes.Special, docutils.nodes.Invisible,
                docutils.nodes.Element):
    pass

class PresentationParser(object):
    def __init__(self, palette, hinter=None):
        docutils.parsers.rst.directives.register_directive(
            'transition', TransitionDirective)
        docutils.parsers.rst.directives.register_directive(
            'ansi', ANSIDirective)
        docutils.parsers.rst.directives.register_directive(
            'figlet', FigletDirective)
        docutils.parsers.rst.directives.register_directive(
            'cowsay', CowsayDirective)
        docutils.parsers.rst.directives.register_directive(
            'hidetitle', HideTitleDirective)
        self.warnings = StringIO.StringIO()
        self.settings = docutils.frontend.OptionParser(
            components=(docutils.parsers.rst.Parser,),
            defaults=dict(warning_stream=self.warnings)).get_default_values()
        self.parser = docutils.parsers.rst.Parser()
        self.palette = palette
        self.hinter = hinter

    def _parse(self, input, filename):
        document = docutils.utils.new_document(filename, self.settings)
        self.parser.parse(input, document)
        visitor = UrwidTranslator(document, self.palette, self.hinter,
                                  os.path.dirname(filename))
        document.walkabout(visitor)
        return document, visitor

    def parse(self, input, filename='program'):
        document, visitor = self._parse(input, filename)
        return visitor.program

def main():
    import argparse
    import palette

    argp = argparse.ArgumentParser(description='Test RST parser')
    argp.add_argument('file', help='presentation file (RST)')
    argp.add_argument('slides', nargs='*', default=[],
                      help='slides to render')
    argp.add_argument('--render', action='store_true',
                      help='Fully render a slide')
    args = argp.parse_args()

    parser = PresentationParser(palette.DARK_PALETTE)
    document, visitor = parser._parse(open(args.file).read(), args.file)

    slides = args.slides
    if not slides:
        slides = range(len(visitor.program))
    slides = [int(x) for x in slides]

    if not args.render:
        print document.pformat()
        for i in slides:
            print '-'*80
            s = visitor.program[i]
            for line in s.render((80,25)).text:
                print line
    else:
        screen = urwid.raw_display.Screen()
        with screen.start():
            for i in slides:
                s = visitor.program[i]
                screen.draw_screen((80,25), s.render((80,25)))
                raw_input()

if __name__ == '__main__':
    main()