linebreak

parslet
Jordan Orelli 9 years ago
parent df3b9cb7d0
commit 0ed184e913

@ -17,6 +17,8 @@ module Bristlecode
rule(:bold_close) { str('[/b]') | str('[/B]') | eof } rule(:bold_close) { str('[/b]') | str('[/B]') | eof }
rule(:bold) { bold_open >> children.as(:bold) >> bold_close } rule(:bold) { bold_open >> children.as(:bold) >> bold_close }
rule(:linebreak) { str('[br]').as(:br) }
rule(:italic_open) { str('[i]') | str('[I]') } rule(:italic_open) { str('[i]') | str('[I]') }
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 }
@ -28,11 +30,11 @@ module Bristlecode
rule(:url) { simple_url.as(:url) } rule(:url) { simple_url.as(:url) }
rule(:eof) { any.absent? } rule(:eof) { any.absent? }
rule(:tag) { bold | italic | url } rule(:tag) { bold | italic | url | linebreak }
rule(:elem) { text.as(:text) | tag } rule(:elem) { text.as(:text) | tag }
rule(:tag_open) { bold_open | italic_open | url_open } rule(:tag_open) { bold_open | italic_open | url_open }
rule(:tag_close) { bold_close | italic_close | url_close } rule(:tag_close) { bold_close | italic_close | url_close }
rule(:tag_delim) { tag_open | tag_close } rule(:tag_delim) { tag_open | tag_close | linebreak }
rule(:text) { (tag_delim.absent? >> any).repeat(1) } rule(:text) { (tag_delim.absent? >> any).repeat(1) }
rule(:children) { space? >> elem.repeat } rule(:children) { space? >> elem.repeat }
@ -46,6 +48,7 @@ module Bristlecode
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) } rule(url: subtree(:url)) { Url.new(url) }
rule(br: simple(:br)) { Linebreak.new }
end end
class Doc class Doc
@ -118,4 +121,10 @@ module Bristlecode
"<a href=\"#{href}\">#{title}</a>" "<a href=\"#{href}\">#{title}</a>"
end end
end end
class Linebreak
def to_html
"<br>"
end
end
end end

@ -61,6 +61,10 @@ module Bristlecode
output = '<a href="x[b]y[/b]z">x[b]y[/b]z</a>' output = '<a href="x[b]y[/b]z">x[b]y[/b]z</a>'
expect(to_html(input)).to eq(output) expect(to_html(input)).to eq(output)
end end
it 'renders a linebreak' do
expect(to_html('[br]')).to eq('<br>')
end
end end
describe Parser do describe Parser do
@ -163,5 +167,11 @@ module Bristlecode
expect(parser.url).not_to parse('[url]x[url]y[/url]z[/url]') expect(parser.url).not_to parse('[url]x[url]y[/url]z[/url]')
end end
end end
describe '#linebreak' do
it 'does its thing' do
expect(parser.linebreak).to parse('[br]')
end
end
end end
end end

Loading…
Cancel
Save