Skip to content

Commit 08f83ba

Browse files
jneenlukeredmorejneen
authored
Gherkin annotated strings (#2252)
* Support annotated strings in Gherkin lexer * move the visual spec into the visual spec file and out of the demo * style and don't re-scan triple quotes * delete a too-brittle spec in favour of the visual spec --------- Co-authored-by: Luke Redmore <[email protected]> Co-authored-by: jneen <[email protected]>
1 parent 8794879 commit 08f83ba

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

lib/rouge/lexers/gherkin.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,29 @@ def self.detect?(text)
2828
rule %r/[ \r\t]+/, Text
2929
end
3030

31+
state :annotated_string do
32+
rule %r((""")(\S*)(.*?)("""))m do |m|
33+
open, lang, content, close = m.captures
34+
35+
token Str, open
36+
token Str::Escape, lang
37+
38+
sublexer = Rouge::Lexer.find(lang.strip)
39+
40+
if sublexer
41+
delegate sublexer, content
42+
else
43+
token Str, content
44+
end
45+
46+
token Str, close
47+
end
48+
end
49+
3150
state :root do
3251
mixin :basic
3352
rule %r(\n), Text
34-
rule %r(""".*?""")m, Str
53+
mixin :annotated_string
3554
rule %r(@[^\s@]+), Name::Tag
3655
mixin :has_table
3756
mixin :has_examples

spec/visual/samples/gherkin

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,52 @@ Feature: Highlander
7979
Then he (or she) will live forever ;-)
8080

8181
Rule: There can be Two (in some cases)
82+
83+
Scenario: Code samples
84+
Given I have some code examples
85+
"""ruby
86+
class Calculator
87+
def multiply(x, y)
88+
x * y
89+
end
90+
end
91+
"""
92+
And I have some code examples
93+
"""rust
94+
fn safe_divide(x: f64, y: f64) -> Result<f64, &'static str> {
95+
match y {
96+
0.0 => Err("Division by zero"),
97+
_ => Ok(x / y),
98+
}
99+
}
100+
"""
101+
And I have some code examples
102+
"""ocaml
103+
let rec factorial = function
104+
| 0 -> 1
105+
| n -> n * factorial (n - 1)
106+
107+
let () = Printf.printf "%d\n" (factorial 5)
108+
"""
109+
And I have some code examples
110+
"""json
111+
{
112+
"calculator": {
113+
"operations": ["add", "subtract", "multiply", "divide"],
114+
"precision": 2,
115+
"enabled": true
116+
}
117+
}
118+
"""
119+
And I have some code examples
120+
"""nonexistent-lang
121+
proc divide(x, y) {
122+
return x / y;
123+
}
124+
"""
125+
And I have some code examples
126+
"""
127+
def add(a, b)
128+
a + b
129+
end
130+
"""

0 commit comments

Comments
 (0)