From 6d19935a36d8a93a48204bd68ed639a20af5be04 Mon Sep 17 00:00:00 2001 From: ksaga Date: Tue, 10 Jan 2017 00:45:36 +0900 Subject: [PATCH 1/2] keep_line_break option --- lib/creole/parser.rb | 12 +++++++++++- test/parser_test.rb | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/creole/parser.rb b/lib/creole/parser.rb index 7e06efe..1b7e727 100644 --- a/lib/creole/parser.rb +++ b/lib/creole/parser.rb @@ -53,11 +53,19 @@ def extensions?; @extensions; end attr_writer :no_escape def no_escape?; @no_escape; end + # Keep line break in

+ # (default: false) + # true: aa\nbb\n -->

aa
bb

+ # false: aa\nbb\n -->

aa bb

+ 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 @@ -98,6 +106,7 @@ def start_tag(tag) end def end_tag + @out.sub!(/\z/, '') if @keep_line_break @out << '' end @@ -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') @@ -374,6 +383,7 @@ def parse_block(str) when /\A([ \t]*\S+.*?)$(\r?\n)?/ start_paragraph parse_inline($1) + @out << '
' if @keep_line_break && @stack.last != 'li' else raise "Parse error at #{str[0,30].inspect}" end diff --git a/test/parser_test.rb b/test/parser_test.rb index 5fbb692..1c52313 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -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 "

This is bold

", "This **is** bold" @@ -664,4 +668,31 @@ def tce(html, creole) tc("

a/b/c

", "[[a/b/c]]") tc("

a/b/c

", "[[a/b/c]]", :no_escape => true) end + + it 'should support keep_line_break' do + tc "

This is my text.

", + "This is my text.", :keep_line_break => true + tc "

This
is
my
text.

", + "This\nis\nmy\ntext.\n", :keep_line_break => true + + tc "

This is
bold

", + "This **is\nbold**", :keep_line_break => true + tc "

This is
italic

", + "This //is\nitalic//", :keep_line_break => true + + tc "", + "* The quick brown\nfox jumps over lazy dog.", :keep_line_break => true + tc "
  1. The quick brown fox jumps over lazy dog.
", + "# The quick brown\nfox jumps over lazy dog.", :keep_line_break => true + + tc "
Hello\nWorld
", + "{{{\nHello\nWorld\n}}}", :keep_line_break => true + tc "

{{{
Hello
}}}

", + " {{{\nHello\n}}}", :keep_line_break => true + tc "

{{{
Hello
}}}

", + "{{{\nHello\n }}}", :keep_line_break => true + + tc "

Hello ~
world

", + "Hello ~\nworld\n", :keep_line_break => true + end end From 229ae4ce9608b829b9c9b35fcee3b1d23347e764 Mon Sep 17 00:00:00 2001 From: ksaga Date: Tue, 10 Jan 2017 20:32:17 +0900 Subject: [PATCH 2/2] fix keep_line_break option in nested tag --- lib/creole/parser.rb | 3 ++- test/parser_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/creole/parser.rb b/lib/creole/parser.rb index 1b7e727..9d7eb5d 100644 --- a/lib/creole/parser.rb +++ b/lib/creole/parser.rb @@ -106,7 +106,7 @@ def start_tag(tag) end def end_tag - @out.sub!(/\z/, '') if @keep_line_break + @out.sub!(/\z/, '') if @keep_line_break && @stack.last == 'p' @out << '' end @@ -379,6 +379,7 @@ def parse_block(str) else start_paragraph parse_inline(line) + @out << '
' if @keep_line_break end when /\A([ \t]*\S+.*?)$(\r?\n)?/ start_paragraph diff --git a/test/parser_test.rb b/test/parser_test.rb index 1c52313..226a823 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -680,6 +680,11 @@ def tce(html, creole) tc "

This is
italic

", "This //is\nitalic//", :keep_line_break => true + tc "

This
is
bold

", + "**This**\nis\n**bold\n**", :keep_line_break => true + tc "

This
is
italic

", + "//This//\nis\n//italic\n//", :keep_line_break => true + tc "
  • The quick brown fox jumps over lazy dog.
", "* The quick brown\nfox jumps over lazy dog.", :keep_line_break => true tc "
  1. The quick brown fox jumps over lazy dog.
",