From f136c6960c31d95a24a0a962775baafd754c54f2 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 16:42:45 -0500 Subject: [PATCH 1/6] Fix method ambiguity with JuliaValue interpolation --- src/writer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/writer.jl b/src/writer.jl index c101a38b..486223f2 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -388,7 +388,7 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod println(io, "\">") end # Interpolated Julia values -function render(io, mime, node::MarkdownAST.Node, value::MarkdownAST.JuliaValue, page, doc) +function render(io::IO, mime::MIME"text/plain", node::MarkdownAST.Node, value::MarkdownAST.JuliaValue, page, doc) @warn(""" Unexpected Julia interpolation in the Markdown. This probably means that you have an unbalanced or un-escaped \$ in the text. From a82cf76b1b1aad7c330e2756564134df4253af4f Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 16:49:37 -0500 Subject: [PATCH 2/6] Render `Documenter.PageLink`s correctly This was causing a bug where a lot of spam would get printed, since the object is fairly eggy :D --- src/writer.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/writer.jl b/src/writer.jl index 486223f2..39eb02a7 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -361,7 +361,7 @@ end # end # end - +# TODO: list ordering is broken! function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, list::MarkdownAST.List, page, doc) # @infiltrate if list.type === :ordered @@ -400,4 +400,17 @@ function render(io::IO, mime::MIME"text/plain", node::MarkdownAST.Node, value::M `$(value.ref)` which is of type `$(typeof(value.ref))` """) println(io, value.ref) -end \ No newline at end of file +end + +# Documenter.jl page links +function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, link::Documenter.PageLink, page, doc) + # @infiltrate + path = if !isempty(link.fragment) + string(hash(link.fragment)) + else + Documenter.pagekey(io.doc, link.page) + end + print(io, "") + render(io, mime, node, node.children, page, doc) + print(io, "") +end From 587b00de39c164bad7def4c18dd712e05d0a1759 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 17:41:48 -0500 Subject: [PATCH 3/6] Render local links --- src/writer.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/writer.jl b/src/writer.jl index 39eb02a7..f73c36a6 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -414,3 +414,14 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod render(io, mime, node, node.children, page, doc) print(io, "") end + +# Documenter.jl local links +function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, link::Documenter.LocalLink, page, doc) + # @infiltrate + href = isempty(link.fragment) ? link.path : "$(link.path)#($(link.fragment))" + print(io, "") + render(io, mime, node, node.children, page, doc) + print(io, "") +end + + From 73a93d59c3688828c2bfcf7e5d16b17bcbd023a4 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 17:42:00 -0500 Subject: [PATCH 4/6] Remove XML descriptor from SVGs --- src/writer.jl | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/writer.jl b/src/writer.jl index f73c36a6..e0fb5618 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -204,7 +204,15 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod # as browsers seem to need to have the xmlns attribute set in the tag if you # want to include it with . However, setting that attribute is up to the code # creating the SVG image. - println(io, d[MIME"image/svg+xml"()]) + image_text = d[MIME"image/svg+xml"()] + # Additionally, Vitepress complains about the XML version and encoding string below, + # so we just remove this bad hombre! + bad_hombre_string = "" |> lowercase + location = findfirst(bad_hombre_string, lowercase(image_text)) + if !isnothing(location) + image_text = replace(image_text, image_text[location] => "") + end + println(io, image_text) elseif haskey(d, MIME"image/png"()) write(joinpath(dirname(page.build), "$(filename).png"), base64decode(d[MIME"image/png"()])) @@ -333,34 +341,6 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod println(io, "\n:::") end # Lists - -# function latex(io::Context, node::Node, list::MarkdownAST.List) -# # TODO: MarkdownAST doesn't support lists starting at arbitrary numbers -# isordered = (list.type === :ordered) -# ordered = (list.type === :bullet) ? -1 : 1 -# # `\begin{itemize}` is used here for both ordered and unordered lists since providing -# # custom starting numbers for enumerated lists is simpler to do by manually assigning -# # each number to `\item` ourselves rather than using `\setcounter{enumi}{}`. -# # -# # For an ordered list starting at 5 the following will be generated: -# # -# # \begin{itemize} -# # \item[5. ] ... -# # \item[6. ] ... -# # ... -# # \end{itemize} -# # -# pad = ndigits(ordered + length(node.children)) + 2 -# fmt = n -> (isordered ? "[$(rpad("$(n + ordered - 1).", pad))]" : "") -# wrapblock(io, "itemize") do -# for (n, item) in enumerate(node.children) -# _print(io, "\\item$(fmt(n)) ") -# latex(io, item.children) -# n < length(node.children) && _println(io) -# end -# end -# end - # TODO: list ordering is broken! function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, list::MarkdownAST.List, page, doc) # @infiltrate @@ -387,6 +367,17 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod render(io, mime, node, node.children, page, doc) println(io, "\">") end + +# Footnote links +# TODO: not handled yet +# We would have to keep track of all footnotes per page +# probably handled best in `page.meta` or so +# but see e.g. +# function latex(io::Context, node::Node, f::MarkdownAST.FootnoteLink) +# id = get!(io.footnotes, f.id, length(io.footnotes) + 1) +# _print(io, "\\footnotemark[", id, "]") +# end + # Interpolated Julia values function render(io::IO, mime::MIME"text/plain", node::MarkdownAST.Node, value::MarkdownAST.JuliaValue, page, doc) @warn(""" From 774939ded9249dbb5a1eb4dcdb7dd69d5563c2b8 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 18:28:20 -0500 Subject: [PATCH 5/6] Correct code highlighting in ansi blocks --- src/writer.jl | 65 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/src/writer.jl b/src/writer.jl index e0fb5618..9df53e37 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -164,18 +164,61 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod return evalnode.result === nothing ? nothing : render(io, mime, node, evalnode.result, page, doc) end +function intelligent_language(lang::String) + if lang == "ansi" + "julia" + elseif lang == "documenter-ansi" + "ansi" + else + lang + end +end + function join_multiblock(mcb::Documenter.MultiCodeBlock) - io = IOBuffer() - for (i, thing) in enumerate(mcb.content) - print(io, thing.code) - if i != length(mcb.content) - println(io) - if findnext(x -> x.language == mcb.language, mcb.content, i + 1) == i + 1 - println(io) + if mcb.language == "ansi" + # Return a vector of Markdown code blocks + # where each block is a single line of the output or input. + # Basically, we iterate through the code, + # and whenever the language changes, we + # start a new code block and push the old one to the array! + codes = Markdown.Code[] + current_language = first(mcb.content).language + current_string = "" + for thing in mcb.content + # reset the buffer and push the old code block + if thing.language != current_language + # Remove this if statement if you want to + # include empty code blocks in the output. + if isempty(thing.code) + current_string *= "\n\n" + continue + end + push!(codes, Markdown.Code(intelligent_language(current_language), current_string)) + current_string = "" + current_language = thing.language # reset the current language end + # push the current code to `io` + current_string *= thing.code end + # push the last code block + push!(codes, Markdown.Code(intelligent_language(current_language), current_string)) + Main.@infiltrate + return codes + end - return Markdown.Code(mcb.language, String(take!(io))) + # else + io = IOBuffer() + for (i, thing) in enumerate(mcb.content) + print(io, thing.code) + if i != length(mcb.content) + println(io) + if findnext(x -> x.language == mcb.language, mcb.content, i + 1) == i + 1 + println(io) + end + end + end + return Markdown.Code(mcb.language, String(take!(io))) + # end end function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, mcb::Documenter.MultiCodeBlock, page, doc) @@ -315,7 +358,11 @@ function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Nod end # Code blocks function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, code::MarkdownAST.CodeBlock, page, doc) - render(io, mime, node, Markdown.Code(code.info, code.code), page, doc) + info = code.info + if info == "julia-repl" + info = "julia" + end + render(io, mime, node, Markdown.Code(info, code.code), page, doc) end # Inline code function render(io::IO, mime::MIME"text/plain", node::Documenter.MarkdownAST.Node, code::MarkdownAST.Code, page, doc) From 669bfe6bad5679e28ea65e16310d517e143da2ef Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 16 Feb 2024 18:30:33 -0500 Subject: [PATCH 6/6] Remove infiltrate again --- src/writer.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/writer.jl b/src/writer.jl index 9df53e37..27b5f1e7 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -202,7 +202,6 @@ function join_multiblock(mcb::Documenter.MultiCodeBlock) end # push the last code block push!(codes, Markdown.Code(intelligent_language(current_language), current_string)) - Main.@infiltrate return codes end