From 4b8fdf987f806bc91f2c97a1309fcd66185ac6bd Mon Sep 17 00:00:00 2001 From: Dominik Goldschmidt Date: Thu, 23 Oct 2014 00:05:06 +0200 Subject: [PATCH] add support for table formatting via comments --- README.adoc | 35 ++++++++++++++ .../asciidoctor/gherkin/default_template.erb | 34 +++++++++----- .../asciidoctor/gherkin/gherkinblockmacro.rb | 46 ++++++++++++++++++- src/test/resources/complex.feature | 33 +++++++++---- 4 files changed, 128 insertions(+), 20 deletions(-) diff --git a/README.adoc b/README.adoc index 6d9aea9..be6d2a5 100644 --- a/README.adoc +++ b/README.adoc @@ -63,6 +63,41 @@ Or just create your own template. See the integration tests for a {github-url}/src/it/asciidoctor-maven-with-gherkin-it/pom.xml[complete maven example]. +=== Formatting tables + +You can add some asciidoctor formatting to tables, e.g. column widths, header style, etc. + +For example, the following gherkin table + +---- + Given a complex background step with table with header + #cols=".<2,.^5,^.>3",options="header" + #cells=h,h,h + | Header Cell 1 | Header Cell 2 | Header Cell 3 | + #cells=,m, + | Cell 1 Row 1 | Cell 2 Row 1 monospace | Cell 3 Row 1 | + | Cell 1 Row 2 with *bold* | Cell 2 Row 2 | Cell 3 Row 2 | +---- + +renders as asciidoctor equivalent : + +---- +[.step-list] +* *Given* a complex background step with table with header ++ +[cols=".<2,.^5,^.>3",options="header"] +|==== +h| Header Cell 1 h| Header Cell 2 h| Header Cell 3 +| Cell 1 Row 1 m| Cell 2 Row 1 monospace | Cell 3 Row 1 +| Cell 1 Row 2 with *bold* | Cell 2 Row 2 | Cell 3 Row 2 +|==== + +---- + +If the first comment of the first row starts with "cols=", then the content of the comment gets placed between brackets right before the table. + +Row comments starting with "cells=" get split on "," and become cell formatters. + == Custom templates You can use the `template` attribute to specify a custom `erb` template. diff --git a/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/default_template.erb b/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/default_template.erb index 544dde1..778bc6f 100644 --- a/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/default_template.erb +++ b/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/default_template.erb @@ -1,5 +1,4 @@ - === <%= feature['name'] %> <%if feature.key?('description') %> @@ -17,21 +16,27 @@ <%= step['doc_string']['value'] %> .... <% end %><%if step.key?('rows') %> -+ ++<% if step['rows'].first && step['rows'].first.key?('cols') %> +[<%= step['rows'].first['cols'] %>]<% end %> |==== -<% step['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %> +<% step['rows'].each_with_index do |row, index| %><% row['cells'].each_with_index do |cell, i| %><% +if row.key?('cell-styles') && row['cell-styles'].length > i %><%= row['cell-styles'][i] %><% end %>| <%= cell %> <% end %> <% end %>|==== <% end %> <% end %> <% end %> -<%if feature['background'].key?('examples') %> +<%if feature['background'].key?('examples') +example = feature['background']['examples'] %> -===== <%= feature['background']['examples']['keyword'].strip %> <%= feature['background']['examples']['name'] %> +===== <%= example['keyword'].strip %> <%= example['name'] %> +<% if example['rows'].first && example['rows'].first.key?('cols') %> +[<%= example['rows'].first['cols'] %>]<% end %> |==== -<% feature['background']['examples']['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %> +<% example['rows'].each_with_index do |row, index| %><% row['cells'].each_with_index do |cell, i| %><% +if row.key?('cell-styles') && row['cell-styles'].length > i %><%= row['cell-styles'][i] %><% end %>| <%= cell %> <% end %> <% if index == 0 %> <% end %><% end %>|==== <% end %> @@ -48,21 +53,28 @@ <%= step['doc_string']['value'] %> .... <% end %><%if step.key?('rows') %> -+ ++<% if step['rows'].first && step['rows'].first.key?('cols') %> +[<%= step['rows'].first['cols'] %>]<% end %> |==== -<% step['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %> +<% step['rows'].each_with_index do |row, index| %><% row['cells'].each_with_index do |cell, i| %><% +if row.key?('cell-styles') && row['cell-styles'].length > i %><%= row['cell-styles'][i] %><% end %>| <%= cell %> <% end %> <% end %>|==== <% end %> <% end %> <% end %> -<%if scenario.key?('examples') %> +<%if scenario.key?('examples') +example = scenario['examples'] +%> -===== <%= scenario['examples']['keyword'].strip %> <%= scenario['examples']['name'] %> +===== <%= example['keyword'].strip %> <%= example['name'] %> +<% if example['rows'].first && example['rows'].first.key?('cols') %> +[<%= example['rows'].first['cols'] %>]<% end %> |==== -<% scenario['examples']['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %> +<% example['rows'].each_with_index do |row, index| %><% row['cells'].each_with_index do |cell, i| %><% +if row.key?('cell-styles') && row['cell-styles'].length > i %><%= row['cell-styles'][i] %><% end %>| <%= cell %> <% end %> <% if index == 0 %> <% end %><% end %>|==== diff --git a/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/gherkinblockmacro.rb b/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/gherkinblockmacro.rb index 37c0543..bf33501 100644 --- a/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/gherkinblockmacro.rb +++ b/src/main/resources/com/github/domgold/doctools/asciidoctor/gherkin/gherkinblockmacro.rb @@ -50,9 +50,53 @@ def process parent, target, attributes #parse feature and make the result available to the template via binding as 'feature' hash. feature = com.github.domgold.doctools.asciidoctor.gherkin.MapFormatter.parse(feature_file_content) + preprocess_feature(feature) rendered_template_output = erb_template.result(binding()) - + reader.push_include rendered_template_output, target, target, 1, attributes nil end + + def preprocess_feature feature + if feature.key?('background') + preprocess_scenario feature['background'] + end + if feature.key?('scenarios') + feature['scenarios'].each do | scenario | + preprocess_scenario scenario + end + end + end + + def preprocess_scenario scenario + if scenario.key?('steps') + preprocess_steplist scenario['steps'] + end + if scenario.key?('examples') + preprocess_table_comments scenario['examples']['rows'] + end + end + + def preprocess_steplist steplist + steplist.each do | step | + if step.key?('rows') + preprocess_table_comments step['rows'] + end + end + end + + def preprocess_table_comments rows + if rows.length > 0 && rows.first.key?('comments') && rows.first['comments'].length > 0 && rows.first['comments'].first['value'].match(/^#cols=/) + cols = rows.first['comments'].first['value'][1..-1] + rows.first['comments'].java_send :remove, [Java::int], 0 + rows.first["cols"] = cols + end + rows.each do | row | + if row.key?('comments') && row['comments'].length > 0 && row['comments'].first['value'].match(/^#cells=/) + cells = row['comments'].first['value'][7..-1].split(/,/) + row['cell-styles'] = cells + row['comments'].java_send :remove, [Java::int], 0 + end + end + end end \ No newline at end of file diff --git a/src/test/resources/complex.feature b/src/test/resources/complex.feature index a21e13a..ed638b3 100644 --- a/src/test/resources/complex.feature +++ b/src/test/resources/complex.feature @@ -2,7 +2,7 @@ Feature: A complex feature This is the feature description. - Use asciidoctor markup as it pleases you. + In the feature's description, you can use asciidoctor markup as it pleases you. [source,java] ---- @@ -11,7 +11,7 @@ Feature: A complex feature * list content * list content2 - *** nested list content. + ** nested list content. WARNING: admonition. @@ -25,11 +25,17 @@ Feature: A complex feature |==== Background: Background - Given a simple step. + Given a complex background step with table with header + #cols=".<2,.^5,^.>3",options="header" + #cells=h,h,h + | Header Cell 1 | Header Cell 2 | Header Cell 3 | + #cells=,m, + | Cell 1 Row 1 | Cell 2 Row 1 monospace | Cell 3 Row 1 | + | Cell 1 Row 2 with *bold* | Cell 2 Row 2 | Cell 3 Row 2 | - Scenario Outline: Scenario title + Scenario Outline: 1st scenario title - The Scenario description comes here. + The scenario description comes here. [NOTE] ==== @@ -40,6 +46,12 @@ Feature: A complex feature \* ----- ==== + + - dash list in scenario description + - the second list item + + \* escaped star list + \* second list item Given a simple outline step with a doc string : """ @@ -56,6 +68,11 @@ Feature: A complex feature And the file "**.png" everything is fine. Examples: - | parameter_name | parameter_value | - | _actorWidth_ | 25 | - | actorHeight | 30 | + #cols="2,2,^4" + | parameter_name | parameter_value | 3rd colonne double width | + | _actorWidth_ | 25 | A | + | actorHeight | 30 | B | + + Scenario: 2nd scenario title + Given a short scenario + Then it's really short.