Skip to content

Instantly share code, notes, and snippets.

@josephwilk
Created March 7, 2009 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephwilk/75378 to your computer and use it in GitHub Desktop.
Save josephwilk/75378 to your computer and use it in GitHub Desktop.
Pdf formatter for Cucumber 0.1.99
#cucumber --require pdf_formatter.rb --quiet --dry-run --format Cucumber::Formatter::Pdf features/
require 'cucumber/formatter/console'
require 'prawn'
class PdfIO
def initialize
@pdf = Prawn::Document.new :page_size => "A4", :top_margin => 60
end
def tty?
false
end
def method_missing(method, *args)
@pdf.send(method, *args)
end
end
module Cucumber
module Formatter
class Pdf < Ast::Visitor
include Console
attr_writer :indent
FEATURE_TITLE_STYLE = {:size => 24}
SCENARIO_TITLE_STYLE = {:size => 16}
STEP_STYLE = {}
FOOTER_STYLE = {:size => 12}
DOC_TITLE_STYLE = {:size => 32, :align => :center}
def initialize(step_mother, io, options, delim='|')
super(step_mother)
@io = PdfIO.new
@options = options
@delim = delim
@indent = 0
end
def visit_features(features)
super
@io.render_file("features.pdf")
end
def visit_feature(feature)
@indent = 0
with_color do
feature.accept(self)
end
end
def visit_comment(comment)
comment.accept(self)
end
def visit_comment_line(comment_line)
unless comment_line.blank?
@io.text(comment_line.indent(@indent))
end
end
def visit_tags(tags)
tags.accept(self)
if @indent == 1
@io.text "\n"
end
end
def visit_tag_name(tag_name)
tag = format_string("@#{tag_name}", :tag).indent(@indent)
@io.text(tag)
@indent = 1
end
def visit_feature_name(name)
name, *narrative = name.split("\n")
@io.text(name, FEATURE_TITLE_STYLE)
@io.text(narrative.join("\n")) unless narrative.nil?
@io.text("\n")
end
def visit_feature_element(feature_element)
@indent = 2
@last_undefined = feature_element.undefined?
feature_element.accept(self)
@io.text("\n")
end
def visit_background(background)
@indent = 2
background_already_visible = background.already_visited_steps?
background.accept(self)
@io.text("\n") unless background_already_visible
end
def visit_examples(examples)
examples.accept(self)
end
def visit_examples_name(keyword, name)
@io.text("\n #{keyword} #{name}")
@indent = 4
end
def visit_scenario_name(keyword, name, file_line, source_indent)
line = " #{keyword} #{name}"
line = format_string(line, :undefined) if @last_undefined
@io.text(line, SCENARIO_TITLE_STYLE)
@io.text("\n")
end
def visit_step(step)
@indent = 6
exception = step.accept(self)
print_exception(exception, @indent) if exception
end
def visit_step_name(keyword, step_name, status, step_definition, source_indent)
source_indent = nil unless @options[:source]
formatted_step_name = format_step(keyword, step_name, status, step_definition, source_indent)
@io.text(" " + formatted_step_name)
end
def visit_multiline_arg(multiline_arg, status)
return if @options[:no_multiline]
multiline_arg.accept(self, status)
end
def visit_table_row(table_row, status)
@io.text(@delim.indent(@indent))
exception = table_row.accept(self, status)
@io.text("\n")
print_exception(exception, 6) if exception
end
def visit_py_string(string, status)
s = "\"\"\"\n#{string}\n\"\"\"".indent(@indent)
s = s.split("\n").map{|l| l =~ /^\s+$/ ? '' : l}.join("\n")
@io.text(format_string(s, status))
end
def visit_table_cell(table_cell, status)
table_cell.accept(self, status)
end
def visit_table_cell_value(value, width, status)
@io.text(' ' + format_string((value.to_s || '').ljust(width), status) + " #{@delim}")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment