From 72d1c15695c7341dab203302ea3630f3b0ac65ae Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Sun, 11 Jul 2010 12:37:33 +0200 Subject: [PATCH] Adding escaped characters and tests, small optimization. Escaped characters only apply inside templates. I think this breaks \| outside of templates though. Ugh. --- lib/marker.rb | 2 ++ lib/marker/language.treetop | 20 +++++++++++++------- lib/marker/text.rb | 10 ++++++++++ test/templates_test.rb | 10 ++++++++++ test/test_helper.rb | 2 +- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/marker.rb b/lib/marker.rb index 5273242..2eee7b7 100644 --- a/lib/marker.rb +++ b/lib/marker.rb @@ -22,6 +22,8 @@ require 'marker/language' +$stderr.puts "loading Marker at #{__FILE__}" + #:include:README module Marker class << self diff --git a/lib/marker/language.treetop b/lib/marker/language.treetop index 6da6ae0..b0e0403 100644 --- a/lib/marker/language.treetop +++ b/lib/marker/language.treetop @@ -477,6 +477,8 @@ module Marker / url / + escaped_char + / plain_word / bold_toggle # allow unmatched delimiters after we have ruled out structures @@ -517,13 +519,7 @@ module Marker # end # }} rule plain_word - (!delimiter ![ \t\r\n] .)+ - / - "\\\\" # fixup vim syntax --> " - / - "\\|" - / - "\\=" + (!delimiter [^ \t\r\n])+ end ##### delimiters @@ -551,6 +547,8 @@ module Marker arg_delimiter / term_delimiter + / + escaped_char end rule bold_toggle @@ -659,6 +657,14 @@ module Marker # [A-Z_]+ # end + rule escaped_char + "\\\\" # fix vim highlighting => " + / + "\\|" + / + "\\=" + end + rule number [1-9] digit* { def to_i diff --git a/lib/marker/text.rb b/lib/marker/text.rb index cc96c05..b24d26a 100644 --- a/lib/marker/text.rb +++ b/lib/marker/text.rb @@ -74,6 +74,16 @@ def to_s( options = {} ) end end + class Escaped < ParseNode + def to_html( options = {} ) + text_value.slice(-1,1) + end + + def to_s( options = {} ) + text_value.slice(-1,1) + end + end + class HorizRule < ParseNode def to_html( options = {} ) "
" + diff --git a/test/templates_test.rb b/test/templates_test.rb index 6f001ce..a8829f6 100644 --- a/test/templates_test.rb +++ b/test/templates_test.rb @@ -52,4 +52,14 @@ def test_rendered_args assert_match("

render:template( :html, [\"Example\"], {} )

", markup.to_html) end + def test_escaped_characters + # the single quotes are important here, to make sure the "\" are in the actual text + # this test is messed up right now. we should have a way to send a single + # '\' through, but ruby seems to have errors with it! for now, just removing that case. + text = '{{ template | \| | a \= b | \|a, b\| }}' + markup = Marker.parse text + + assert_match('

render:template( :html, ["|", "a = b", "|a, b|"], {} )

', markup.to_html) + end + end diff --git a/test/test_helper.rb b/test/test_helper.rb index 21a9480..b834213 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,2 +1,2 @@ require 'test/unit' -$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) +$LOAD_PATH.unshift File.expand_path( File.dirname(__FILE__) + '/../lib' )