Skip to content

Commit e9ab533

Browse files
authored
Merge pull request #475 from aycabta/fix-backtick-handling
Fix backtick handling
2 parents 8525c79 + 4035a1c commit e9ab533

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ def identify_string(ltype, quoted = ltype, type = nil)
13191319
@ltype = ltype
13201320
@quoted = quoted
13211321

1322-
str = if ltype == quoted and %w[" ' /].include? ltype then
1322+
str = if ltype == quoted and %w[" ' / `].include? ltype then
13231323
ltype.dup
13241324
else
13251325
"%#{type}#{PERCENT_PAREN_REV[quoted]||quoted}"

lib/rdoc/token_stream.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def self.to_html token_stream
4040
when RDoc::RubyToken::TkDREGEXP then 'ruby-regexp'
4141
when RDoc::RubyToken::TkNode then 'ruby-node'
4242
when RDoc::RubyToken::TkCOMMENT then 'ruby-comment'
43+
when RDoc::RubyToken::TkXSTRING then 'ruby-string'
4344
when RDoc::RubyToken::TkSTRING then 'ruby-string'
4445
when RDoc::RubyToken::TkVal then 'ruby-value'
4546
end

test/test_rdoc_markup_to_html.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,46 @@ def bar
545545
assert_equal expected, @to.res.join
546546
end
547547

548+
def test_accept_verbatim_escape_in_backtick
549+
code = <<-'RUBY'
550+
def foo
551+
[
552+
`\\`,
553+
`\'\"\``,
554+
`\#`,
555+
`\#{}`,
556+
`#`,
557+
`#{}`
558+
]
559+
end
560+
def bar
561+
end
562+
RUBY
563+
verb = @RM::Verbatim.new(*code.split(/(?<=\n)/))
564+
565+
@to.start_accepting
566+
@to.accept_verbatim verb
567+
568+
expected = <<-'EXPECTED'
569+
570+
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier">foo</span>
571+
[
572+
<span class="ruby-string">`\\`</span>,
573+
<span class="ruby-string">`\&#39;\&quot;\``</span>,
574+
<span class="ruby-string">`\#`</span>,
575+
<span class="ruby-string">`\#{}`</span>,
576+
<span class="ruby-string">`#`</span>,
577+
<span class="ruby-node">`#{}`</span>
578+
]
579+
<span class="ruby-keyword">end</span>
580+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">bar</span>
581+
<span class="ruby-keyword">end</span>
582+
</pre>
583+
EXPECTED
584+
585+
assert_equal expected, @to.res.join
586+
end
587+
548588
def test_accept_verbatim_ruby
549589
verb = @RM::Verbatim.new("1 + 1\n")
550590
verb.format = :ruby

test/test_rdoc_ruby_lex.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,51 @@ def test_class_tokenize_string_with_escape
719719
assert_equal expected, tokens
720720
end
721721

722+
def test_class_tokenize_backtick_with_escape
723+
tokens = RDoc::RubyLex.tokenize <<'RUBY', nil
724+
[
725+
`\\`,
726+
`\'\"\``,
727+
`\#`,
728+
`\#{}`,
729+
`#`,
730+
`#{}`
731+
]
732+
RUBY
733+
734+
expected = [
735+
@TK::TkLBRACK .new( 0, 1, 0, "["),
736+
@TK::TkNL .new( 1, 1, 1, "\n"),
737+
@TK::TkSPACE .new( 2, 2, 0, " "),
738+
@TK::TkXSTRING .new( 4, 2, 2, "`\\\\`"),
739+
@TK::TkCOMMA .new( 8, 2, 6, ","),
740+
@TK::TkNL .new( 9, 2, 2, "\n"),
741+
@TK::TkSPACE .new(10, 3, 0, " "),
742+
@TK::TkXSTRING .new(12, 3, 2, "`\\'\\\"\\``"),
743+
@TK::TkCOMMA .new(20, 3, 10, ","),
744+
@TK::TkNL .new(21, 3, 10, "\n"),
745+
@TK::TkSPACE .new(22, 4, 0, " "),
746+
@TK::TkXSTRING .new(24, 4, 2, "`\\#`"),
747+
@TK::TkCOMMA .new(28, 4, 6, ","),
748+
@TK::TkNL .new(29, 4, 22, "\n"),
749+
@TK::TkSPACE .new(30, 5, 0, " "),
750+
@TK::TkXSTRING .new(32, 5, 2, "`\\\#{}`"),
751+
@TK::TkCOMMA .new(38, 5, 8, ","),
752+
@TK::TkNL .new(39, 5, 30, "\n"),
753+
@TK::TkSPACE .new(40, 6, 0, " "),
754+
@TK::TkXSTRING .new(42, 6, 2, "`#`"),
755+
@TK::TkCOMMA .new(45, 6, 5, ","),
756+
@TK::TkNL .new(46, 6, 40, "\n"),
757+
@TK::TkSPACE .new(47, 7, 0, " "),
758+
@TK::TkDXSTRING.new(49, 7, 2, "`\#{}`"),
759+
@TK::TkNL .new(54, 7, 7, "\n"),
760+
@TK::TkRBRACK .new(55, 8, 0, "]"),
761+
@TK::TkNL .new(56, 8, 55, "\n")
762+
]
763+
764+
assert_equal expected, tokens
765+
end
766+
722767
def test_class_tokenize_string_escape
723768
tokens = RDoc::RubyLex.tokenize '"\\n"', nil
724769
assert_equal @TK::TkSTRING.new( 0, 1, 0, "\"\\n\""), tokens.first

0 commit comments

Comments
 (0)