Created
November 21, 2012 15:27
-
-
Save dnshl/4125433 to your computer and use it in GitHub Desktop.
Rake Task with XMLFormatter for cucumber and XSLT Document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes"/> | |
<xsl:template match="/"> | |
<html> | |
<head> | |
<title>Documentation</title> | |
<style> | |
body { | |
background-color: #EEE; | |
color: #333; | |
font-family: Arial, Verdana, sans-serif; | |
margin: 0; | |
padding: 0; | |
} | |
h1, h2, h3, h4, h5, h6 { | |
border-bottom: 1px solid #DDD; | |
padding-bottom: 0.3em; | |
} | |
p { | |
line-height: 1.4em; | |
margin-bottom: 1.5em; | |
} | |
a { text-decoration: none; } | |
a:hover { text-decoration: underline; } | |
table { | |
border-collapse: collapse; | |
margin: 10px; | |
} | |
td { | |
border: 1px solid #DDD; | |
padding: 6px; | |
} | |
#container { | |
background-color: #FFF; | |
border-left: 1px solid #DDD; | |
border-right: 1px solid #DDD; | |
margin-left: 40px; | |
padding: 10px 20px; | |
width: 750px; | |
} | |
#container > ul { | |
margin-bottom: 40px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>Features Documentation</h1> | |
<ul> | |
<xsl:for-each select="/objects/object"> | |
<li> | |
<a href="#{translate(normalize-space(name),' ','-')}"><xsl:value-of select="name" /></a> | |
<ul> | |
<xsl:for-each select="elements/element"> | |
<li> | |
<a href="#{translate(normalize-space(name),' ','-')}"><xsl:value-of select="name" /></a> | |
</li> | |
</xsl:for-each> | |
</ul> | |
</li> | |
</xsl:for-each> | |
</ul> | |
<xsl:for-each select="/objects/object"> | |
<h2><a name="{translate(normalize-space(name),' ','-')}"><xsl:value-of select="keyword" />: <xsl:value-of select="name" /></a></h2> | |
<xsl:apply-templates select="description"/> | |
<xsl:apply-templates select="elements/element"/> | |
</xsl:for-each> | |
</div> | |
</body> | |
</html> | |
</xsl:template> | |
<xsl:template match="element"> | |
<h3><a name="{translate(normalize-space(name),' ','-')}"><xsl:value-of select="keyword" />: <xsl:value-of select="name" /></a></h3> | |
<xsl:apply-templates select="description" /> | |
<p> | |
<xsl:apply-templates select="steps" /> | |
</p> | |
<xsl:if test="examples/example/rows/row/cells/cell != ''"> | |
<table> | |
<xsl:apply-templates select="examples/example/rows/row" /> | |
</table> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="description"> | |
<xsl:if test=". != ''"> | |
<p> | |
<xsl:value-of select="." /> | |
</p> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="step"> | |
<xsl:value-of select="keyword" /> <xsl:value-of select="name" /> | |
<xsl:if test="following-sibling::step"> | |
<br/> | |
</xsl:if> | |
<xsl:if test="rows/row/cells/cell != ''"> | |
<table> | |
<xsl:apply-templates select="rows/row" /> | |
</table> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="row"> | |
<tr> | |
<xsl:for-each select="cells/cell"> | |
<td> | |
<xsl:value-of select="." /> | |
</td> | |
</xsl:for-each> | |
</tr> | |
</xsl:template> | |
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gherkin/parser/parser' | |
require 'gherkin/formatter/json_formatter' | |
require 'stringio' | |
require "active_support/core_ext/hash/conversions" | |
require 'nokogiri' | |
class XMLFormatter < Gherkin::Formatter::JSONFormatter | |
def done | |
@io.write(@feature_hashes.to_xml) | |
end | |
end | |
namespace :doc do | |
desc "Generates an XML Documentl for all cucumber features" | |
task :cucumber do | |
io = StringIO.new | |
formatter = XMLFormatter.new(io) | |
#formatter = Gherkin::Formatter::JSONFormatter.new(io) | |
#def formatter.done | |
# @io.write(@feature_hashes.to_xml) | |
#end | |
parser = Gherkin::Parser::Parser.new(formatter) | |
Dir['features/*.feature'].each do |feature| | |
path = File.expand_path(File.dirname(__FILE__) + '/' + feature) | |
parser.parse(IO.read(path), path, 0) | |
end | |
formatter.done | |
xml = Nokogiri::XML io.string | |
xml.root.add_previous_sibling( | |
Nokogiri::XML::ProcessingInstruction.new(xml, "xml-stylesheet", 'type="text/xsl" href="features.xsl"')) | |
output_directory = 'doc/features/' | |
Dir.mkdir(output_directory) unless File.exists?(output_directory) | |
File.open(output_directory + 'features.xml', 'w') {|f| f.write(xml)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment