Skip to content

Commit

Permalink
add support for table formatting via comments
Browse files Browse the repository at this point in the history
  • Loading branch information
domgold committed Oct 22, 2014
1 parent 66e59c9 commit 4b8fdf9
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 20 deletions.
35 changes: 35 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


=== <%= feature['name'] %>

<%if feature.key?('description') %>
Expand All @@ -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 %>
Expand All @@ -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 %>|====

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 25 additions & 8 deletions src/test/resources/complex.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand All @@ -11,7 +11,7 @@ Feature: A complex feature

* list content
* list content2
*** nested list content.
** nested list content.

WARNING: admonition.

Expand All @@ -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]
====
Expand All @@ -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 :
"""
Expand All @@ -56,6 +68,11 @@ Feature: A complex feature
And the file "*<parameter_name>*.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.

0 comments on commit 4b8fdf9

Please sign in to comment.