Skip to content

Commit

Permalink
lexer: c: Highlight #includes as Comment::PreprocFile
Browse files Browse the repository at this point in the history
When changing https://chrisdown.name over from Pygments to Rouge, one
quite noticeable change was that `{% highlight c %}` blocks lost the
delineation between #include statements and the actual include, with
them all now just being stylised as a comment.

This patch brings back Comment::PreprocFile support for C-like
languages, as it was with Pygments.

Tested locally by rebuilding chrisdown.name with a gem from this commit,
and seeing that the Comment::PreprocFile highlighting reappeared.
  • Loading branch information
cdown committed Dec 27, 2021
1 parent 79468ba commit 4d09736
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/rouge/lexers/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,22 @@ def self.builtins
end

state :macro do
# NB: pop! goes back to :bol
rule %r/\n/, Comment::Preproc, :pop!
mixin :include
rule %r([^/\n\\]+), Comment::Preproc
rule %r/\\./m, Comment::Preproc
mixin :inline_whitespace
rule %r(/), Comment::Preproc
# NB: pop! goes back to :bol
rule %r/\n/, Comment::Preproc, :pop!
end

state :include do
rule %r/(include)(\s*)(<[^>]+>)([^\n]*)/ do
groups Comment::Preproc, Text, Comment::PreprocFile, Comment::Single
end
rule %r/(include)(\s*)("[^"]+")([^\n]*)/ do
groups Comment::Preproc, Text, Comment::PreprocFile, Comment::Single
end
end

state :if_0 do
Expand Down
7 changes: 7 additions & 0 deletions spec/visual/samples/c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ void foo() {
}
}

#include <string.h> /* this is a comment */
#include <string.h> // this is a comment
#include "string.h" /* this is a comment */
#include "string.h" // this is a comment
#include<string>/* this is a comment */
#include<string>// this is a comment

/* Execute compiled code */

/* XXX TO DO:
Expand Down

0 comments on commit 4d09736

Please sign in to comment.