Commits

Peter Ward committed 82bd331 Merge

merged

Comments (0)

Files changed (6)

docs/handlers.rst

 Handlers
-~~~~~~~~
+========
 Handlers must be an object which specifies a ``layout`` method which takes a
 theme object, which must have a ``templates`` attribute. If none of the required
 templates are given in the ``templates`` attribute, then the handler should
 .. automodule:: shinypress.handler
     :members:
 
+Documentation for specific handlers follows:
+
+Songs/Lyrics
+~~~~~~~~~~~~
+This handler is for songs/lyrics.
+
+.. automodule:: shinypress.songs
+    :members:

shinypress/handler.py

         """
         raise NotImplemented
 
+class CannotRender(Exception):
+    """
+    Generic error for handlers to raise.
+    """
+    pass

shinypress/songs/__init__.py

 from itertools import groupby
 import json
 
-import ..handler.Handler as BaseHandler
-from ..handler import CannotRender
+from shinypress.handler import Handler as BaseHandler
+from shinypress.handler import CannotRender
 
-from .old_format import parse as old_parse
-from .old_format import dumps as old_dumps
-from .old_format import Verse
-from .utils import flatten, uniqc
+from shinypress.songs.old_format import parse as old_parse
+from shinypress.songs.old_format import dumps as old_dumps
+from shinypress.songs.old_format import Verse
+from shinypress.songs.utils import flatten, uniqc
 
 class Song(BaseHandler):
     """
     A representation of a song.
     """
-    
     info = {}
     verses = {}
     ordering = {}
 
-
-
     templates = [
-            ("song-single",) # A single slide with a title, main text and info box
+            ("song-single",), # A single slide with a title, main text and info box
             (
                 "title-text", # Slide with title and main text
                 "text", # Slide with only main text
                 self.ordering[ordering_name].append(verseid)
 
     def to_old_format(self, f, ordering_name=""):
-        f.write(old_dumps(self.info, Verse(
+        f.write(old_dumps(self.info, (Verse(
                 self.verses[verse]["title"],
                 self.verses[verse]["based-on"],
                 self.verses[verse]["markup"],
                 n_repeats
-            ) for verse in uniqc(flatten(self.ordering[ordering_name]))))
+            ) for verse in uniqc(flatten(self.ordering[ordering_name])))))
+
+def old_to_new(old_song_file, new_json_file):
+    """
+    Converts a old song format file to new json format file.
+
+    :param old_song_file: file object in old song format
+    :param new_json_file: file object to write json to
+    """
+    new_json_file.write(Song().from_old_format(old_song_file).to_json())

shinypress/songs/old_format.py

 from collections import namedtuple
 import re
 
-from utils import group_paragraphs
+try:
+    from cStringIO import StringIO
+except ImportError:
+    from StringIO import StringIO
+
+import itertools
 
 VERSE_REFERENCE = re.compile(r'^\((.*)\)(?: x([0-9]+))?$')
 VERSE_LABEL = re.compile(r'^(.*?[^:])(?: \((.*?)\))?:$')
 
     return '\n'.join(output)
 
+def group_paragraphs(lines):
+    """
+    Group an iterable by groups of non-empty lines.
+
+    Warning: the iterables this returns must be used in order,
+    otherwise badness will ensue.
+    """
+
+    # number of the current paragraph
+    i = [0]
+    up_to = lambda j: i[0] == j
+    finished = lambda: up_to(-1)
+    def next_paragraph():
+        i[0] += 1
+    def finish():
+        i[0] = -1
+
+    def make_iter(j):
+        while True:
+            if finished():
+                break
+
+            if not up_to(j):
+                raise ValueError, "iterators must be used in order"
+
+            try:
+                line = lines.next()
+            except StopIteration:
+                finish()
+                return
+
+            if not line:
+                break
+
+            yield line
+
+        next_paragraph()
+
+    for j in itertools.count():
+        yield make_iter(j)
+        if finished():
+            break

shinypress/songs/utils.py

 # -*- coding: utf-8 -*-
 
-from ..text.mulitple import GreedyDualSize
-
-from . import Song
-
-def old_to_new(old_song_file, new_json_file):
-    """
-    Converts a old song format file to new json format file.
-
-    :param old_song_file: file object in old song format
-    :param new_json_file: file object to write json to
-    """
-    new_json_file.write(Song().from_old_format(old_song_file).to_json())
-
-
 def flatten(x):
     """
     From http://stackoverflow.com/a/2158522/1306020

shinypress/utils.py

-try:
-    from cStringIO import StringIO
-except ImportError:
-    from StringIO import StringIO
-
-import itertools
-
-def group_paragraphs(lines):
-    """
-    Group an iterable by groups of non-empty lines.
-
-    Warning: the iterables this returns must be used in order,
-    otherwise badness will ensue.
-    """
-
-    # number of the current paragraph
-    i = [0]
-    up_to = lambda j: i[0] == j
-    finished = lambda: up_to(-1)
-    def next_paragraph():
-        i[0] += 1
-    def finish():
-        i[0] = -1
-
-    def make_iter(j):
-        while True:
-            if finished():
-                break
-
-            if not up_to(j):
-                raise ValueError, "iterators must be used in order"
-
-            try:
-                line = lines.next()
-            except StopIteration:
-                finish()
-                return
-
-            if not line:
-                break
-
-            yield line
-
-        next_paragraph()
-
-    for j in itertools.count():
-        yield make_iter(j)
-        if finished():
-            break
-