Commit dd5acdb
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
158 | 167 | | |
159 | 168 | | |
160 | 169 | | |
| |||
0 commit comments