Skip to content

Commit

Permalink
Fix for bug nyergler#102 (nextslide uses wrong title and insert point…
Browse files Browse the repository at this point in the history
…) when using headers above auto-slide-level (bug nyergler#100)
  • Loading branch information
Albert Mietus authored and Albert Mietus committed Jul 28, 2015
1 parent 273f785 commit 2375e8f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 78 deletions.
1 change: 0 additions & 1 deletion src/hieroglyph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,3 @@ def setup(app):
app.connect('builder-inited', html.inspect_config)
app.connect('html-page-context', html.add_link)

import hack; app.connect('doctree-resolved', hack.dump_xml)
106 changes: 46 additions & 60 deletions src/hieroglyph/directives.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function

from docutils import nodes

Expand All @@ -10,10 +9,9 @@
from docutils.transforms import Transform


def raiseSkip(self, node):
def raiseSkip(self, node): # Used in __init__: app.add_node
raise SkipNode()

import hack

class if_slides(nodes.Element):
pass
Expand All @@ -36,10 +34,9 @@ def run(self):
node.document = self.state.document
set_source_info(self, node)

node.attributes['ifslides'] = self.name in ('slides', 'ifslides',)
node.attributes['ifslides'] = self.name in ('slides', 'ifslides')

self.state.nested_parse(self.content, self.content_offset,
node, match_titles=1)
self.state.nested_parse(self.content, self.content_offset, node, match_titles=1)
return [node]


Expand Down Expand Up @@ -103,18 +100,16 @@ class TransformNextSlides(Transform):

default_priority = 550

@hack.TRACE
def apply(self, *args, **kwargs):

app = self.document.settings.env.app

from hieroglyph import builder

app = self.document.settings.env.app
is_slides = builder.building_slides(app)

return self.apply_to_document(self.document, env=self.document.settings.env, building_slides=is_slides)
self.apply_to_document(self.document, env=self.document.settings.env, building_slides=is_slides)


@hack.TRACE
def apply_to_document(self, document, env, building_slides):
need_reread = False

Expand All @@ -125,83 +120,76 @@ def apply_to_document(self, document, env, building_slides):
if need_reread:
env.note_reread()

@hack.TRACE
def _find_last_slide(self, node):
# Find all ancestors, including the doctree (dads[-1])
ancestors, n = [], node
while n is not None:
ancestors.append(n)
n = n.parent
ancestors.reverse()
doctree = ancestors[0]

if not slideconf.get_conf(self.document.settings.env.app.builder, doctree)['autoslides']:
return node.parent
#else it's one for the ancestors

auto_level = slideconf.get_conf(self.document.settings.env.app.builder, doctree)['slide_levels']
my_level = len(ancestors) -1 # Don't count the doctree
level = auto_level if my_level > auto_level else my_level-1

return ancestors[level]


def _make_title_node(self, node, increment=True):
"""Generate a new title node for ``node``.
``node`` is a ``nextslide`` node. The title will use the node's
parent's title, or the title specified as an argument.
"""

parent_title_node = node.parent.next_node(nodes.title)
nextslide_info = getattr(
parent_title_node, 'nextslide_info',
(parent_title_node.deepcopy().children, 1),
)
nextslide_info = (
nextslide_info[0],
nextslide_info[1] + 1,
)
ancestor = self._find_last_slide(node)
parent_title_node = ancestor.next_node(nodes.title)

nextslide_info = getattr(parent_title_node, 'nextslide_info', (parent_title_node.deepcopy().children, 1))
nextslide_info = (nextslide_info[0], nextslide_info[1] + 1)

if node.args:
textnodes, messages = node.state.inline_text(
node.args[0],
1,
)
textnodes, messages = node.state.inline_text(node.args[0], 1)
new_title = nodes.title(node.args[0], '', *textnodes)

else:

title_nodes = nextslide_info[0][:]

if 'increment' in node.attributes:
title_nodes.append(
nodes.Text(' (%s)' % nextslide_info[1])
)

new_title = nodes.title(
'', '',
*title_nodes
)
title_nodes.append(nodes.Text(' (%s)' % nextslide_info[1]))
new_title = nodes.title('', '', *title_nodes)

new_title.nextslide_info = nextslide_info
return new_title

@hack.TRACE

def visit_nextslide(self, node, building_slides):

index = node.parent.index(node)

if not building_slides or not node.parent.children[index+1:]:
if not building_slides or not node.parent.children[index+1:]: # Just delete the directive-node, when not building slides or no content
node.parent.replace(node, [])
# nothing else to do
return

if True:
print('XXX fore-fathers of node:', node)
n = node
while n is not None:
print('XXX \t', n, "--", str(n)[:40]+'...', "class:", type(n))
n = n.parent
doctree = n

auto_level=slideconf.get_conf(self.document.settings.env.app.builder, doctree)['slide_levels']
print('XXX auto_level:', auto_level)
#else

# figure out where to hoist the subsequent content to
ancestor = self._find_last_slide(node)
forefather = ancestor.parent
insertion_point = forefather.index(ancestor) + 1

# truncate siblings, storing a reference to the rest of the content
parent = node.parent
grandparent = node.parent.parent
insertion_point = grandparent.index(node.parent) + 1

# truncate siblings, storing a reference to the rest of the
# content
# truncate siblings, storing a reference to the rest of the content
new_children = parent.children[index+1:]
parent.children = parent.children[:index+1]

# create the next section
new_section = nodes.section()
new_section += self._make_title_node(node) # XXX
new_section += self._make_title_node(node)
new_section.extend(new_children)
self.document.set_id(new_section)

Expand All @@ -210,7 +198,7 @@ def visit_nextslide(self, node, building_slides):
new_section['classes'].extend(node.get('classes'))

# attach the section and delete the nextslide node
grandparent.insert(insertion_point, new_section)
forefather.insert(insertion_point, new_section)
del node.parent[index]


Expand Down Expand Up @@ -317,9 +305,7 @@ def filter_doctree_for_slides(doctree):
while current < num_children:

child = doctree.children[current]
child.replace_self(
child.traverse(no_autoslides_filter)
)
child.replace_self(child.traverse(no_autoslides_filter))

if len(doctree.children) == num_children:
# nothing removed, increment current
Expand Down
22 changes: 5 additions & 17 deletions src/hieroglyph/writer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"""Writer Support for Hieroglyph Slides."""

from __future__ import print_function

from docutils import nodes
from docutils.writers.html4css1 import HTMLTranslator as BaseTranslator
from sphinx.writers.html import HTMLTranslator
from hieroglyph import html
from hieroglyph.directives import slideconf

import hack

class SlideData(object):

Expand Down Expand Up @@ -63,13 +60,11 @@ def __init__(self, *args, **kwargs):
self.current_slide = None


#@hack.SHOW_stack
def push_body(self):
"""Push the current body onto the stack and create an empty one."""
self._body_stack.append(self.body)
self.body = []

#@hack.SHOW_stack
def pop_body(self):
"""Replace the current body with the last pushed one. And return the popped one."""
body= self.body
Expand All @@ -82,7 +77,7 @@ def visit_slideconf(self, node):
def depart_slideconf(self, node):
pass

#@hack.TRACE

def slide_start(self, node):
"""A pseudo visitor to start (and end) a slide"""

Expand All @@ -99,11 +94,9 @@ def slide_start(self, node):
ids =str(abs(hash(node))) # Just some string-value

self.push_body() # Save old data and collect all upto slide_end
self.current_slide = SlideData(translator=self, id=ids,
level=self.section_level, slide_number=self.slide_number,
classes=classes)
self.current_slide = SlideData(translator=self, id=ids, level=self.section_level, slide_number=self.slide_number, classes=classes)


#@hack.TRACE
def slide_end(self, node):
"""A pseudo visitor to (start and) end a slide"""

Expand Down Expand Up @@ -168,7 +161,6 @@ def visit_block_quote(self, node):

class SlideTranslator(BaseSlideTranslator):

#@hack.TRACE
def visit_section(self, node):
# Increase the section_level, and 'maybe' start a new slide
self.section_level += 1
Expand All @@ -177,7 +169,7 @@ def visit_section(self, node):
def depart_section(self, node):
self.section_level -= 1

#@hack.TRACE

def _maybe_new_slide(self, node):
"""Determine whether a slide-transition is needed, and insert it when needed.
Also return True/False depending on that need."""
Expand All @@ -200,12 +192,9 @@ def _maybe_new_slide(self, node):
return False


#@hack.TRACE
def visit_document(self, node): # Only to TRACE it
BaseSlideTranslator.visit_document(self, node)


#@hack.TRACE
def depart_document(self, node):
if getattr(self, 'slide_open', False): #Close the last slide
self.slide_end(node)
Expand Down Expand Up @@ -240,14 +229,13 @@ def visit_start_of_file(self, node):

BaseSlideTranslator.visit_start_of_file(self, node)

#@hack.TRACE

def visit_slide(self, node):
if getattr(self, 'slide_open', False): # end an "auto slide"
self.slide_end(node)
self.slide_start(node);
self.slide_open=True

#@hack.TRACE
def depart_slide(self, node):
self.slide_end(node);
self.slide_open=False
Expand Down

0 comments on commit 2375e8f

Please sign in to comment.