Skip to content

Commit 8f5fa21

Browse files
committed
fix code blocks
1 parent 8e7a868 commit 8f5fa21

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

.devcontainer/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ RUN pip install pyyaml
2121
RUN pip install watchdog
2222

2323
RUN yarn global add @antora/lunr-extension
24+
RUN yarn global add @antora/collector-extension
2425

2526
RUN apk add --no-cache curl tar xz \
2627
&& curl -L https://github.com/cppalliance/mrdocs/releases/download/v${MRDOCS_VERSION}/MrDocs-${MRDOCS_VERSION}-Linux.tar.xz \
@@ -30,5 +31,10 @@ RUN apk add --no-cache curl tar xz \
3031
&& rm /tmp/mrdocs.tar.xz \
3132
&& ln -s /opt/mrdocs/bin/mrdocs /usr/local/bin/mrdocs
3233

34+
# Patch code blocks for use with rouge
35+
COPY .devcontainer/code-block.patch /opt/mrdocs/code-block.patch
36+
RUN patch -p1 -d /opt/mrdocs < /opt/mrdocs/code-block.patch
37+
RUN rm /opt/mrdocs/code-block.patch
38+
3339
USER node
3440

.devcontainer/code-block.patch

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
diff --git a/share/mrdocs/addons/generator/adoc/partials/markup/code-block.adoc.hbs b/share/mrdocs/addons/generator/adoc/partials/markup/code-block.adoc.hbs
2+
index 6037d1a7..71ba6ad3 100644
3+
--- a/share/mrdocs/addons/generator/adoc/partials/markup/code-block.adoc.hbs
4+
+++ b/share/mrdocs/addons/generator/adoc/partials/markup/code-block.adoc.hbs
5+
@@ -1,4 +1,4 @@
6+
-[source,cpp,subs="verbatim,replacements,macros,-callouts"]
7+
+[source,cpp,subs="verbatim,-replacements,+macros,+callouts"]
8+
----
9+
{{> @partial-block }}
10+

tools/rehighlight.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,25 @@ class Cpp
107107
# Strip any "language-" prefix just in case
108108
lang = lang.sub(/^language-/, '')
109109

110-
source = code_node.text
110+
source_html = code_node.inner_html
111+
112+
links = []
113+
source = source_html.gsub(/<a[^>]*class="xref[^"]*"[^>]*>.*?<\/a>/) do |link|
114+
token = "ROUGE_LINK_#{links.length}"
115+
links << link
116+
token
117+
end
118+
source = Nokogiri::HTML.fragment(source).text
111119

112120
lexer = Rouge::Lexer.find_fancy(lang, source) || Rouge::Lexers::PlainText.new
113121
formatter = Rouge::Formatters::HTML.new
114122

115123
if diff_enabled
116124
highlighted_html = formatter.format(lexer.lex(source))
125+
links.each_with_index do |link, i|
126+
highlighted_html.gsub!("ROUGE_LINK_#{i}", link)
127+
end
128+
117129
source_lines = source.lines.map(&:chomp)
118130
highlighted_lines = highlighted_html.lines.map(&:chomp)
119131

@@ -140,6 +152,10 @@ class Cpp
140152
changed = true
141153
else
142154
highlighted = formatter.format(lexer.lex(source))
155+
links.each_with_index do |link, i|
156+
highlighted.gsub!("ROUGE_LINK_#{i}", link)
157+
end
158+
143159
frag = Nokogiri::HTML::DocumentFragment.parse(
144160
"<div class='rouge'><pre><code class='highlight #{lang}'>#{highlighted}</code></pre></div>"
145161
)

0 commit comments

Comments
 (0)