Skip to content

Commit

Permalink
use doctree hook for positionning
Browse files Browse the repository at this point in the history
  • Loading branch information
gawel committed Oct 7, 2012
1 parent 3ce99fe commit 3219cc3
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 50 deletions.
4 changes: 4 additions & 0 deletions docs/default.rst
@@ -1,3 +1,7 @@
.. impress::
:func:default

========================
Default positioning demo
========================

Expand Down
15 changes: 13 additions & 2 deletions docs/index.rst
@@ -1,5 +1,6 @@
.. impress::

===================================
impress's documentation!
===================================

Expand All @@ -14,6 +15,15 @@ Use space/left/right keys to navigate.

http://github.com/impress/

.. toctree::
:maxdepth: 1
:titlesonly:

sample
default
linear
spiral

Installation
============

Expand All @@ -31,6 +41,7 @@ Quick start
===========

.. slide::
:class: dense

Write a ``index.rst`` file like this:

Expand Down Expand Up @@ -178,5 +189,5 @@ EOS

.. step::
:data-scale: 6
:data-x: 900
:data-y: 200
:data-x: 1300
:data-y: -1200
1 change: 1 addition & 0 deletions docs/linear.rst
@@ -1,6 +1,7 @@
.. impress::
:func: linear

=======================
Linear positioning demo
=======================

Expand Down
11 changes: 5 additions & 6 deletions docs/mymodule.py
@@ -1,7 +1,6 @@
from impress.funcs import defaults


def awesome_positioning(directive, i, coord):
coord.update(defaults)
coord['x'] += 1000
def awesome_positioning(slide, i, coord, slides):
if i > 0:
coord['x'] += 1000
coord['y'] += 1000
coord['rotate_y'] += 45
return coord
2 changes: 2 additions & 0 deletions docs/sample.rst
@@ -1,7 +1,9 @@
==============
My fisrt slide
==============

.. slide::
:class: first

My second slide
===============
Expand Down
3 changes: 1 addition & 2 deletions docs/spiral.rst
@@ -1,8 +1,7 @@
.. impress::
:func: spiral

.. _spiral:

========================
Spiral positionning demo
========================

Expand Down
2 changes: 2 additions & 0 deletions docs/static/custom.css
@@ -1 +1,3 @@
.black {color: black;}
.toctree-wrapper {display:none;}
.dense pre {line-height: 0.9em;}
54 changes: 32 additions & 22 deletions impress/directives.py
Expand Up @@ -84,21 +84,14 @@ class Step(rst.Directive):
'data-rotate-z': directives.unchanged,
}

def resolve_func(self, name):
if hasattr(funcs, name):
return getattr(funcs, name)
else:
mod, func = name.split('.')
mod = __import__(mod, globals(), locals(), [''])
return getattr(mod, func)

def run(self):
if 'reset' in os.environ:
del os.environ['reset']
Step.amounts = {}
Step.last_coord = {}

parent = self.state.parent

source = parent.document.attributes['source']
global_options = Impress.opts.setdefault(source, {})
for k, v in global_options.items():
Expand All @@ -114,20 +107,6 @@ def run(self):
if self.options.get('hide-title', 'false') == 'true':
title = parent.next_node()
title.attributes['classes'].insert(0, 'hidden')
if 'data-x' not in self.options:
func = self.resolve_func(self.options.get('func', 'default'))
amount = Step.amounts.setdefault(source, 0)
last_coord = Step.last_coord.setdefault(source, {})
new_attrs = func(self, amount, last_coord)
for k, v in new_attrs.items():
last_coord[k] = v
if k in ('x', 'y', 'z',
'rotate_x', 'rotate_y', 'rotate_z',
'scale'):
k = 'data-%s' % k.replace('_', '-')
if k not in self.options:
attrs[k] = [str(v)]
Step.amounts[source] += 1
else:
print('%s:: WARNING: %s found out of section are ignored' % (
source, self.__class__.__name__.lower()))
Expand All @@ -145,9 +124,40 @@ def run(self):
return super(Slide, self).run()


def resolve_func(name):
if hasattr(funcs, name):
return getattr(funcs, name)
else:
mod, func = name.split('.')
mod = __import__(mod, globals(), locals(), [''])
return getattr(mod, func)


def slides_position(app, doctree, docname):
slides = [s for s in doctree.children if s.tagname == 'section']
for slide in slides:
slides.extend([s for s in slide.children if s.tagname == 'section'])
slide.children = [s for s in slide.children if s.tagname != 'section']
doctree.children = [s for s in doctree.children if s.tagname != 'section']
doctree.children.extend(slides)
slides = [s for s in doctree.children if s.tagname == 'section']
coord = funcs.defaults.copy()
for i, slide in enumerate(slides):
func = resolve_func(slide.attributes.get('func', 'default'))
coord = func(slide, i, coord.copy(), slides)
for k, v in coord.items():
if k in ('x', 'y', 'z',
'rotate_x', 'rotate_y', 'rotate_z',
'scale'):
k = 'data-%s' % k.replace('_', '-')
if k not in slide.attributes:
slide.attributes[k] = [str(v)]


def setup(app):
app.add_directive('impress', Impress)
app.add_directive('step', Step)
app.add_directive('slide', Slide)
app.connect('html-page-context', change_pathto)
app.connect('doctree-resolved', slides_position)
app.connect('build-finished', move_private_folders)
20 changes: 7 additions & 13 deletions impress/funcs.py
Expand Up @@ -4,30 +4,26 @@
defaults = dict(x=0, y=0, rotate_x=0, rotate_y=0, rotate_z=0, scale=1)


def default(directive, i, coord):
def default(slide, i, coord, slides):
""":doc:`default`"""
if coord:
if i > 0:
coord['x'] += 1000
coord['y'] += 500
coord['rotate_x'] += (1000 / 180. * math.pi)
else:
coord.update(defaults)
return coord


def linear(directive, i, coord):
def linear(slide, i, coord, slides):
""":doc:`linear`"""
if coord:
if i > 0:
coord['x'] += 1000
else:
coord.update(defaults)
return coord


def spiral(directive, i, coord):
def spiral(slide, i, coord, slides):
""":doc:`spiral`"""
if coord:
r = 1200
r = 1200
if i > 0:
if coord['x'] > 0:
incr = 100 * i / 3.
else:
Expand All @@ -38,6 +34,4 @@ def spiral(directive, i, coord):
coord['rotate_x'] += ((r + incr) / 180. * math.pi)
coord['rotate_y'] += ((r + incr) / 180. * math.pi)
coord['rotate_y'] += ((r + incr) / 180. * math.pi)
else:
coord = defaults.copy()
return coord
6 changes: 1 addition & 5 deletions impress/layout.html
Expand Up @@ -12,11 +12,7 @@
</head>
<body>
<div id="impress">
{%- block content %}
{%- block document %}
{% block body %} {% endblock %}
{%- endblock %}
{%- endblock %}
{% block body %} {% endblock %}
</div>
<script src="{{ pathto('_static/impress/js/impress.js', 1) }}"></script>
<script src="{{ pathto('_static/jquery.js', 1) }}"></script>
Expand Down

0 comments on commit 3219cc3

Please sign in to comment.