diff options
author | Martin Vilcans <martin@librador.com> | 2012-03-12 22:04:48 +0100 |
---|---|---|
committer | Martin Vilcans <martin@librador.com> | 2012-03-12 22:04:48 +0100 |
commit | d1b3033b546a1e96f77f87d06fffe1ff36b0b989 (patch) | |
tree | 77303f77945bed263f664b596a1af17af4ea4ab9 | |
parent | dada415e8077f0d6f43ed7d6a31191af69551d0f (diff) | |
download | screenplain-d1b3033b546a1e96f77f87d06fffe1ff36b0b989.tar.gz |
Support sections that are bunched together.
-rw-r--r-- | screenplain/parsers/fountain.py | 20 | ||||
-rw-r--r-- | tests/fountain_test.py | 17 |
2 files changed, 28 insertions, 9 deletions
diff --git a/screenplain/parsers/fountain.py b/screenplain/parsers/fountain.py index 698ede1..986d092 100644 --- a/screenplain/parsers/fountain.py +++ b/screenplain/parsers/fountain.py @@ -55,7 +55,7 @@ class InputParagraph(object): """ previous_paragraphs.append( self.as_synopsis(previous_paragraphs) or - self.as_section() or + self.as_section(previous_paragraphs) or self.as_slug() or self.as_centered_action() or self.as_dialog(previous_paragraphs) or @@ -83,16 +83,18 @@ class InputParagraph(object): else: return Slug(_to_rich(text)) - def as_section(self): - if len(self.lines) != 1: - return None + def as_section(self, previous_paragraphs): + sections = [] - match = section_re.match(self.lines[0]) - if not match: - return None + for line in self.lines: + match = section_re.match(line) + if not match: + return None + hashes, text = match.groups() + sections.append(Section(_to_rich(text), len(hashes))) - hashes, text = match.groups() - return Section(_to_rich(text), len(hashes)) + previous_paragraphs += sections[:-1] + return sections[-1] def as_centered_action(self): if not all(centered_re.match(line) for line in self.lines): diff --git a/tests/fountain_test.py b/tests/fountain_test.py index 5b1d386..69426db 100644 --- a/tests/fountain_test.py +++ b/tests/fountain_test.py @@ -96,6 +96,23 @@ class SectionTests(unittest2.TestCase): self.assertEquals(2, paras[1].level) self.assertEquals(plain('second level'), paras[1].text) + def test_multiple_sections_in_one_paragraph(self): + paras = parse([ + '# first level', + '## second level', + '# first level again' + ]) + self.assertEquals( + [Section, Section, Section], + [type(p) for p in paras] + ) + self.assertEquals(1, paras[0].level) + self.assertEquals(plain('first level'), paras[0].text) + self.assertEquals(2, paras[1].level) + self.assertEquals(plain('second level'), paras[1].text) + self.assertEquals(1, paras[2].level) + self.assertEquals(plain('first level again'), paras[2].text) + class DialogTests(unittest2.TestCase): # A Character element is any line entirely in caps, with one empty |