Skip to content

Do not parse non-Ruby files as Ruby when :markup: is markdown or tomdoc - #1772

Open
sjh9714 wants to merge 1 commit into
ruby:masterfrom
sjh9714:codex/20260811-1597-markup-parser-file-name
Open

Do not parse non-Ruby files as Ruby when :markup: is markdown or tomdoc#1772
sjh9714 wants to merge 1 commit into
ruby:masterfrom
sjh9714:codex/20260811-1597-markup-parser-file-name

Conversation

@sjh9714

@sjh9714 sjh9714 commented Aug 11, 2026

Copy link
Copy Markdown

Summary

Fixes #1597.

A file whose first three lines contain :markup: markdown (or tomdoc) was always handed to the Ruby parser: RDoc::Parser.for consults use_markup before any file-name-based selection, and use_markup never sees the file name. The issue's file.c was therefore documented as Ruby source. The buggy line carried # TODO Ruby should be returned only when the filename is correct.

This change passes the file name into use_markup and returns the Ruby parser for these two markups only when the file name actually selects the Ruby parser. Other files fall through to for's existing shebang/extension logic, so file.c is parsed by the C parser — while the :markup: directive still sets the comment format, since that is handled independently by RDoc::Markup::PreProcess (its directive regexp already matches /* */ comments).

Prior art: #1613 attempted this and was self-closed over the parser-vs-format conflation; gating only the parser choice on the file name and leaving format handling to PreProcess avoids that problem.

Behavior notes:

  • .rb/.rbw files and extensionless scripts with a Ruby shebang keep the Ruby parser (the shebang branch in for is unchanged).
  • A README.md with a markup comment was previously also parsed as Ruby; it now falls through to the Markdown parser.
  • use_markup(content) without a file name (existing public API) behaves as before.

Changes

  • RDoc::Parser.use_markup accepts an optional file_name and consults can_parse_by_name for the markdown/tomdoc markups; RDoc::Parser.for passes the file name through.
  • Regression tests: a .c file with /* :markup: markdown */ selects RDoc::Parser::C end to end (fails on master with RDoc::Parser::Ruby), plus use_markup unit coverage for .rb vs .c with both markups.

Testing

  • bundle exec ruby -Ilib -Itest test/rdoc/parser/parser_test.rb
  • bundle exec rake (2520 unit tests + 20 RubyGems integration tests, 100% pass)
  • bundle exec rubocop lib/rdoc/parser.rb test/rdoc/parser/parser_test.rb

RDoc::Parser.for consulted use_markup before any file-name-based
selection, and use_markup unconditionally mapped the markdown and tomdoc
markup formats to the Ruby parser. A C file with /* :markup: markdown */
in its first three lines was therefore parsed as Ruby source.

Pass the file name into use_markup and return the Ruby parser for these
formats only when the file name selects the Ruby parser (or when no file
name is given, preserving the documented behavior for direct callers).
Other files fall through to the regular shebang and extension logic, so
the C file above is parsed as C while the :markup: directive still sets
the comment format.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RDoc parses .c file as ruby code if markup is set to markdown in the first 3 lines

1 participant