Skip to content

Commit

Permalink
More fixes to backslash and pipe escaping/unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed May 17, 2010
1 parent 0ca21af commit 7f0533f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion History.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
== In Git

=== New Features
* Table cells can now contain escaped bars - \|. (#48. Gregory Hnatiuk)
* Table cells can now contain escaped bars - \|. (#48. Gregory Hnatiuk, Aslak Hellesøy)
* Luxemburgish (lu) added. (Christoph König)

== 1.0.26 (2010-05-09)
Expand Down
6 changes: 5 additions & 1 deletion features/escaped_pipes.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Feature: Escaped pipes
Scenario: They are the future
Given they have arrived
| \|OK\| | (OK) |
| æ | o |
| a | ø |
Given they have arrived
| æ | \|o |
| \|a | ø\\ |
3 changes: 3 additions & 0 deletions features/step_definitions/eyeball_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Given /^they have arrived$/ do |table|
announce table.raw
end
6 changes: 5 additions & 1 deletion java/src/main/java/gherkin/formatter/PrettyFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ public void step(String keyword, String name, int line) {

public void row(List<String> row, int line) {
if (rows == null) rows = new ArrayList<List<String>>();
rows.add(row);
List<String> escapedRow = new ArrayList<String>(row.size());
for(String cell : row) {
escapedRow.add(cell.replaceAll("\\|", "\\\\|").replaceAll("\\\\(?!\\|)", "\\\\\\\\"));
}
rows.add(escapedRow);
}

public void pyString(String string, int line) {
Expand Down
15 changes: 15 additions & 0 deletions lib/gherkin/formatter/escaping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Gherkin
module Formatter
module Escaping
# Escapes a pipes and backslashes:
#
# * | becomes \|
# * \ becomes \\
#
# This is used in the pretty formatter.
def escape_cell(s)
s.gsub(/\|/, "\\|").gsub(/\\(?!\|)/, "\\\\\\\\")
end
end
end
end
4 changes: 3 additions & 1 deletion lib/gherkin/formatter/pretty_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'gherkin/formatter/colors'
require 'gherkin/formatter/monochrome_format'
require 'gherkin/formatter/argument'
require 'gherkin/formatter/escaping'

module Gherkin
module Formatter
Expand All @@ -11,6 +12,7 @@ class PrettyFormatter
java_impl('gherkin.jar')

include Colors
include Escaping

def initialize(io, monochrome=false)
@io = io
Expand Down Expand Up @@ -67,7 +69,7 @@ def step(keyword, name, line, status=nil, exception=nil, arguments=nil, location

def row(row, line)
@rows ||= []
@rows << row.map{|cell| cell.gsub(/\|/, "\\|")}
@rows << row.map{|cell| escape_cell(cell)}
end

def py_string(string, line)
Expand Down
6 changes: 4 additions & 2 deletions ragel/lexer.c.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ static VALUE rb_eGherkinLexingError;
action store_cell_content {
VALUE con = ENCODED_STR_NEW(PTR_TO(content_start), LEN(content_start, p));
rb_funcall(con, rb_intern("strip!"), 0);
VALUE re = rb_reg_regcomp(rb_str_new2("\\\\\\|"));
rb_funcall(con, rb_intern("gsub!"), 2, re, rb_str_new2("|"));
VALUE re_pipe = rb_reg_regcomp(rb_str_new2("\\\\\\|"));
VALUE re_backslash = rb_reg_regcomp(rb_str_new2("\\\\\\\\"));
rb_funcall(con, rb_intern("gsub!"), 2, re_pipe, rb_str_new2("|"));
rb_funcall(con, rb_intern("gsub!"), 2, re_backslash, rb_str_new2("\\"));

rb_ary_push(current_row, con);
}
Expand Down
2 changes: 1 addition & 1 deletion ragel/lexer.java.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class <%= @i18n.underscored_iso_code.upcase %> implements Lexer {

action store_cell_content {
String con = substring(data, contentStart, p).trim();
currentRow.add(con.replaceAll("\\\\\\|", "|"));
currentRow.add(con.replaceAll("\\\\\\|", "|").replaceAll("\\\\\\\\", "\\\\"));
}

action store_row {
Expand Down
2 changes: 1 addition & 1 deletion ragel/lexer.rb.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module Gherkin

action store_cell_content {
con = utf8_pack(data[@content_start...p]).strip
current_row << con.gsub(/\\\|/, "|")
current_row << con.gsub(/\\\|/, "|").gsub(/\\\\/, "\\")
}

action store_row {
Expand Down
11 changes: 10 additions & 1 deletion spec/gherkin/formatter/pretty_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def assert_pretty(input, output=input)
"""
And there is another step
| æ | \\|o |
| \\|a | ø |
| \\|a | ø\\\\ |
Then we will see steps
})
end
Expand Down Expand Up @@ -148,6 +148,15 @@ def assert_pretty(input, output=input)
it "should preserve tabs" do
assert_pretty(IO.read(File.dirname(__FILE__) + '/tabs.feature'), IO.read(File.dirname(__FILE__) + '/spaces.feature'))
end

it "should escape backslashes and pipes" do
io = StringIO.new
l = PrettyFormatter.new(io, true)
l.row(['|', '\\'], 1)
l.flush_table
io.rewind
io.read.should == ' | \\| | \\\\ |' + "\n"
end
end
end
end

0 comments on commit 7f0533f

Please sign in to comment.