Skip to content

Commit 7e41afa

Browse files
Improve code language detection for ERB and HTML
This improves language detection for code examples that include ERB or HTML tags in a trailing position, such as `Title: <%= @post.title %>`.
1 parent ddf43db commit 7e41afa

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/sdoc/postprocessor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def guess_code_language(code)
130130
else
131131
"yaml"
132132
end
133-
when /^ *<[%a-z]/i
133+
when /^ *<[%a-z]|%>$|<\/\w+>$/i
134134
"erb" # also highlights HTML
135135
else
136136
"ruby"

spec/postprocessor_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@
400400
<%= 1 + 1 %>
401401
ERB
402402

403+
_(SDoc::Postprocessor.guess_code_language(<<~ERB)).must_equal "erb"
404+
1 + 1 = <%= 1 + 1 %>
405+
ERB
406+
403407
_(SDoc::Postprocessor.guess_code_language(<<~ERB)).must_equal "erb"
404408
<% x = 1 + 1 %>
405409
ERB
@@ -413,6 +417,10 @@
413417
_(SDoc::Postprocessor.guess_code_language(<<~HTML)).must_equal "erb"
414418
<p>1 + 1 = 2</p>
415419
HTML
420+
421+
_(SDoc::Postprocessor.guess_code_language(<<~HTML)).must_equal "erb"
422+
1 + 1 = <span>2</span>
423+
HTML
416424
end
417425

418426
it "guesses erb for HTML that includes ERB" do
@@ -432,6 +440,11 @@
432440
Object.new # => #<Object>
433441
RUBY
434442

443+
_(SDoc::Postprocessor.guess_code_language(<<~RUBY)).must_equal "ruby"
444+
Pathname("/span")
445+
# => #<Pathname:/span>
446+
RUBY
447+
435448
_(SDoc::Postprocessor.guess_code_language(<<~RUBY)).must_equal "ruby"
436449
image_tag("image.png")
437450
# => <img src="/assets/image.png" />

0 commit comments

Comments
 (0)