Skip to content

Commit

Permalink
BUSY. cleanup writer.py: removed dead code, rename/code slider 'visto…
Browse files Browse the repository at this point in the history
…r'. Using hardcode style (TEMP) again/ Hplus-bug (nyergler#100) is gone; but slides2 (gio) and other will not work. Result is not perfect. non-slides sections are gone (instead of div). extra span for IDS are solved to
  • Loading branch information
Albert Mietus authored and Albert Mietus committed Jul 14, 2015
1 parent b9e1ecf commit a8c3627
Showing 1 changed file with 67 additions and 159 deletions.
226 changes: 67 additions & 159 deletions src/hieroglyph/writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Writer Support for Hieroglyph Slides."""

from __future__ import print_function

from docutils import nodes
from sphinx.locale import _
from docutils.writers.html4css1 import HTMLTranslator as BaseTranslator
Expand All @@ -11,40 +13,11 @@
slideconf,
)

import hack

def depart_title(self, node):

# XXX Because we want to inject our link into the title, this is
# largely copy-pasta'd from sphinx.html.writers.HtmlTranslator.

close_tag = self.context[-1]

if (self.permalink_text and self.builder.add_permalinks and
node.parent.hasattr('ids') and node.parent['ids']):
aname = node.parent['ids'][0]

if close_tag.startswith('</a></h'):
self.body.append('</a>')

self.body.append(u'<a class="headerlink" href="#%s" ' % aname +
u'title="%s">%s</a>' % (
_('Permalink to this headline'),
self.permalink_text))

self.body.append(
u'<a class="headerlink" href="%s#%s" ' % (
html.slide_path(self.builder),
aname,
) +
u'title="%s">%s' % (
_('Slides'),
self.builder.app.config.slide_html_slide_link_symbol,
))

if not close_tag.startswith('</a></h'):
self.body.append('</a>')

BaseTranslator.depart_title(self, node)
def depart_title(self, node):
assert False, "The removed file-level function 'writer.depart_title' is called"


class SlideData(object):
Expand Down Expand Up @@ -80,7 +53,7 @@ def _filter_classes(self, include=None, exclude=None):

return classes

def get_slide_context(self):
def _get_slide_context(self):
"""Return the context dict for rendering this slide."""

return {
Expand All @@ -98,6 +71,7 @@ def get_slide_context(self):

class BaseSlideTranslator(HTMLTranslator):

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

HTMLTranslator.__init__(self, *args, **kwargs)
Expand All @@ -124,134 +98,46 @@ def visit_slideconf(self, node):
def depart_slideconf(self, node):
pass

def _add_slide_number(self, slide_no):
"""Add the slide number to the output if enabled."""

if self.builder.config.slide_numbers:
self.body.append(
'\n<div class="slide-no">%s</div>\n' % (slide_no,),
)

def _add_slide_footer(self, slide_no):
"""Add the slide footer to the output if enabled."""

if self.builder.config.slide_footer:
self.body.append(
'\n<div class="slide-footer">%s</div>\n' % (
self.builder.config.slide_footer,
),
)
@hack.TRACE
def slide_start(self, node): #GAM: renamed
"""A pseudo visitor for slides; as `slides` isn't a docutils node, it can't be visited. It
is called in the writer to start a (new) slide. Typically on a new section."""
# NOTE: for this to work 100%, the slide directive has to be changed to work to
# That directive is not creating a slide tag ... (no really valid)
# Make it work like the nextslide directive, that one is OL

def visit_slide(self, node):
self.body.append(self.starttag(node, 'article', CLASS='slide level-%i' % self.section_level, ids=node.get('ids')))

from hieroglyph import builder
@hack.TRACE
def slide_end(self, node): #GAM: renamed
## """Also see `slide_start; this is the matching` pseudo depart for slides. Always called by a writer"""
self.body.append('</article>\n\n')

slide_level = node.attributes.get('level', self.section_level)
## self.current_slide.content = ''.join(self.body)
## self.pop_body()
## rendered_slide = self.builder.templates.render('slide.html',
## self.current_slide.get_slide_context())
## self.body.append(rendered_slide)
## self.slide_data.append(self.current_slide)
## self.current_slide = None

if slide_level > self.builder.config.slide_levels:
# dummy for matching div's
self.body.append(
self.starttag(
node, 'div', CLASS='section level-%s' % slide_level)
)
node.tag_name = 'div'
else:

slide_conf = slideconf.get_conf(self.builder, node.document)
if (builder.building_slides(self.builder.app) and
slide_conf['autoslides'] and
isinstance(node.parent, nodes.section) and
not getattr(node.parent, 'closed', False)):

# we're building slides and creating slides from
# sections; close the previous section, if needed
self.depart_slide(node.parent)

# don't increment section_count until we've (potentially)
# closed the previous slide
self.section_count += 1

node.closed = False

classes = node.get('classes')
if not classes:
classes = slide_conf['slide_classes']

# self.body.append(
# self.starttag(
# node, 'article',
# CLASS='%s slide level-%s' % (
# ' '.join(classes),
# slide_level,
# ),
# )
# )
node.tag_name = 'article'

slide_id = node.get('ids')
if slide_id:
slide_id = slide_id[0]
else:
slide_id = ''

assert self.current_slide is None
self.current_slide = SlideData(
self,
id=slide_id,
level=slide_level,
classes=classes,
slide_number=self.section_count,
)
self.push_body()

def depart_slide(self, node):

if self.current_slide and not getattr(node, 'closed', False):

# mark the slide closed
node.closed = True

# self._add_slide_footer(self.section_count)
# self._add_slide_number(self.section_count)
# self.body.append(
# '\n</%s>\n' % getattr(node, 'tag_name', 'article')
# )

self.current_slide.content = ''.join(self.body)
self.pop_body()
rendered_slide = self.builder.templates.render(
'slide.html',
self.current_slide.get_slide_context(),
)
self.body.append(rendered_slide)
self.slide_data.append(self.current_slide)
self.current_slide = None

#@hack.TRACE
def visit_title(self, node):

self.push_body()

if (isinstance(node.parent, slide) or
node.parent.attributes.get('include-as-slide', False)):
slide_level = node.parent.attributes.get(
'level',
self.section_level)
level = max(
slide_level + self.initial_header_level - 1,
1,
)
self.current_slide.level = level

# tag = 'h%s' % level
# self.body.append(self.starttag(node, tag, ''))
# self.context.append('</%s>\n' % tag)
if (isinstance(node.parent, slide) or node.parent.attributes.get('include-as-slide', False)):
slide_level = node.parent.attributes.get('level', self.section_level)
self.current_slide.level = max(slide_level + self.initial_header_level - 1, 1)

if self.current_slide and isinstance(node.parent, (nodes.section, slide)):
self.current_slide.title = node.astext().strip()
else:
HTMLTranslator.visit_title(self, node)


#@hack.TRACE
def depart_title(self, node):

if self.current_slide and isinstance(node.parent, (nodes.section, slide)):
Expand Down Expand Up @@ -298,29 +184,50 @@ def visit_block_quote(self, node):

class SlideTranslator(BaseSlideTranslator):

@hack.TRACE
def visit_section(self, node):
# Just increase the section_level, and 'maybe' start a new slide

# XXX: We're actually removing content that's not in slide
# nodes with autoslides is false, so it's not clear that we
# even need this guard.
if (slideconf.get_conf(self.builder, node.document)['autoslides'] or
node.attributes.get('include-as-slide', False)):
self.section_level += 1
self._maybe_new_slide(node) # we might need a new slide (stop one and start one)

self.section_level += 1
return self.visit_slide(node)

def depart_section(self, node):
# Mostly, copied from `docutils.writers.html4css1.HTMLTranslator; but changed the tag. After 'maybe' switching slided

self.section_level -= 1

if (slideconf.get_conf(self.builder, node.document)['autoslides'] or
node.attributes.get('include-as-slide', False)):

if self.section_level > self.builder.config.slide_levels:
self.body.append('</div>')
else:
self.depart_slide(node)
@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."""

if not slideconf.get_conf(self.builder, node.document)['autoslides']:
return False
# else

slide_levels = self.builder.config.slide_levels

if self.section_level <= slide_levels:
if getattr(self, 'slide_open', False): # This will skip ending before the first open
self.slide_end(node)

self.slide_start(node)
self.slide_open=True

return True
else:
return False

self.section_level -= 1
@hack.TRACE
def depart_document(self, node):
#Close the last slide
if getattr(self, 'slide_open', False):
self.slide_end(node)
BaseSlideTranslator.depart_document(self, node)

#@hack.TRACE
def depart_title(self, node):

if node.parent.hasattr('ids') and node.parent['ids']:
Expand All @@ -339,6 +246,7 @@ def depart_title(self, node):

BaseSlideTranslator.depart_title(self, node)

#@hack.TRACE
def visit_start_of_file(self, node):
previous = node.parent
if isinstance(previous, nodes.compound):
Expand Down

0 comments on commit a8c3627

Please sign in to comment.