Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix in-line math output in HTMLWriter #2281

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

* Fixed display of inline LaTeX math `$ ... $` in HTMLWriter ([#2281])
mortenpi marked this conversation as resolved.
Show resolved Hide resolved

## Version [v1.0.1] - 2023-09-18

### Fixed
Expand Down Expand Up @@ -1683,6 +1689,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#2259]: https://github.com/JuliaDocs/Documenter.jl/issues/2259
[#2260]: https://github.com/JuliaDocs/Documenter.jl/issues/2260
[#2269]: https://github.com/JuliaDocs/Documenter.jl/issues/2269
[#2281]: https://github.com/JuliaDocs/Documenter.jl/issues/2281
[JuliaLang/julia#29344]: https://github.com/JuliaLang/julia/issues/29344
[JuliaLang/julia#36953]: https://github.com/JuliaLang/julia/issues/36953
[JuliaLang/julia#38054]: https://github.com/JuliaLang/julia/issues/38054
Expand Down
29 changes: 21 additions & 8 deletions src/html/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2389,17 +2389,15 @@
end
# Check for some 'fallback' MIMEs, defaulting to 'text/plain' if we can't find any of them.
return if haskey(d, MIME"text/latex"())
# If the show(io, ::MIME"text/latex", x) output is already wrapped in \[ ... \] or $$ ... $$, we
# unwrap it first, since when we output Markdown.LaTeX objects we put the correct
# delimiters around it anyway.
latex = d[MIME"text/latex"()]
# Make sure to match multiline strings!
m_bracket = match(r"\s*\\\[(.*)\\\]\s*"s, latex)
m_dollars = match(r"\s*\$\$(.*)\$\$\s*"s, latex)
out = if m_bracket === nothing && m_dollars === nothing
# If the show(io, ::MIME"text/latex", x) output is already wrapped in
# \[ ... \] or $$ ... $$, we unwrap it first, since when we output
# Markdown.LaTeX objects we put the correct delimiters around it anyway.
has_math, latex = _strip_latex_math_delimiters(latex)
out = if !has_math

Check warning on line 2397 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2396-L2397

Added lines #L2396 - L2397 were not covered by tests
Documenter.mdparse(latex; mode = :single)
else
[MarkdownAST.@ast MarkdownAST.DisplayMath(m_bracket !== nothing ? m_bracket[1] : m_dollars[1])]
[MarkdownAST.@ast MarkdownAST.DisplayMath(latex)]

Check warning on line 2400 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2400

Added line #L2400 was not covered by tests
end
domify(dctx, out)
elseif haskey(d, MIME"text/markdown"())
Expand All @@ -2415,6 +2413,21 @@
end
end

function _strip_latex_math_delimiters(latex::AbstractString)

Check warning on line 2416 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2416

Added line #L2416 was not covered by tests
# Make sure to match multiline strings!
m_bracket = match(r"\s*\\\[(.*)\\\]\s*"s, latex)
if m_bracket !== nothing
return true, m_bracket[1]

Check warning on line 2420 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2418-L2420

Added lines #L2418 - L2420 were not covered by tests
end
# This regex matches the strings "$ ... $" and "$$ ... $$", but not
# strings with miss-matched dollar signs such as "$ ... $$" or "$$ ... $"
m_dollars = match(r"^\s*(\${1,2})([^\$]*)\1\s*$"s, latex)
if m_dollars !== nothing
return true, m_dollars[2]

Check warning on line 2426 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2424-L2426

Added lines #L2424 - L2426 were not covered by tests
end
return false, latex

Check warning on line 2428 in src/html/HTMLWriter.jl

View check run for this annotation

Codecov / codecov/patch

src/html/HTMLWriter.jl#L2428

Added line #L2428 was not covered by tests
end

function domify_show_image_binary(dctx::DCtx, filetype::AbstractString, d::Dict{MIME,Any})
@tags img
mime_name = "image/$filetype"
Expand Down
48 changes: 47 additions & 1 deletion test/htmlwriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test
import MarkdownAST
using Documenter
using Documenter: DocSystem
using Documenter.HTMLWriter: HTMLWriter, generate_version_file, generate_redirect_file, expand_versions
using Documenter.HTMLWriter: HTMLWriter, generate_version_file, generate_redirect_file, expand_versions, _strip_latex_math_delimiters

function verify_version_file(versionfile, entries)
@test isfile(versionfile)
Expand Down Expand Up @@ -295,5 +295,51 @@ end
@test html.size_threshold == 12345
@test html.size_threshold_warn == 1234
end

@testset "HTML: _strip_latex_math_delimiters" begin
for content in [
"a",
"x_1",
"x_{1} + x_{2}",
"\\begin{array}x_1\\\nx_2\\end{array}",
]
for (left, right) in [("\\[", "\\]"), ("\$", "\$"), ("\$\$", "\$\$")]
for (input, output) in [
content => (false, content),
"$left$content$right" => (true, content),
" $left$content$right" => (true, content),
"$left$content$right " => (true, content),
"\t$left$content$right " => (true, content),
" \t$left$content$right\t\t" => (true, content),
]
@test _strip_latex_math_delimiters(input) == output
end
end
# Test that miss-matched delimiters are not treated as math
# delimiters
for (left, right) in [
("\\[", ""),
("\$", ""),
("\$\$", ""),
("", "\\]"),
("", "\$"),
("", "\$\$"),
("\$", "\\]"),
("\$\$", "\$"),
("\$", "\$\$"),
]
for input in [
content,
"$left$content$right",
" $left$content$right",
"$left$content$right ",
"\t$left$content$right ",
" \t$left$content$right\t\t",
]
@test _strip_latex_math_delimiters(input) == (false, input)
end
end
end
end
odow marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading