Class: Gherkin::I18n
Constant Summary collapse
- LexerNotFound =
Class.new(LoadError)
- FEATURE_ELEMENT_KEYS =
%w{feature background scenario scenario_outline examples}
- STEP_KEYWORD_KEYS =
%w{given when then and but}
- KEYWORD_KEYS =
FEATURE_ELEMENT_KEYS + STEP_KEYWORD_KEYS
- LANGUAGES =
YAML.load_file(File.dirname(__FILE__) + '/i18n.yml')
Instance Attribute Summary collapse
-
#iso_code ⇒ Object
readonly
Returns the value of attribute iso_code.
Class Method Summary collapse
-
.all ⇒ Object
Used by code generators for other lexer tools like pygments lexer and textmate bundle.
- .code_keyword_for(gherkin_keyword) ⇒ Object
- .code_keywords ⇒ Object
- .get(iso_code) ⇒ Object
-
.keyword_regexp(*keywords) ⇒ Object
Returns all keyword translations and aliases of
keywords
, escaped and joined with|
. - .language_table ⇒ Object
- .unicode_escape(word, prefix = "\\u") ⇒ Object
Instance Method Summary collapse
- #c(listener) ⇒ Object
-
#code_keywords ⇒ Object
Keywords that can be used in code.
-
#initialize(iso_code) ⇒ I18n
constructor
A new instance of I18n.
- #js(listener) ⇒ Object
- #keyword_table ⇒ Object
- #keywords(key) ⇒ Object
- #lexer(listener, force_ruby = false) ⇒ Object
- #rb(listener) ⇒ Object
-
#step_keywords ⇒ Object
Keywords that can be used in Gherkin source.
- #underscored_iso_code ⇒ Object
Methods included from Rubify
Constructor Details
#initialize(iso_code) ⇒ I18n
Returns a new instance of I18n.
89 90 91 92 93 94 |
# File 'lib/gherkin/i18n.rb', line 89 def initialize(iso_code) @iso_code = iso_code @keywords = LANGUAGES[iso_code] raise "Language not supported: #{iso_code.inspect}" if @iso_code.nil? @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '') end |
Instance Attribute Details
#iso_code ⇒ Object (readonly)
Returns the value of attribute iso_code.
87 88 89 |
# File 'lib/gherkin/i18n.rb', line 87 def iso_code @iso_code end |
Class Method Details
.all ⇒ Object
Used by code generators for other lexer tools like pygments lexer and textmate bundle
20 21 22 |
# File 'lib/gherkin/i18n.rb', line 20 def all LANGUAGES.keys.sort.map{|iso_code| get(iso_code)} end |
.code_keyword_for(gherkin_keyword) ⇒ Object
53 54 55 |
# File 'lib/gherkin/i18n.rb', line 53 def code_keyword_for(gherkin_keyword) gherkin_keyword.gsub(/[\s',!]/, '').strip end |
.code_keywords ⇒ Object
49 50 51 |
# File 'lib/gherkin/i18n.rb', line 49 def code_keywords rubify(all.map{|i18n| i18n.code_keywords}).flatten.uniq.sort end |
.get(iso_code) ⇒ Object
24 25 26 |
# File 'lib/gherkin/i18n.rb', line 24 def get(iso_code) languages[iso_code] ||= new(iso_code) end |
.keyword_regexp(*keywords) ⇒ Object
Returns all keyword translations and aliases of keywords
, escaped and joined with |
. This method is convenient for editor support and syntax highlighting engines for Gherkin, where there is typically a code generation tool to generate regular expressions for recognising the various I18n translations of Gherkin’s keywords.
The keywords
arguments can be one of :feature
, :background
, :scenario
, :scenario_outline
, :examples
, :step
.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gherkin/i18n.rb', line 35 def keyword_regexp(*keywords) unique_keywords = all.map do |i18n| keywords.map do |keyword| if keyword.to_s == 'step' i18n.step_keywords.to_a else i18n.keywords(keyword).to_a end end end unique_keywords.flatten.compact.map{|kw| kw.to_s}.sort.reverse.uniq.join('|').gsub(/\*/, '\*') end |
.language_table ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gherkin/i18n.rb', line 57 def language_table require 'stringio' require 'gherkin/formatter/pretty_formatter' require 'gherkin/formatter/model' io = StringIO.new pf = Gherkin::Formatter::PrettyFormatter.new(io, true, false) table = all.map do |i18n| Formatter::Model::DataTableRow.new([], [i18n.iso_code, i18n.keywords('name')[0], i18n.keywords('native')[0]], nil) end pf.table(table) io.string end |
.unicode_escape(word, prefix = "\\u") ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/gherkin/i18n.rb', line 70 def unicode_escape(word, prefix="\\u") word = word.unpack("U*").map do |c| if c > 127 || c == 32 "#{prefix}%04x" % c else c.chr end end.join end |
Instance Method Details
#c(listener) ⇒ Object
111 112 113 114 |
# File 'lib/gherkin/i18n.rb', line 111 def c(listener) require 'gherkin/c_lexer' CLexer[underscored_iso_code].new(listener) end |
#code_keywords ⇒ Object
Keywords that can be used in code
136 137 138 139 140 |
# File 'lib/gherkin/i18n.rb', line 136 def code_keywords result = step_keywords.map{|keyword| self.class.code_keyword_for(keyword)} result.delete('*') result end |
#js(listener) ⇒ Object
121 122 123 124 |
# File 'lib/gherkin/i18n.rb', line 121 def js(listener) require 'gherkin/js_lexer' JsLexer[underscored_iso_code].new(listener) end |
#keyword_table ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/gherkin/i18n.rb', line 148 def keyword_table require 'stringio' require 'gherkin/formatter/pretty_formatter' require 'gherkin/formatter/model' io = StringIO.new pf = Gherkin::Formatter::PrettyFormatter.new(io, false, false) gherkin_keyword_table = KEYWORD_KEYS.map do |key| Formatter::Model::Row.new([], [key, keywords(key).map{|keyword| %{"#{keyword}"}}.join(', ')], nil) end code_keyword_table = STEP_KEYWORD_KEYS.map do |key| code_keywords = keywords(key).reject{|keyword| keyword == '* '}.map do |keyword| %{"#{self.class.code_keyword_for(keyword)}"} end.join(', ') Formatter::Model::Row.new([], ["#{key} (code)", code_keywords], nil) end pf.table(gherkin_keyword_table + code_keyword_table) io.string end |
#keywords(key) ⇒ Object
142 143 144 145 146 |
# File 'lib/gherkin/i18n.rb', line 142 def keywords(key) key = key.to_s raise "No #{key.inspect} in #{@keywords.inspect}" if @keywords[key].nil? @keywords[key].split('|').map{|keyword| real_keyword(key, keyword)} end |
#lexer(listener, force_ruby = false) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gherkin/i18n.rb', line 96 def lexer(listener, force_ruby=false) if force_ruby rb(listener) else begin c(listener) rescue NameError, LoadError => e warn("WARNING: #{e.}. Reverting to Ruby lexer.") rb(listener) end end rescue LoadError => e raise LexerNotFound, "No lexer was found for #{iso_code} (#{e.}). Supported languages are listed in gherkin/i18n.yml." end |
#rb(listener) ⇒ Object
116 117 118 119 |
# File 'lib/gherkin/i18n.rb', line 116 def rb(listener) require 'gherkin/rb_lexer' RbLexer[underscored_iso_code].new(listener) end |
#step_keywords ⇒ Object
Keywords that can be used in Gherkin source
131 132 133 |
# File 'lib/gherkin/i18n.rb', line 131 def step_keywords STEP_KEYWORD_KEYS.map{|iso_code| keywords(iso_code)}.flatten.uniq end |
#underscored_iso_code ⇒ Object
126 127 128 |
# File 'lib/gherkin/i18n.rb', line 126 def underscored_iso_code @iso_code.gsub(/[\s-]/, '_').downcase end |