Skip to content

Commit dd5acdb

Browse files
authored
Avoid detecting encoding comments in documentation text (ruby#1727)
RDoc currently reads file content with [`RDoc::Encoding.read_file`](https://github.com/ruby/rdoc/blob/master/lib/rdoc/encoding.rb#L526-L613) during [`RDoc::RDoc#parse_file`](https://github.com/ruby/rdoc/blob/master/lib/rdoc/rdoc.rb#L2033-L2049), before parser selection. The current header regexp starts with `^` and permits arbitrary comment prose before `coding:`, so documentation text can be interpreted as an encoding directive before the selected parser handles the file ([current regexp](https://github.com/ruby/rdoc/blob/master/lib/rdoc/encoding.rb#L487-L507)). Ruby documents `^` as the beginning of a line and `\A` as the beginning of a string, and documents magic comments as top-level directives with plain `encoding` / `coding` syntax or Emacs-style `-*- ... -*-` syntax ([Regexp anchors](https://docs.ruby-lang.org/en/3.4/Regexp.html#class-Regexp-label-Boundary+Anchors), [magic comments](https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-Magic+Comments)). This PR updates RDoc header detection to stay at the file header and limits the `coding:` branch to those magic-comment forms ([updated regexp](https://github.com/st0012/rdoc/blob/codex/avoid-encoding-comment-doc-text/lib/rdoc/encoding.rb#L10-L20)). The regression test covers documentation text that mentions `#encoding: Returns`, `Encoding::US_ASCII.name`, and a later prose `# coding:` line so those examples are treated as documentation text instead of encoding declarations ([test fixture](https://github.com/st0012/rdoc/blob/codex/avoid-encoding-comment-doc-text/test/rdoc/rdoc_encoding_test.rb#L158-L164)).
1 parent 27c3101 commit dd5acdb

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/rdoc/encoding.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
module RDoc::Encoding
99

10-
HEADER_REGEXP = /^
10+
HEADER_REGEXP = /\A
1111
(?:
12-
\A\#!.*\n
12+
\#!.*\n
1313
|
1414
^\#\s+frozen[-_]string[-_]literal[=:].+\n
1515
|
16-
^\#[^\n]+\b(?:en)?coding[=:]\s*(?<name>[^\s;]+).*\n
16+
^\#\s*(?:-\*-\s*(?:[^;\n]*;\s*)*)?(?:en)?coding[=:]\s*(?<name>[^:\s;]+).*\n
1717
|
1818
<\?xml[^?]*encoding=(?<quote>["'])(?<name>.*?)\k<quote>.*\n
1919
)+

test/rdoc/rdoc_encoding_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ def test_class_detect_encoding
155155
assert_equal Encoding::UTF_8, encoding
156156
end
157157

158+
def test_class_detect_encoding_ignores_documentation_comments
159+
document = "# This document describes a method named #encoding: Returns an object.\n" \
160+
"# Encoding::US_ASCII.name # => \"US-ASCII\"\n" \
161+
"# This is a documentation paragraph.\n" \
162+
"# coding: UTF-8 is example text, not a magic comment.\n"
163+
164+
assert_nil RDoc::Encoding.detect_encoding document
165+
end
166+
158167
def test_class_set_encoding_bad
159168
s = ""
160169
encoding = RDoc::Encoding.detect_encoding s

0 commit comments

Comments
 (0)