Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep line break #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/creole/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@ def extensions?; @extensions; end
attr_writer :no_escape
def no_escape?; @no_escape; end

# Keep line break in <p>
# (default: false)
# true: aa\nbb\n --> <p>aa<br/>bb</p>
# false: aa\nbb\n --> <p>aa bb</p>
attr_writer :keep_line_break
def keep_line_break?; @keep_line_break; end

# Create a new CreoleParser instance.
def initialize(text, options = {})
@allowed_schemes = %w(http https ftp ftps)
@text = text
@extensions = @no_escape = nil
@keep_line_break = false
options.each_pair {|k,v| send("#{k}=", v) }
end

Expand Down Expand Up @@ -98,6 +106,7 @@ def start_tag(tag)
end

def end_tag
@out.sub!(/<br\/?>\z/, '') if @keep_line_break && @stack.last == 'p'
@out << '</' << @stack.pop << '>'
end

Expand All @@ -120,7 +129,7 @@ def end_paragraph

def start_paragraph
if @p
@out << ' ' if @out[-1] != ?\s
@out << ' ' if (!@keep_line_break || @stack.last == 'li') && @out[-1] != ?\s
else
end_paragraph
start_tag('p')
Expand Down Expand Up @@ -370,10 +379,12 @@ def parse_block(str)
else
start_paragraph
parse_inline(line)
@out << '<br/>' if @keep_line_break
end
when /\A([ \t]*\S+.*?)$(\r?\n)?/
start_paragraph
parse_inline($1)
@out << '<br/>' if @keep_line_break && @stack.last != 'li'
else
raise "Parse error at #{str[0,30].inspect}"
end
Expand Down
44 changes: 40 additions & 4 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
require 'creole'

class Bacon::Context
describe Creole::Parser do
def tc(html, creole, options = {})
Creole.creolize(creole, options).should.equal html
if defined? Bacon
Creole.creolize(creole, options).should.equal html
elsif defined? RSpec
expect(Creole.creolize(creole, options)).to eq(html)
else
raise "unkown testing framework"
end
end

def tce(html, creole)
tc(html, creole, :extensions => true)
end
end

describe Creole::Parser do
it 'should parse bold' do
# Creole1.0: Bold can be used inside paragraphs
tc "<p>This <strong>is</strong> bold</p>", "This **is** bold"
Expand Down Expand Up @@ -664,4 +668,36 @@ def tce(html, creole)
tc("<p><a href=\"a%2Fb%2Fc\">a/b/c</a></p>", "[[a/b/c]]")
tc("<p><a href=\"a/b/c\">a/b/c</a></p>", "[[a/b/c]]", :no_escape => true)
end

it 'should support keep_line_break' do
tc "<p>This is my text.</p>",
"This is my text.", :keep_line_break => true
tc "<p>This<br/>is<br/>my<br/>text.</p>",
"This\nis\nmy\ntext.\n", :keep_line_break => true

tc "<p>This <strong>is<br/>bold</strong></p>",
"This **is\nbold**", :keep_line_break => true
tc "<p>This <em>is<br/>italic</em></p>",
"This //is\nitalic//", :keep_line_break => true

tc "<p><strong>This</strong><br/>is<br/><strong>bold<br/></strong></p>",
"**This**\nis\n**bold\n**", :keep_line_break => true
tc "<p><em>This</em><br/>is<br/><em>italic<br/></em></p>",
"//This//\nis\n//italic\n//", :keep_line_break => true

tc "<ul><li>The quick brown fox jumps over lazy dog.</li></ul>",
"* The quick brown\nfox jumps over lazy dog.", :keep_line_break => true
tc "<ol><li>The quick brown fox jumps over lazy dog.</li></ol>",
"# The quick brown\nfox jumps over lazy dog.", :keep_line_break => true

tc "<pre>Hello\nWorld</pre>",
"{{{\nHello\nWorld\n}}}", :keep_line_break => true
tc "<p> {{{<br/>Hello<br/>}}}</p>",
" {{{\nHello\n}}}", :keep_line_break => true
tc "<p>{{{<br/>Hello<br/> }}}</p>",
"{{{\nHello\n }}}", :keep_line_break => true

tc "<p>Hello ~<br/>world</p>",
"Hello ~\nworld\n", :keep_line_break => true
end
end