Skip to content

Commit

Permalink
Handle ##. sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
gigamonkey committed Apr 15, 2012
1 parent 835b94a commit 7cb82ce
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 4 deletions.
83 changes: 79 additions & 4 deletions markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,14 @@ def initialize(markup, brace_is_eof=false)

class DocumentParser < Parser

def initialize(markup, subdoc=false)
def initialize(markup, subdoc=false, section=false)
super(markup, subdoc)
@subdoc = subdoc
@section = section
end

def grok(token)
what, extra = token.value

case what
case token.value
when :blank, :newline
#raise "Parse error #{token}"
when "*"
Expand All @@ -299,6 +298,17 @@ def grok(token)
@markup.close_element(@subdoc, token)
@markup.pop_parser
end
when '#'
if not @section
# This is the top level document parser so this may be the
# beginning of a section.
@markup.push_parser(SectionStartParser.new(@markup, token))
else
# This is a nested section parser so this may be the end of
# the section.
@markup.push_parser(SectionEndParser.new(@markup, token, @subdoc))
end

else
p = @markup.open_element(:p)
@markup.push_parser(ParagraphParser.new(@markup, p, @brace_is_eof))
Expand Down Expand Up @@ -334,6 +344,49 @@ def grok(token)
end
end

class SectionStartParser < Parser

def initialize(markup, token)
super(markup)
@tokens = [ token ]
end

def grok(token)
if @tokens.length == 1 and token.value == '#'
@tokens << token
elsif @tokens.length == 2 and token.value == ' '
@markup.pop_parser
@markup.push_parser(SectionNameParser.new(@markup))
else
raise "Parser error: #{token}"
end
end
end

class SectionEndParser < Parser

def initialize(markup, token, section)
super(markup)
@tokens = [ token ]
@section = section
end

def grok(token)
if @tokens.length == 1 and token.value == '#'
@tokens << token
elsif @tokens.length == 2 and token.value == '.'
@tokens << token
elsif @tokens.length == 3 and token.value == :blank
@markup.pop_parser # this one
@markup.pop_parser # document parser
@markup.close_element(@section)
else
raise "Parser error: #{token}"
end
end
end


class NameParser < Parser

def initialize(markup)
Expand All @@ -359,6 +412,28 @@ def grok(token)
end
end

class SectionNameParser < Parser

def initialize(markup)
super(markup)
@name = ''
end

def grok(token)
case token.value
when :blank
@markup.pop_parser
tag = @name.to_sym
e = @markup.open_element(tag)
@markup.push_parser(DocumentParser.new(@markup, e, true))
else
# FIXME: should check that we only get legal name characters.
@name << token.value
end
end
end


class BraceDelimetedParser < Parser

def initialize(markup, element)
Expand Down
1 change: 1 addition & 0 deletions tests/test_44.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["body",["p","Regular paragraph."],["foo",["p","A first foo paragraph."],["p","B second foo paragraph."]],["p","Last regular paragraph."]]
11 changes: 11 additions & 0 deletions tests/test_44.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Regular paragraph.

## foo

A first foo paragraph.

B second foo paragraph.

##.

Last regular paragraph.
1 change: 1 addition & 0 deletions tests/test_45.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["body",["p","Regular paragraph."],["foo",["pre","Verbatim stuff. Biff ##. Bam boom! < \\{"]],["p","Last regular paragraph."]]
9 changes: 9 additions & 0 deletions tests/test_45.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Regular paragraph.

## foo

Verbatim stuff. Biff ##. Bam boom! < \{

##.

Last regular paragraph.
1 change: 1 addition & 0 deletions tests/test_46.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["body",["p","Regular paragraph."],["foo",["h1","A header in foo"],["p","A first foo paragraph."],["p","B second foo paragraph."]],["p","Last regular paragraph."]]
13 changes: 13 additions & 0 deletions tests/test_46.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Regular paragraph.

## foo

* A header in foo

A first foo paragraph.

B second foo paragraph.

##.

Last regular paragraph.
1 change: 1 addition & 0 deletions tests/test_47.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["body",["p","Regular paragraph."],["foo",["p","A first foo paragraph.",["note",["p","This is a subdocument."],["bar",["p","With it's own multiline section."]],["p","And some more stuff."]]," Blah"],["p","B second foo paragraph."]],["p","Last regular paragraph."]]
21 changes: 21 additions & 0 deletions tests/test_47.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Regular paragraph.

## foo

A first foo paragraph.\note{This is a subdocument.

## bar

With it's own multiline section.

##.

And some more stuff.

} Blah

B second foo paragraph.

##.

Last regular paragraph.
1 change: 1 addition & 0 deletions tests/test_48.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["body",["p","Regular paragraph."],["foo",["pre","Verbatim section\n\n##."],["p","More foo"]],["p","Last regular paragraph."]]
13 changes: 13 additions & 0 deletions tests/test_48.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Regular paragraph.

## foo

Verbatim section

##.

More foo

##.

Last regular paragraph.

0 comments on commit 7cb82ce

Please sign in to comment.