Module: Cukable::Helper
- Included in:
- Converter
- Defined in:
- lib/cukable/helper.rb
Overview
Common/shared methods supporting the Cukable library
Instance Method Summary collapse
-
#clean_cell(string) ⇒ String
Return the given string, cleaned of any HTML tags and status indicators added by the JSON output formatter.
-
#clean_filename(filename, prefix, suffix) ⇒ String
Return
filenamewithprefixandsuffixremoved, and any path-separators converted to underscores. -
#literalize(string) ⇒ String
Return the given string with any CamelCase words, email addresses, and URLs escaped with FitNesse's
!-...-!string-literal markup. -
#remove_cruft(string) ⇒ String
Remove FitNesse-generated link cruft from a string.
-
#table_digest(table) ⇒ String
Return an MD5 digest string for
table. -
#unescape(string) ⇒ String
Unescape any HTML entities and FitNesse markup in the given string.
-
#wikify(string) ⇒ String
Wikify (CamelCase) the given string, removing spaces, underscores, dashes and periods, and CamelCasing the remaining words.
-
#wikify_path(path) ⇒ String
Wikify the given path name, and return a path that's suitable for use as a FitNesse wiki page path.
Instance Method Details
#clean_cell(string) ⇒ String
Return the given string, cleaned of any HTML tags and status indicators added by the JSON output formatter. The intent here is to take a table cell from JSON output, and make it match the original FitNesse table cell.
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/cukable/helper.rb', line 207
def clean_cell(string)
# FIXME: This may not be terribly efficient...
# strip first to get a copy of the string
result = string.strip
# Remove extra stuff added by JSON formatter
result.gsub!(/^[^:]*:(.*)$/, '\1') # status indicator
result.gsub!(/<b>|<\/b>/, '') # all bold tags
result.gsub!(/<br\/?>.*/, '') # <br> and anything that follows
result.gsub!(/<span[^>]*>.*<\/span>/, '') # spans and their content
result.gsub!(/\(Undefined Step\)/, '') # (Undefined Step)
return result.strip
end
|
#clean_filename(filename, prefix, suffix) ⇒ String
Return filename with prefix and suffix removed, and any
path-separators converted to underscores.
28 29 30 31 |
# File 'lib/cukable/helper.rb', line 28
def clean_filename(filename, prefix, suffix)
middle = filename.gsub(/^#{prefix}\/(.+)\/#{suffix}$/, '\1')
return middle.gsub('/', '_')
end
|
#literalize(string) ⇒ String
Return the given string with any CamelCase words, email addresses, and
URLs escaped with FitNesse's !-...-! string-literal markup.
FIXME: Literals inside other literals will cause too much escaping!
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cukable/helper.rb', line 101
def literalize(string)
result = string.strip
# Literalize email addresses
# FitNesse pattern for email addresses, per TextMaker.java:
# [\w\-_.]+@[\w\-_.]+\.[\w\-_.]+
result.gsub!(/([\w\-_.]+@[\w\-_.]+\.[\w\-_.]+)/, '!-\1-!')
# Literalize CamelCase words
# Regex for matching wiki words, according to FitNesse.UserGuide.WikiWord
# \b[A-Z](?:[a-z0-9]+[A-Z][a-z0-9]*)+
result.gsub!(/(\b[A-Z](?:[a-z0-9]+[A-Z][a-z0-9]*)+)/, '!-\1-!')
# Literalize URLs
# Brain-dead URL matcher, should do the trick in most cases though
# (Better to literalize too much than not enough)
result.gsub!(/(http[^ ]+)/, '!-\1-!')
return result
end
|
#remove_cruft(string) ⇒ String
Remove FitNesse-generated link cruft from a string. Strips <a ...></a>
tags, keeping the inner content unless that content is '[?]'.
49 50 51 |
# File 'lib/cukable/helper.rb', line 49
def remove_cruft(string)
string.gsub(/<a [^>]*>([^<]*)<\/a>/, '\1').gsub('[?]', '')
end
|
#table_digest(table) ⇒ String
Return an MD5 digest string for table. Any HTML entities and FitNesse
markup in the table is unescaped before the digest is calculated.
161 162 163 164 165 166 167 |
# File 'lib/cukable/helper.rb', line 161
def table_digest(table)
digest = Digest::MD5.new
table.flatten.each do |cell|
digest.update(unescape(cell))
end
return digest.to_s
end
|
#unescape(string) ⇒ String
Unescape any HTML entities and FitNesse markup in the given string.
183 184 185 186 187 |
# File 'lib/cukable/helper.rb', line 183
def unescape(string)
result = CGI.unescapeHTML(string)
result.gsub!(/!-(.*?)-!/, '\1')
return result
end
|
#wikify(string) ⇒ String
Wikify (CamelCase) the given string, removing spaces, underscores, dashes and periods, and CamelCasing the remaining words. If this does not result in a CamelCase word with at least two words in it (that is, if the input was only a single word), then the last letter in the word is capitalized so as to make FitNesse happy.
FIXME: This will not generate valid FitNesse wiki page names for pathological cases, such as any input that would result in consecutive capital letters, including words having only two letters in them.
76 77 78 79 80 81 82 83 84 |
# File 'lib/cukable/helper.rb', line 76
def wikify(string)
string.gsub!(/^[a-z]|[_.\s\-]+[a-z]/) { |a| a.upcase }
string.gsub!(/[_.\s\-]/, '')
if string =~ /(([A-Z][a-z]*){2})/
return string
else
return string.gsub(/.\b/) { |c| c.upcase }
end
end
|
#wikify_path(path) ⇒ String
Wikify the given path name, and return a path that's suitable for use as a FitNesse wiki page path. Any path component having only a single word in it will have the last letter in that word capitalized.
138 139 140 141 142 143 |
# File 'lib/cukable/helper.rb', line 138
def wikify_path(path)
wiki_parts = path.split(File::SEPARATOR).collect do |part|
wikify(part)
end
return File.join(wiki_parts)
end
|