|
|
@ -21,14 +21,20 @@ module Bristlecode
|
|
|
|
rule(:italic_close) { str('[/i]') | str('[/I]') | eof }
|
|
|
|
rule(:italic_close) { str('[/i]') | str('[/I]') | eof }
|
|
|
|
rule(:italic) { italic_open >> children.as(:italic) >> italic_close }
|
|
|
|
rule(:italic) { italic_open >> children.as(:italic) >> italic_close }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rule(:url_open) { str('[url]') }
|
|
|
|
|
|
|
|
rule(:url_close) { str('[/url]') | eof }
|
|
|
|
|
|
|
|
rule(:simple_href) { (url_close.absent? >> any).repeat }
|
|
|
|
|
|
|
|
rule(:simple_url) { url_open >> simple_href.as(:href) >> url_close }
|
|
|
|
|
|
|
|
rule(:url) { simple_url.as(:url) }
|
|
|
|
|
|
|
|
|
|
|
|
rule(:eof) { any.absent? }
|
|
|
|
rule(:eof) { any.absent? }
|
|
|
|
rule(:tag) { bold | italic }
|
|
|
|
rule(:tag) { bold | italic | url }
|
|
|
|
rule(:elem) { text | tag }
|
|
|
|
rule(:elem) { text.as(:text) | tag }
|
|
|
|
rule(:tag_open) { bold_open | italic_open }
|
|
|
|
rule(:tag_open) { bold_open | italic_open | url_open }
|
|
|
|
rule(:tag_close) { bold_close | italic_close }
|
|
|
|
rule(:tag_close) { bold_close | italic_close | url_close }
|
|
|
|
rule(:tag_delim) { tag_open | tag_close }
|
|
|
|
rule(:tag_delim) { tag_open | tag_close }
|
|
|
|
|
|
|
|
|
|
|
|
rule(:text) { (tag_delim.absent? >> any).repeat(1).as(:text) }
|
|
|
|
rule(:text) { (tag_delim.absent? >> any).repeat(1) }
|
|
|
|
rule(:children) { space? >> elem.repeat }
|
|
|
|
rule(:children) { space? >> elem.repeat }
|
|
|
|
rule(:doc) { space? >> elem.repeat.as(:doc) }
|
|
|
|
rule(:doc) { space? >> elem.repeat.as(:doc) }
|
|
|
|
root(:doc)
|
|
|
|
root(:doc)
|
|
|
@ -39,55 +45,77 @@ module Bristlecode
|
|
|
|
rule(italic: sequence(:children)) { Italic.new(children) }
|
|
|
|
rule(italic: sequence(:children)) { Italic.new(children) }
|
|
|
|
rule(text: simple(:text)) { Text.new(text) }
|
|
|
|
rule(text: simple(:text)) { Text.new(text) }
|
|
|
|
rule(doc: subtree(:doc)) { Doc.new(doc) }
|
|
|
|
rule(doc: subtree(:doc)) { Doc.new(doc) }
|
|
|
|
|
|
|
|
rule(url: subtree(:url)) { Url.new(url) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Doc
|
|
|
|
class Doc
|
|
|
|
|
|
|
|
attr_accessor :children
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(children)
|
|
|
|
def initialize(children)
|
|
|
|
@children = children
|
|
|
|
self.children = children
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
def to_html
|
|
|
|
s = StringIO.new
|
|
|
|
s = StringIO.new
|
|
|
|
@children.each{|child| s << child.to_html }
|
|
|
|
children.each{|child| s << child.to_html }
|
|
|
|
s.string
|
|
|
|
s.string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Text
|
|
|
|
class Text
|
|
|
|
|
|
|
|
attr_accessor :text
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(text)
|
|
|
|
def initialize(text)
|
|
|
|
@text = text.to_str.strip
|
|
|
|
self.text = text.to_str.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
def to_html
|
|
|
|
@text
|
|
|
|
text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Bold
|
|
|
|
class Bold
|
|
|
|
|
|
|
|
attr_accessor :children
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(children)
|
|
|
|
def initialize(children)
|
|
|
|
@children = children
|
|
|
|
self.children = children
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
def to_html
|
|
|
|
s = StringIO.new
|
|
|
|
s = StringIO.new
|
|
|
|
s << "<b>"
|
|
|
|
s << "<b>"
|
|
|
|
@children.each{|child| s << child.to_html }
|
|
|
|
children.each{|child| s << child.to_html }
|
|
|
|
s << "</b>"
|
|
|
|
s << "</b>"
|
|
|
|
s.string
|
|
|
|
s.string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Italic
|
|
|
|
class Italic
|
|
|
|
|
|
|
|
attr_accessor :children
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(children)
|
|
|
|
def initialize(children)
|
|
|
|
@children = children
|
|
|
|
self.children = children
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
def to_html
|
|
|
|
s = StringIO.new
|
|
|
|
s = StringIO.new
|
|
|
|
s << "<i>"
|
|
|
|
s << "<i>"
|
|
|
|
@children.each{|child| s << child.to_html }
|
|
|
|
children.each{|child| s << child.to_html }
|
|
|
|
s << "</i>"
|
|
|
|
s << "</i>"
|
|
|
|
s.string
|
|
|
|
s.string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Url
|
|
|
|
|
|
|
|
attr_accessor :href, :title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(args)
|
|
|
|
|
|
|
|
self.href = args[:href].to_str.strip
|
|
|
|
|
|
|
|
self.title = args[:title] || href
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
|
|
|
|
"<a href=\"#{href}\">#{title}</a>"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|