aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Vilcans <martin@librador.com>2012-03-12 23:24:32 +0100
committerMartin Vilcans <martin@librador.com>2012-03-12 23:24:32 +0100
commitac4bc25392d5e406e9f2232982fb147ce6efa845 (patch)
tree579312983b6037dc1d80d072e0662c24335b2337
parentebed2982048d1ded7c39dae5cf20944b299e9f45 (diff)
downloadscreenplain-ac4bc25392d5e406e9f2232982fb147ce6efa845.tar.gz
Support mixed sections and synopsises
-rw-r--r--screenplain/parsers/fountain.py23
-rw-r--r--screenplain/types.py15
-rw-r--r--tests/fountain_test.py10
3 files changed, 39 insertions, 9 deletions
diff --git a/screenplain/parsers/fountain.py b/screenplain/parsers/fountain.py
index 2b1c95c..f7093cb 100644
--- a/screenplain/parsers/fountain.py
+++ b/screenplain/parsers/fountain.py
@@ -55,7 +55,7 @@ class InputParagraph(object):
"""
(
self.append_synopsis(previous_paragraphs) or
- self.append_section(previous_paragraphs) or
+ self.append_sections_and_synopsises(previous_paragraphs) or
self.append_slug(previous_paragraphs) or
self.append_centered_action(previous_paragraphs) or
self.append_dialog(previous_paragraphs) or
@@ -84,17 +84,26 @@ class InputParagraph(object):
paragraphs.append(Slug(_to_rich(text)))
return True
- def append_section(self, paragraphs):
- sections = []
+ def append_sections_and_synopsises(self, paragraphs):
+
+ new_paragraphs = []
for line in self.lines:
match = section_re.match(line)
- if not match:
+ if match:
+ hashes, text = match.groups()
+ section = Section(_to_rich(text), len(hashes))
+ new_paragraphs.append(section)
+ elif (
+ line.startswith('=') and
+ new_paragraphs and
+ hasattr(new_paragraphs[-1], 'set_synopsis')
+ ):
+ new_paragraphs[-1].set_synopsis(line[1:].lstrip())
+ else:
return False
- hashes, text = match.groups()
- sections.append(Section(_to_rich(text), len(hashes)))
- paragraphs += sections
+ paragraphs += new_paragraphs
return True
def append_centered_action(self, paragraphs):
diff --git a/screenplain/types.py b/screenplain/types.py
index 92ff174..a73e92b 100644
--- a/screenplain/types.py
+++ b/screenplain/types.py
@@ -27,14 +27,25 @@ class Slug(object):
class Section(object):
"""A section heading."""
- def __init__(self, text, level):
+
+ def __init__(self, text, level, synopsis=None):
self.text = text
self.level = level
- self.synopsis = None
+ self.synopsis = synopsis
def set_synopsis(self, text):
self.synopsis = text
+ def __repr__(self):
+ return 'Section(%r, %r, %r)' % (self.text, self.level, self.synopsis)
+
+ def __eq__(self, other):
+ return (
+ self.text == other.text and
+ self.level == other.level and
+ self.synopsis == other.synopsis
+ )
+
class Dialog(object):
def __init__(self, character, lines):
diff --git a/tests/fountain_test.py b/tests/fountain_test.py
index 69426db..47bd270 100644
--- a/tests/fountain_test.py
+++ b/tests/fountain_test.py
@@ -113,6 +113,16 @@ class SectionTests(unittest2.TestCase):
self.assertEquals(1, paras[2].level)
self.assertEquals(plain('first level again'), paras[2].text)
+ def test_multiple_sections_with_synopsis(self):
+ paras = parse([
+ '# first level',
+ '= level one synopsis',
+ '## second level',
+ ])
+ self.assertEquals([
+ Section(plain(u'first level'), 1, 'level one synopsis'),
+ Section(plain(u'second level'), 2, None),
+ ], paras)
class DialogTests(unittest2.TestCase):
# A Character element is any line entirely in caps, with one empty