Skip to content

Commit

Permalink
Assume that docstring code with no lang is julia
Browse files Browse the repository at this point in the history
By making this assumption when parsing docstrings, we can remove the
assumption that all no-lang Markdown code blocks are in fact Julia
without compromising docstring highlighting.
  • Loading branch information
tecosaur committed Aug 11, 2024
1 parent d302272 commit 6fc2915
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function term(io::AnnotIO, md::Header{l}, columns) where l
end

function term(io::IO, md::Code, columns)
code = if md.language ("", "julia")
code = if md.language == "julia"
highlight(md.code)
elseif md.language == "julia-repl" || Base.startswith(md.language, "jldoctest")
hl = AnnotatedString(md.code)
Expand Down
24 changes: 23 additions & 1 deletion stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function formatdoc(d::DocStr)
for part in d.text
formatdoc(buffer, d, part)
end
Markdown.MD(Any[Markdown.parse(seekstart(buffer))])
md = Markdown.MD(Any[Markdown.parse(seekstart(buffer))])
assume_julia_code!(md)
end
@noinline formatdoc(buffer, d, part) = print(buffer, part)

Expand All @@ -95,6 +96,27 @@ function parsedoc(d::DocStr)
d.object
end

"""
assume_julia_code!(doc::Markdown.MD) -> doc
Assume that code blocks with no language specified are Julia code.
"""
function assume_julia_code!(doc::Markdown.MD)
assume_julia_code!(doc.content)
doc
end

function assume_julia_code!(blocks::Vector)
for (i, block) in enumerate(blocks)
if block isa Markdown.Code && block.language == ""
blocks[i] = Markdown.Code("julia", block.code)
elseif block isa Vector || block isa Markdown.MD
assume_julia_code!(block)
end
end
blocks
end

## Trimming long help ("# Extended help")

struct Message # For direct messages to the terminal
Expand Down

0 comments on commit 6fc2915

Please sign in to comment.