summaryrefslogtreecommitdiff
path: root/sandbox/rst2odp/bin/rst2odp.py
blob: 3f61c7daca4847c24249a876d858777c713dcb90 (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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2008-2009 Matt Harrison
# Licensed under Apache License, Version 2.0 (current)
import sys
import os

import docutils
from docutils import io, writers, nodes
from docutils.readers import standalone
from docutils.core import Publisher, default_description, \
    default_usage
from docutils.parsers import rst

import odplib.preso as preso
  
  
S5_COLORS = dict(
    black='#000000',
    gray='#545454',
    silver='#c0c0c0',
    white='#ffffff',
    maroon='#b03060',
    red='#ff0000',
    magenta='#ff00ff',
    fuchsia='#ff00ff', # FIX
    pink='#ff1493',
    orange='#ffa500',
    yellow='#ffff00',
    lime='#32cd32',
    green='#00ff00',
    olive='#6b8e23',
    teal='#008080',
    cyan='#00ffff',
    aqua='#00ffff', # FIX
    blue='#0000ff',
    navy='#000080',
    purple='#a020f0'
)  

S5_SIZES = dict(
    huge='66pt',
    big='44pt',
    normal='28pt',
    small='22pt',
    tiny='18pt'
)

class SyntaxHighlightCodeBlock(rst.Directive):
    required_arguments = 1
    optional_arguments = 0
    has_content = True
    #
    # See visit_literal_block for code that processes the node 
    #   created here.
    def run(self):
        language = self.arguments[0]
        code_block = nodes.literal_block(classes=["code-block", language],
            language=language)
        lines = self.content
        content = '\n'.join(lines)
        text_node = nodes.Text(content)
        code_block.append(text_node)
        # Mark this node for high-lighting so that visit_literal_block
        #   will be able to hight-light those produced here and
        #   *not* high-light regular literal blocks (:: in reST).
        code_block['hilight'] = True
        return [code_block]

rst.directives.register_directive('code-block', SyntaxHighlightCodeBlock)

class ImportNode(nodes.General, nodes.Inline, nodes.Element): pass

class ImportSlideBlock(rst.Directive):
    required_arguments = 2
    optional_arguments = 0
    has_content = False
    node_class = ImportNode
    #
    # See visit_literal_block for code that processes the node 
    #   created here.
    def run(self):
        odp_path = self.arguments[0]
        page_num = self.arguments[1]
        node = ImportNode(odp_path=odp_path, page_num=page_num)
        return [node]

rst.directives.register_directive('importslide', ImportSlideBlock)

class Writer(writers.Writer):
    settings_spec = (
        'ODP Specific Options', # option group title
        None, # Description
        ( # options (help string, list of options, dictions of OptionParser.add_option dicts)
            ('Specify a template (.otp) to use for styling',
             ['--template-file'],
             {'action': 'store',
              'dest': 'template_file'}),
            ('Specify a monospace font to use ("Courier New" default)',
             ['--mono-font'],
             {'action': 'store',
              'dest': 'mono_font'}),
            ('Specify a normal font to use ("Arial" default)',
             ['--font'],
             {'action': 'store',
              'dest': 'font'}),
            ('Specify pages to export (2,3,9-10)',
             ['--pages-to-output'],
             {'action': 'store',
              'dest': 'pages_to_output'})
            )
        )
    def __init__(self):
        writers.Writer.__init__(self)
        self.translator_class = ODPTranslator

    def translate(self):
        self.visitor = self.translator_class(self.document)
        self.document.walkabout(self.visitor)
        self.parts['whole'] = self.visitor.get_whole()
        self.output = self.parts['whole']
        self.parts['encoding'] = self.document.settings.output_encoding
        self.parts['version'] = docutils.__version__
        
class ODPTranslator(nodes.GenericNodeVisitor):
    def __init__(self, document):
        nodes.GenericNodeVisitor.__init__(self, document)
        self.settings = document.settings
        self.preso = preso.Preso()
        
        if self.settings.pages_to_output:
            self.preso.limit_pages = num_string_to_list(self.settings.pages_to_output)

        if self.settings.mono_font:
            preso.MONO_FONT = self.settings.mono_font
            
        if self.settings.font:
            preso.NORMAL_FONT = self.settings.font

        self.in_node = {} # map of tagname to True if we are in/under this
        self._reset()

    def _reset(self):
        # state we keep track of
        self.cur_slide = None
        self.bullet_list = None
        self.bullet_depth = 0
        self.footer = None

    def _init_slide(self, force=False):
        if force or self.cur_slide is None:
            self._reset()
            self.cur_slide = self.preso.add_slide()

    def at(self, nodename):
        """
        shortcut for at/under this node
        """
        return self.in_node.get(nodename, False)

    def get_whole(self):
        return self.preso.get_data(self.settings.template_file)

    def dispatch_visit(self, node):
        # Easier just to throw nodes I'm in in a dict, than keeping
        # state for each one
        count = self.in_node.setdefault(node.tagname, 0)
        self.in_node[node.tagname] += 1
        nodes.GenericNodeVisitor.dispatch_visit(self, node)

    def dispatch_departure(self, node):
        self.in_node[node.tagname] -= 1
        nodes.GenericNodeVisitor.dispatch_departure(self, node)

    def default_visit(self, node):
        if self.settings.report_level >= 3:
            print "ERR! NODE", node, node.tagname
        raise NotImplementedError('node is %r, tag is %s' % (node, node.tagname))


    def default_departure(self, node):
        if self.settings.report_level >= 3:
            print "NODE", node, node.tagname
        raise NotImplementedError


    def _dumb_visit(self, node):
        pass
    _dumb_depart = _dumb_visit


    def visit_document(self, node):
        if self.settings.report_level >= 3:
            print "DOC", node
    
    depart_document = _dumb_depart

    def visit_title(self, node):
        if self.at('section') < 2:
            self._init_slide()
        if self.at('topic'):
            return
        elif self.at('sidebar'):
            return
        elif self.at('section') < 2:
            self.cur_slide.add_title_frame()
    
    def depart_title(self, node):
        if self.at('topic'):
            return
        elif self.at('sidebar'):
            return
        elif self.at('section') < 2:
            # not in a title element anymore
            self.cur_slide.cur_element = None
        else:
            pass
        
            
    def visit_Text(self, node):
        if self.bullet_list and not self.at('handout'): # !!!need to deal w/ bullets in handout
            self.bullet_list.write(node.astext())
        elif self.at('footer'):
            self.footer.write(node.astext())
        elif self.at('comment'):
            pass
        elif self.at('topic'):
            pass
        elif self.at('literal_block'):
            pass
        elif self.at('reference'):
            # FirstClown - if we have link text, we need to make sure the text
            # doesn't get any styles applied or it sometimes doesn't show.
            self.cur_slide.write(node.astext(), add_p_style=False, add_t_style=False)
        elif self.at('doctest_block'):
            pass
        elif self.at('field_name'):
            pass
        else:
            self.cur_slide.write(node.astext())
    depart_Text = _dumb_depart

    def _push_handout(self, classes):
        if 'handout' in classes:
            self.in_node['handout'] = True
            self.cur_slide.push_element()
            if not self.cur_slide.notes_frame:
                self.cur_slide.add_notes_frame()
            else:
                self.cur_slide.cur_element = self.cur_slide.notes_frame
                self.cur_slide.insert_line_break += 1
                self.cur_slide.insert_line_breaks()


    def visit_paragraph(self, node):
        classes =  node.attributes.get('classes', [])
        self._push_handout(classes)

        ## if 'center' in classes:
        ##     attribs = {'fo:text-align':'center', 
        ##                'fo:margin-left':'1.2cm',
        ##                'fo:margin-right':'-.9cm',
        ##                }
        ##     style = preso.ParagraphStyle(**attribs)
        ##     self.cur_slide.push_style(style)

        ## elif 'left' in classes:
        ##     pass # default
        ## elif 'right' in classes:
        ##     attribs = {'fo:text-align':'end',
        ##                }
        ##     style = preso.ParagraphStyle(**attribs)
        ##     self.cur_slide.push_style(style)
        p_attribs = self._get_para_attribs(node)
        if p_attribs:
            self.cur_slide.push_style(preso.ParagraphStyle(**p_attribs))
            
        # text styles
        attribs = self._get_text_attribs(node)
        if attribs:
            style = preso.TextStyle(**attribs)
            self.cur_slide.push_style(style)
            
        if self.bullet_list:
            pass
        elif self.at('topic'):
            return
        elif self.at('block_quote'):
            return # block quote adds paragraph style
        elif self.at('doctest_block'):
            pass
        

    def depart_paragraph(self, node):
        # add newline
        if not self.at('list_item'):
            self.depart_line(node)

        classes = node.attributes.get('classes', [])
        if 'center' in classes or 'right' in classes:
            self.cur_slide.pop_node()
        if 'handout' in classes:
            self.in_node['handout'] = False
            self.cur_slide.pop_element()
            
        if self._get_text_attribs(node):
            # pop off text:span
            self.cur_slide.pop_node()
            self.cur_slide.pop_style()

        elif self.at('topic'):
            return
        elif self.at('block_quote'):
            return # block quote adds paragraph style
        else:
            self.cur_slide.parent_of('text:p')

    visit_definition = _dumb_visit
    depart_definition = _dumb_depart

    def visit_bullet_list(self, node):
        if self.at('topic'):
            return
        p_attribs = self._get_para_attribs(node)
        if p_attribs:
            self.cur_slide.push_style(preso.ParagraphStyle(**p_attribs))

        attribs = self._get_text_attribs(node)
        if attribs:
            style = preso.TextStyle(**attribs)
            self.cur_slide.push_style(style)

        classes =  node.attributes.get('classes', [])
        if 'handout' in classes:
            self._push_handout(classes)

        self.bullet_depth += 1
        if not self.bullet_list:
            # start a new list
            self.bullet_list = preso.OutlineList(self.cur_slide)
            self.cur_slide.add_list(self.bullet_list)
        else:
            # indent one more
            self.bullet_list.indent()
        if 'incremental' in node.attributes.get('classes', []):
            self.in_node['incremental'] = True 

    def depart_bullet_list(self, node):
        if self.at('topic'):
            return

        classes = node.attributes.get('classes', [])
        if 'handout' in classes:
            self.in_node['handout'] = False
            self.cur_slide.pop_element()
            
        self.bullet_depth -= 1
        if self.bullet_depth == 0:
            # done with list
            self.bullet_list = None
            self.cur_slide.pop_element()
            self.cur_slide.insert_line_break += 1
        else:
            self.bullet_list.dedent()            
        if 'incremental' in node.attributes.get('classes', []):
            self.in_node['incremental'] = False

    visit_definition_list = visit_bullet_list
    depart_definition_list = depart_bullet_list


    def visit_list_item(self, node):
        if self.at('topic'):
            return
        if self.at('incremental'):
            self.cur_slide.start_animation(preso.Animation())
        self.bullet_list.new_item()

    def depart_list_item(self, node):
        if self.at('topic'):
            return
        if self.at('incremental'):
            self.cur_slide.end_animation()

    visit_definition_list_item = visit_list_item
    depart_definition_list_item = depart_list_item


    visit_decoration = _dumb_visit
    depart_decoration = _dumb_depart

    def visit_footer(self, node):
        self.footer = preso.Footer(self.cur_slide)

    def depart_footer(self, node):
        self.preso.add_footer(self.footer)
        self.footer = None
        
    visit_docinfo = _dumb_visit
    depart_docinfo = _dumb_depart

    # bibliographic elements
    def visit_author(self, node):
        self.visit_attribution(node)

    def depart_author(self, node):
        self.depart_line(node) # add new-line
        self.depart_attribution(node)

    visit_copyright = visit_author
    depart_copyright = depart_author

    def visit_date(self, node):
        self.visit_attribution(node)

    def depart_date(self, node):
        self.depart_line(node) # add new-line
        self.depart_attribution(node)

    def visit_field(self, node):
        pass

    def depart_field(self, node):
        pass

    def visit_field_name(self, node):
        pass # maybe put this somewhere

    def depart_field_name(self, node):
        pass

    def visit_field_body(self, node):
        self.visit_attribution(node)

    def depart_field_body(self, node):
        self.depart_attribution(node)
        
    def visit_comment(self, node):
        pass
    def depart_comment(self, node):
        pass

    visit_topic = _dumb_visit
    depart_topic = _dumb_depart
 
    def visit_reference(self, node):
        """
        <draw:text-box>
          <text:p text:style-name="P4">
            <text:a xlink:href="http://www.yahoo.com/">Yahoo corp</text:a>
          </text:p>
        </draw:text-box>
        """
        if node.has_key('refid'):
            return
        elif self.at('topic'):
            return
        elif self.at('field_body'):
            self.visit_attribution(node)
            self.cur_slide.push_pending_node('text:a', {'xlink:href': '%s' % node['refuri'],
                                                        'xlink:type': 'simple'})
            self.cur_slide.write(node.astext()) 
        else:
            # needs to be in a p
            # need to hack a bit, since .write usually inserts text:p and text:span
            if not self.cur_slide.cur_element or not self.cur_slide.cur_element._in_p():
                #self.cur_slide.add_node('text:p', {})
                # pyohio code
                # we write an empty string since write() creates the paragraph
                # we need, with the style needed to reset from a possible Title
                # P0 style. This was most apparent when a link was first word
                # in a section after a title.
                self.cur_slide.write("")

            self.cur_slide.add_node('text:a', attrib={'xlink:href': '%s' % node['refuri'],
                                                      'xlink:type': 'simple'})
            
    def depart_reference(self, node):
        if node.has_key('refid'):
            return
        elif self.at('topic'):
            return
        elif self.at('field_body'):
            self.depart_attribution(node)
            self.cur_slide.parent_of('text:a')
        else:
            self.cur_slide.parent_of('text:a')

    def visit_target(self,node):
        # Skip the target element since the <reference> of the target is
        # responsible for writing out the content
        pass
    
    def depart_target(self, node):
        pass

    def visit_container(self, node):
        classes = node.attributes.get('classes', [])
        self._push_handout(classes)

    def depart_container(self, node):
        if self.in_node.get('handout', False):
            self.cur_slide.pop_element()
            self.in_node['handout'] = False

    visit_substitution_definition = _dumb_visit
    depart_substitution_definition = _dumb_depart

    def visit_section(self, node):

        # first page has no section
        if self.at('section') < 2:
            # don't create slide for subsections
            self._init_slide(force=True)

    def depart_section(self, node):
        if self.at('section') < 1:
            self._reset()

    def visit_transition(self, node):
        # hack to have titleless slides (transitions come in between sections)
        self._reset()
        self._init_slide(force=True)
    
    depart_transition = _dumb_depart

    def visit_literal(self, node):
        style = preso.TextStyle(**{
                'fo:font-family':preso.MONO_FONT,
                'style:font-family-generic':"swiss",
                'style:font-pitch':"fixed"})
        self.cur_slide.push_style(style)

    def depart_literal(self, node):
        # pop off the text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()

    def visit_inline(self,node):
        attribs = self._get_text_attribs(node)
        if attribs:
            style = preso.TextStyle(**attribs)       

            self.cur_slide.push_style(style)
        if 'incremental' in node.attributes.get('classes', []):
            self.cur_slide.start_animation(preso.Animation())

    def depart_inline(self, node):
        # pop off the text:span
        attribs = self._get_text_attribs(node)
        if attribs:
            self.cur_slide.pop_style()
            self.cur_slide.pop_node()
        if 'incremental' in node.attributes.get('classes', []):
            self.cur_slide.end_animation()

    def visit_emphasis(self, node):
        attribs = {'fo:font-style':'italic'}
        style = preso.TextStyle(**attribs)       
        self.cur_slide.push_style(style)

    def depart_emphasis(self, node):
        # pop off the text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()

    visit_title_reference = visit_emphasis
    depart_title_reference = depart_emphasis


    def visit_strong(self, node):
        attribs = {'fo:font-weight':'bold'}
        style = preso.TextStyle(**attribs)       
        self.cur_slide.push_style(style)
       
    def depart_strong(self, node):
        # pop off the text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()

    visit_term = visit_strong
    depart_term = depart_strong

    def visit_superscript(self, node):
        attribs = {'style:text-position':'super 58%'}
        style = preso.TextStyle(**attribs)
        self.cur_slide.push_style(style)

    def depart_superscript(self, node):
        # pop off the text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        
    def visit_subscript(self, node):
        attribs = {'style:text-position':'sub 58%'}
        style = preso.TextStyle(**attribs)
        self.cur_slide.push_style(style)

    def depart_subscript(self, node):
        # pop off the text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        
    def visit_block_quote(self, node):
        attribs = {'fo:text-align':'start', 
                   'fo:margin-left':'1.2cm',
                   'fo:margin-right':'-.9cm',
                   }
        style = preso.ParagraphStyle(**attribs)
        self.cur_slide.push_style(style)

    def depart_block_quote(self, node):
        # pop off the text:p
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        
    def visit_attribution(self, node):
        # right justify
        attribs = {'fo:text-align':'end',
                   'fo:margin-right':'.9cm'
                   }
        style = preso.ParagraphStyle(**attribs)
        self.cur_slide.push_style(style)

        # italics
        style = preso.TextStyle(**{'fo:font-size':S5_SIZES['small'],
                                   'fo:font-style':'italic'})
        self.cur_slide.push_style(style)

    def depart_attribution(self, node):
        # pop off the text:p and text:span
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()

    def visit_line_block(self, node):
        #jump out of current paragraph
        self.cur_slide.parent_of('text:p')

        p_attribs = self._get_para_attribs(node)
        if p_attribs:
            self.cur_slide.push_style(preso.ParagraphStyle(**p_attribs))
        else:    
            # right justify
            P_attribs = {'fo:text-align':'end',
                       'fo:margin-right':'.9cm'
                       }
            style = preso.ParagraphStyle(**p_attribs)
            self.cur_slide.push_style(preso.ParagraphStyle(**p_attribs))
        #self.cur_slide.push_style(style)
        attribs = self._get_text_attribs(node)
        if attribs:
            style = preso.TextStyle(**attribs)       
            self.cur_slide.push_style(style)

    def depart_line_block(self, node):
        attribs = self._get_text_attribs(node)
        if attribs:
            #self.cur_slide.pop_style()
            self.cur_slide.pop_node()
        # pop off text:p
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        

    def visit_line(self, node):
        pass

    def depart_line(self, node):
        self.cur_slide.insert_line_break += 1
        self.cur_slide.insert_line_breaks()
        
    @preso.cwd_decorator
    def visit_image(self, node):
        classes = node.attributes.get('classes', [])

        source = node.attributes['uri']
        p = preso.Picture(os.path.abspath(source), **node.attributes)
        self.cur_slide.add_picture(p)

    def depart_image(self, node):
        pass

    visit_figure = _dumb_visit
    depart_figure = _dumb_depart

    def visit_caption(self, node):
        #!!! fix
        pass

    def depart_caption(self, node):
        pass

    def visit_literal_block(self, node):
        attributes = node.attributes
        # left align
        style = preso.ParagraphStyle(**{'fo:text-align':'start'})
        self.cur_slide.push_style(style)
        # text styles
        attribs = self._get_text_attribs(node)
        if attribs:
            style = preso.TextStyle(**attribs)       
            self.cur_slide.push_style(style)

        if attributes['classes'] and 'code-block' in attributes['classes']:
            node_input = node.astext()
            language = node.attributes['language']
            self.cur_slide.add_code(node_input, language)
            # insert a new line after
            self.cur_slide.insert_line_break += 1

        else:
            style = preso.TextStyle(**{
                    'fo:font-family':preso.MONO_FONT,
                    'style:font-family-generic':"swiss",
                    'style:font-pitch':"fixed"})
            self.cur_slide.push_style(style)
            node_input = node.astext()
            chunks = node_input.split('\n')
            for chunk in chunks:
                self.cur_slide.write(chunk)
                self.cur_slide.insert_line_break += 1
                self.cur_slide.insert_line_breaks()



    def depart_literal_block(self, node):
        # pop text-align
        self.cur_slide.pop_style()
        self.cur_slide.pop_node()
        attributes = node.attributes
        if attributes['classes'] and 'code-block' in attributes['classes']:
            pass
        else:
            self.cur_slide.pop_style()
            self.cur_slide.pop_node()

    def visit_footnote(self, node):
        # shift to bottom of page?
        pass

    def depart_footnote(self, node):
        pass

    def visit_footnote_reference(self, node):
        self.visit_superscript(node)
        self.cur_slide.write('[')

    def depart_footnote_reference(self, node):
        self.cur_slide.write(']')
        self.depart_superscript(node)

    
    def visit_label(self, node):
        # part of footnote
        if self.at('footnote'):
            self.cur_slide.write('[')

    def depart_label(self, node):
        if self.at('footnote'):
            self.cur_slide.write('] ')


    def visit_enumerated_list(self, node):
        if self.at('topic'):
            return
        self.bullet_depth += 1
        if not self.bullet_list:
            self.bullet_list = preso.NumberList(self.cur_slide)
            self.cur_slide.add_list(self.bullet_list)
        else:
            # indent one more
            self.bullet_list.indent()
        if 'incremental' in node.attributes.get('classes', []):
            self.in_node['incremental'] = True 


    def depart_enumerated_list(self, node):
        self.depart_bullet_list(node)

    def visit_doctest_block(self, node):
        node_input = node.astext()
        language = 'pycon'
        self.cur_slide.add_code(node_input, language)
        # insert a new line after
        self.cur_slide.insert_line_break += 1


    def depart_doctest_block(self, node):
        pass

    def visit_importslide(self, node):
        pass


    def visit_table(self, node):
        table = preso.TableFrame(self.cur_slide)
        self.cur_slide.add_table(table)

    def depart_table(self, node):
        self.cur_slide.pop_element()

    def visit_row(self, node):
        self.cur_slide.cur_element.add_row()

    def depart_row(self, node):
        pass

    def visit_entry(self, node):
        self.cur_slide.cur_element.add_cell()

    def depart_entry(self, node):
        pass
    
    visit_tgroup = _dumb_visit
    depart_tgroup = _dumb_depart

    visit_colspec = _dumb_visit
    depart_colspec = _dumb_depart

    visit_thead = _dumb_visit
    depart_thead = _dumb_depart

    visit_tbody = _dumb_visit
    depart_tbody = _dumb_depart

    def visit_hint(self, node):
        return self._visit_hint(node, 'Hint')
    
    
    def _visit_hint(self, node, name):
        if self.cur_slide.text_frames:
            # should adjust width of other frame
            node = self.cur_slide.text_frames[-1].get_node()
            node.attrib['svg:width'] = '12.296cm'
        else:
            self.cur_slide.add_text_frame()
            
        self.cur_slide.push_element()
        #put the hint on the right side
        attrib = {
            'presentation:style-name':'pr2',
            'draw:layer':'layout',
            'svg:width':'12.296cm',
            'svg:height':'13.86cm',
            'svg:x':'14.311cm',
            'svg:y':'4.577cm',
            'presentation:class':'subtitle'
        }
        self.cur_slide.add_text_frame(attrib)
        self.cur_slide.write(name)
        self.cur_slide.insert_line_break = 2
        self.cur_slide.insert_line_breaks()

    def depart_hint(self, node):
        self.cur_slide.pop_element()

    def visit_sidebar(self, node):
        return self._visit_hint(node, 'Sidebar')

    depart_sidebar = depart_hint
        
        
    def _get_para_attribs(self, node):
        classes = node.attributes.get('classes', [])
        attribs = {}
        for c in classes:
            if c == 'center':
                attribs.update({'fo:text-align':'center', 
                                'fo:margin-left':'1.2cm',
                                'fo:margin-right':'.9cm',
                                })
            elif c == 'right':
                attribs.update({'fo:text-align':'end',
                                'fo:margin-right':'.9cm'
                                })
            elif c == 'left':
                attribs.update({'fo:text-align':'start', 
                                'fo:margin-left':'1.2cm',
                                'fo:margin-right':'5.9cm',
                                })
                #pass # default
        return attribs
        

    def _get_text_attribs(self, node):
        classes = node.attributes.get('classes', [])
        attribs = {}
        for c in classes:
            if c in S5_COLORS:
                attribs['fo:color'] = S5_COLORS[c]
            elif c in S5_SIZES:
                attribs['fo:font-size'] = S5_SIZES[c]
        return attribs

def num_string_to_list(numstr):
    """
    >>> num_string_to_list('2,5-7')
    [2, 5, 6, 7]
    >>> num_string_to_list('1')
    [1]

    """
    nums = []
    if ',' in numstr:
        comma_delim = numstr.split(',')
        for part in comma_delim:
            if '-' in part:
                start, end = [int(x) for x in part.split('-')]
                for num in range(start, end+1):
                    nums.append(num)
            else:
                nums.append(int(part))
    elif '-' in numstr:
        start, end = [int(x) for x in numstr.split('-')]
        for num in range(start, end+1):
            nums.append(num)
    else:
        nums.append(int(numstr))
    return nums
            

class BinaryFileOutput(io.FileOutput):
    """
    A version of docutils.io.FileOutput which writes to a binary file.
    """
    def open(self):
        try:
            self.destination = open(self.destination_path, 'wb')
            
        except IOError, error:
            if not self.handle_io_errors:
                raise
            print >>sys.stderr, '%s: %s' % (error.__class__.__name__,
                                            error)
            print >>sys.stderr, ('Unable to open destination file for writing '
                                 '(%r).  Exiting.' % self.destination_path)
            sys.exit(1)
        self.opened = 1


def main(prog_args):
    argv = None
    reader = standalone.Reader()
    reader_name = 'standalone'
    writer = Writer()
    writer_name = 'pseudoxml'
    parser = None
    parser_name = 'restructuredtext'
    settings = None
    settings_spec = None
    settings_overrides = None
    config_section = None
    enable_exit_status = 1
    usage = default_usage
    publisher = Publisher(reader, parser, writer, settings,
                          destination_class=BinaryFileOutput)
    publisher.set_components(reader_name, parser_name, writer_name)
    description = ('Generates OpenDocument/OpenOffice/ODF slides from '
                   'standalone reStructuredText sources.  ' + default_description)

    output = publisher.publish(argv, usage, description,
                               settings_spec, settings_overrides,
                               config_section=config_section,
                               enable_exit_status=enable_exit_status)


def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    if '--doctest' in sys.argv:
        _test()
    else:
        sys.exit(main(sys.argv) or 0)