diff --git a/lib/rouge/lexers/gherkin.rb b/lib/rouge/lexers/gherkin.rb index 35f25bab59..567183fd20 100644 --- a/lib/rouge/lexers/gherkin.rb +++ b/lib/rouge/lexers/gherkin.rb @@ -28,10 +28,29 @@ def self.detect?(text) rule %r/[ \r\t]+/, Text end + state :annotated_string do + rule %r((""")(\S*)(.*?)("""))m do |m| + open, lang, content, close = m.captures + + token Str, open + token Str::Escape, lang + + sublexer = Rouge::Lexer.find(lang.strip) + + if sublexer + delegate sublexer, content + else + token Str, content + end + + token Str, close + end + end + state :root do mixin :basic rule %r(\n), Text - rule %r(""".*?""")m, Str + mixin :annotated_string rule %r(@[^\s@]+), Name::Tag mixin :has_table mixin :has_examples diff --git a/spec/visual/samples/gherkin b/spec/visual/samples/gherkin index 759e0b6524..39ff064476 100644 --- a/spec/visual/samples/gherkin +++ b/spec/visual/samples/gherkin @@ -79,3 +79,52 @@ Feature: Highlander Then he (or she) will live forever ;-) Rule: There can be Two (in some cases) + + Scenario: Code samples + Given I have some code examples + """ruby + class Calculator + def multiply(x, y) + x * y + end + end + """ + And I have some code examples + """rust + fn safe_divide(x: f64, y: f64) -> Result { + match y { + 0.0 => Err("Division by zero"), + _ => Ok(x / y), + } + } + """ + And I have some code examples + """ocaml + let rec factorial = function + | 0 -> 1 + | n -> n * factorial (n - 1) + + let () = Printf.printf "%d\n" (factorial 5) + """ + And I have some code examples + """json + { + "calculator": { + "operations": ["add", "subtract", "multiply", "divide"], + "precision": 2, + "enabled": true + } + } + """ + And I have some code examples + """nonexistent-lang + proc divide(x, y) { + return x / y; + } + """ + And I have some code examples + """ + def add(a, b) + a + b + end + """