Browse files

Merge pull request #21 from raphink/dev/slide_design

Set slide design per slide
  • Loading branch information...
2 parents dfe5fb7 + 691ac3f commit b032f5a44ea1cde3638828e077aedb02c394d90a @mattharrison mattharrison committed Jul 3, 2012
Showing with 59 additions and 14 deletions.
  1. +39 −5 bin/rst2odp
  2. +20 −9 odplib/preso.py
View
44 bin/rst2odp
@@ -69,6 +69,21 @@ class SyntaxHighlightCodeBlock(rst.Directive):
rst.directives.register_directive('code-block', SyntaxHighlightCodeBlock)
+class SetSlideDesign(rst.Directive):
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = 1
+ has_content = False
+ #
+ # See visit_literal_block for code that processes the node
+ # created here.
+ def run(self):
+ design = self.arguments[0]
+ design_block = nodes.docinfo(name="slide-design", design=design)
+ return [design_block]
+
+rst.directives.register_directive('slide-design', SetSlideDesign)
+
class ImportNode(nodes.General, nodes.Inline, nodes.Element): pass
class ImportSlideBlock(rst.Directive):
@@ -108,6 +123,10 @@ class Writer(writers.Writer):
['--pages-to-output'],
{'action': 'store',
'dest': 'pages_to_output'}),
+ ('Specify a Pygments style (see pygmentize -L styles)',
+ ['--pygments-style'],
+ {'action': 'store',
+ 'dest': 'pygments_style'}),
('Specify WxH (cm) default(%sx%s)' % (preso.SLIDE_WIDTH,
preso.SLIDE_HEIGHT),
['--page-size'],
@@ -149,6 +168,9 @@ class ODPTranslator(nodes.GenericNodeVisitor):
if self.settings.font:
preso.NORMAL_FONT = self.settings.font
+ if self.settings.pygments_style:
+ preso.PYGMENTS_STYLE = self.settings.pygments_style
+
self.in_node = {} # map of tagname to True if we are in/under this
self._reset()
@@ -159,10 +181,10 @@ class ODPTranslator(nodes.GenericNodeVisitor):
self.bullet_depth = 0
self.footer = None
- def _init_slide(self, force=False):
+ def _init_slide(self, force=False, master_page_name=None):
if force or self.cur_slide is None:
self._reset()
- self.cur_slide = self.preso.add_slide()
+ self.cur_slide = self.preso.add_slide(master_page_name=master_page_name)
def at(self, nodename):
"""
@@ -173,6 +195,15 @@ class ODPTranslator(nodes.GenericNodeVisitor):
def get_whole(self):
return self.preso.get_data(self.settings.template_file)
+ def get_slide_design(self, node):
+ children = node.parent.children
+ design = ''
+ for child in children:
+ if child is node:
+ return design
+ if child.attributes.get('name', None) == 'slide-design':
+ design = child.attributes['design']
+
def dispatch_visit(self, node):
# Easier just to throw nodes I'm in in a dict, than keeping
# state for each one
@@ -209,7 +240,8 @@ class ODPTranslator(nodes.GenericNodeVisitor):
def visit_title(self, node):
if self.at('section') < 2:
- self._init_slide()
+ master_page_name = self.get_slide_design(node)
+ self._init_slide(master_page_name=master_page_name)
if self.at('topic'):
return
elif self.at('sidebar'):
@@ -523,7 +555,8 @@ class ODPTranslator(nodes.GenericNodeVisitor):
# first page has no section
if self.at('section') < 2:
# don't create slide for subsections
- self._init_slide(force=True)
+ master_page_name = self.get_slide_design(node)
+ self._init_slide(force=True, master_page_name=master_page_name)
def depart_section(self, node):
if self.at('section') < 1:
@@ -532,7 +565,8 @@ class ODPTranslator(nodes.GenericNodeVisitor):
def visit_transition(self, node):
# hack to have titleless slides (transitions come in between sections)
self._reset()
- self._init_slide(force=True)
+ master_page_name = self.get_slide_design(node)
+ self._init_slide(force=True, master_page_name=master_page_name)
depart_transition = _dumb_depart
View
29 odplib/preso.py
@@ -66,6 +66,7 @@
MONO_FONT = 'Courier New' # I like 'Envy Code R'
NORMAL_FONT = 'Arial'
+PYGMENTS_STYLE = 'default'
SLIDE_WIDTH = 30 # cm
SLIDE_HEIGHT = 21
@@ -336,9 +337,9 @@ def add_style(self, style):
self._auto_styles.append(node)
- def add_slide(self):
+ def add_slide(self, master_page_name=''):
pnum = len(self.slides)+1
- s = Slide(self, page_number=pnum)
+ s = Slide(self, page_number=pnum, master_page_name=master_page_name)
self.slides.append(s)
return s
@@ -562,7 +563,7 @@ def get_node(self):
class Slide(object):
- def __init__(self, preso, page_number=None):
+ def __init__(self, preso, page_number=None, master_page_name=''):
self.title_frame = None
self.preso = preso
self.text_frames = []
@@ -578,6 +579,10 @@ def __init__(self, preso, page_number=None):
self.paragraph_attribs = {} # used to mark id's for animations
self.page_number_listeners = [self]
self.pending_styles = []
+ if master_page_name:
+ self.master_page_name = master_page_name
+ else:
+ self.master_page_name = self._get_master_page_name()
self.element_stack = [] # allow us to push pop
self.cur_element = None # if we write it could be to title,
@@ -635,7 +640,7 @@ def add_code(self, code, language):
style = ParagraphStyle(**{'fo:text-align':'start'})
self.push_style(style)
output = pygments.highlight(code, lexers.get_lexer_by_name(language, stripall=True),
- OdtCodeFormatter(self.cur_element, self._preso))
+ OdtCodeFormatter(self.cur_element, self._preso, style=PYGMENTS_STYLE))
self.pop_style()
self.pop_node()
@@ -696,11 +701,11 @@ def _get_master_page_name(self):
return 'Default'
def _init_xml(self):
- mpn = self._get_master_page_name()
+ mpn = self.master_page_name
self._page = el('draw:page', attrib={
'draw:name':'page%d' % self.page_number,
'draw:style-name':'dp1',
- 'draw:master-page-name':self._get_master_page_name(),
+ 'draw:master-page-name':mpn,
'presentation:presentation-page-layout-name':'AL1T0'
})
office_forms = sub_el(self._page, 'office:forms',
@@ -1271,8 +1276,8 @@ class ParagraphStyle(TextStyle):
if pygmentsAvail:
class OdtCodeFormatter(formatter.Formatter):
- def __init__(self, writable, preso):
- formatter.Formatter.__init__(self)
+ def __init__(self, writable, preso, **options):
+ formatter.Formatter.__init__(self, **options)
self.writable = writable
self.preso = preso
@@ -1323,7 +1328,13 @@ def get_style(self, tokentype):
if value['bold']:
results['fo:font-weight'] = 'bold'
if value['italic']:
- results['fo:font-weight'] = 'italic'
+ results['fo:font-style'] = 'italic'
+ if value['underline']:
+ results['style:text-underline-type'] = 'single'
+ if value['bgcolor']:
+ results['fo:background-color'] = '#' + value['bgcolor']
+ if value['border']:
+ results['fo:border'] = '#' + value['border']
return results

0 comments on commit b032f5a

Please sign in to comment.