Skip to content

Commit

Permalink
proto
Browse files Browse the repository at this point in the history
  • Loading branch information
domgold committed Oct 6, 2014
1 parent 206ed3e commit 285aa78
Show file tree
Hide file tree
Showing 23 changed files with 348 additions and 363 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.classpath
.project
.settings/
target/

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,48 @@
import gherkin.formatter.model.Scenario;
import gherkin.formatter.model.ScenarioOutline;
import gherkin.formatter.model.Step;
import gherkin.parser.Parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.kinimod.asciidoctor.gherkin.template.TemplateProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.io.IOUtils;

public class AsciidocFormatter implements Formatter {
public class MapFormatter implements Formatter {

private TemplateProcessor templateProcessor;
public static String getDefaultTemplate() {

public AsciidocFormatter(StringBuilder builder) {
this.builder = builder;
ClassPathXmlApplicationContext applicationContext = null;
String text = "";
try {
applicationContext = new ClassPathXmlApplicationContext();

applicationContext.setConfigLocation("classpath:/beans.xml");
applicationContext.refresh();
this.templateProcessor = applicationContext
.getBean(TemplateProcessor.class);

} finally {
if (applicationContext != null) {
applicationContext.close();
}
text = IOUtils
.toString(
MapFormatter.class
.getResourceAsStream("/com/github/domgold/asciidoctor/extension/gherkin/default_template.erb"),
"UTF-8");
} catch (IOException e) {
throw new RuntimeException(e);
}
return text;
}

private StringBuilder builder;
public static Map<String, Object> parse(String fileContent) {
MapFormatter f = new MapFormatter();
Parser p = new Parser(f);
p.parse(fileContent, "feature", 0);
return f.getFeature();
}

private Map<String, Object> currentFeature;

private Map<String, Object> currentScenario;

private Map<String, Object> currentStep;

private Map<String, Object> currentExamples;

public MapFormatter() {
super();
}

@Override
public void background(Background arg0) {
currentScenario = arg0.toMap();
Expand All @@ -66,18 +67,6 @@ public void done() {
public void endOfScenarioLifeCycle(Scenario arg0) {
}

@Override
public void eof() {
try {
String content = templateProcessor.process("feature.ftl",
currentFeature);
builder.append(content);
} catch (IOException e) {
throw new RuntimeException(e);
}

}

@Override
public void examples(Examples arg0) {
currentExamples = arg0.toMap();
Expand Down Expand Up @@ -120,6 +109,7 @@ public void syntaxError(String arg0, String arg1, List<String> arg2,
public void uri(String arg0) {
}

@SuppressWarnings("unchecked")
private void addNew(Map<String, Object> baseMap, String key,
Map<String, Object> newMap) {
if (!baseMap.containsKey(key)) {
Expand All @@ -128,4 +118,13 @@ private void addNew(Map<String, Object> baseMap, String key,
((List<Map<String, Object>>) baseMap.get(key)).add(newMap);
}

@Override
public void eof() {

}

public Map<String, Object> getFeature() {
return currentFeature;
}

}

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/resources/beans.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@


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

<%if feature.key?('description') %>
<%= feature['description'] %>
<% end %>

<%if feature.key?('background') %>==== <%= feature['background']['name'] %>
<%if feature['background'].key?('description') %>
<%= feature['background']['description'] %>
<% end %><%if feature['background'].key?('steps') %>
[.step-list]<% feature['background']['steps'].each do |step| %>
* *<%= step['keyword'].strip %>* <%= step['name'] %><%if step.key?('doc_string') %>
+
....
<%= step['doc_string']['value'] %>
....
<% end %><%if step.key?('rows') %>
+
|====
<% step['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %>
<% end %>|====
<% end %>

<% end %>
<% end %>

<%if feature['background'].key?('examples') %>

===== <%= feature['background']['examples']['keyword'].strip %> <%= feature['background']['examples']['name'] %>

|====
<% feature['background']['examples']['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %>
<% if index == 0 %>
<% end %><% end %>|====
<% end %>
<% end %>

<%if feature.key?('scenarios') %><% feature['scenarios'].each do |scenario| %>==== <%= scenario['name'] %>
<%if scenario.key?('description') %>
<%= scenario['description'] %>
<% end %><%if scenario.key?('steps') %>
[.step-list]<% scenario['steps'].each do |step| %>
* *<%= step['keyword'].strip %>* <%= step['name'] %><%if step.key?('doc_string') %>
+
....
<%= step['doc_string']['value'] %>
....
<% end %><%if step.key?('rows') %>
+
|====
<% step['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %>
<% end %>|====
<% end %>

<% end %>
<% end %>

<%if scenario.key?('examples') %>

===== <%= scenario['examples']['keyword'].strip %> <%= scenario['examples']['name'] %>

|====
<% scenario['examples']['rows'].each_with_index do |row, index| %><% row['cells'].each do |cell| %>a| <%= cell %> <% end %>
<% if index == 0 %>
<% end %><% end %>|====

<% end %>

<% end %>
<% end %>

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'erb'
require 'java'

class GherkinIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor

def process doc, reader, target, attributes
#get the default template from java resource
template_content = org.kinimod.asciidoctor.gherkin.MapFormatter.getDefaultTemplate()

if doc.attributes.key?('docdir') && attributes.key?('template') && File.exist?(File.expand_path(attributes['template'], doc.attributes['docdir']))
template_file = File.open(File.expand_path(attributes['template'], doc.attributes['docdir']), "rb")
template_content = template_file.read
else
template_content = org.kinimod.asciidoctor.gherkin.MapFormatter.getDefaultTemplate()
end

erb_template = ERB.new(template_content)

feature_file_name = target[8..-1]
if doc.attributes.key?('docdir')
feature_file_name = File.expand_path(feature_file_name, doc.attributes['docdir'])
end

file = File.open(feature_file_name, "rb")
feature_file_content = file.read

#parse feature and make the result available to the template via binding as 'feature' hash.
feature = org.kinimod.asciidoctor.gherkin.MapFormatter.parse(feature_file_content)

rendered_template_output = erb_template.result(binding())

reader.push_include rendered_template_output, target, target, 1, attributes

reader
end

def handles? target
target.start_with? 'gherkin:'
end

end
Loading

0 comments on commit 285aa78

Please sign in to comment.