Skip to content

Commit

Permalink
Specially render "styled" Markdown code blocks
Browse files Browse the repository at this point in the history
Since we're already using StyledStrings for rendering Julia in the
terminal, we can also handle "styled"-labelled code blocks fancily,
thanks to the `styled` function provided by StyledStrings. In all
non-terminal contexts, the styling metadata is simply discarded, but
could be used in the future (for instance StyledStrings currently
supports HTML output too).
  • Loading branch information
tecosaur authored and lazarusA committed Jul 12, 2024
1 parent e758f2c commit 4c33082
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Markdown
import Base: AnnotatedString, AnnotatedIOBuffer, show, ==, with_output_color, mapany
using Base64: stringmime

using StyledStrings: StyledStrings, Face, addface!, @styled_str
using StyledStrings: StyledStrings, Face, addface!, @styled_str, styled
using JuliaSyntaxHighlighting: highlight, highlight!

# Margin for printing in terminal.
Expand Down
6 changes: 6 additions & 0 deletions stdlib/Markdown/src/render/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ end

function html(io::IO, code::Code)
withtag(io, :pre) do
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
maybe_lang = !isempty(code.language) ? Any[:class=>"language-$(code.language)"] : []
withtag(io, :code, maybe_lang...) do
htmlesc(io, code.code)
Expand Down Expand Up @@ -134,6 +137,9 @@ function htmlinline(io::IO, content::Vector)
end

function htmlinline(io::IO, code::Code)
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
withtag(io, :code) do
htmlesc(io, code.code)
end
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Markdown/src/render/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function latex(io::IO, header::Header{l}) where l
end

function latex(io::IO, code::Code)
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
occursin("\\end{verbatim}", code.code) && error("Cannot include \"\\end{verbatim}\" in a latex code block")
wrapblock(io, "verbatim") do
println(io, code.code)
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Markdown/src/render/rst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function rst(io::IO, code::Code)
elseif code.language in ("", "julia", "julia-repl")
println(io, ".. code-block:: julia\n")
elseif code.language == "rst"
elseif code.language == "styled"
code = Code("", String(styled(code.code)))
println(io, "::\n")
else
println(io, "::\n")
end
Expand Down
9 changes: 8 additions & 1 deletion stdlib/Markdown/src/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ end
function term(io::IO, md::Code, columns)
code = if md.language ("", "julia", "julia-repl", "jldoctest")
highlight(md.code)
elseif md.language == "styled"
styled(md.code)
else
styled"{markdown_code:$(md.code)}"
end
Expand Down Expand Up @@ -185,7 +187,12 @@ function terminline(io::AnnotIO, md::Link)
end

function terminline(io::IO, code::Code)
print(io, styled"{markdown_inlinecode:$(code.code)}")
body = if code.language == "styled"
styled(code.code)
else
code.code
end
print(io, styled"{markdown_inlinecode:$body}")
end

function terminline(io::IO, tex::LaTeX)
Expand Down

0 comments on commit 4c33082

Please sign in to comment.