Skip to content

Commit

Permalink
Fix minted/lstlistings escaping (#1269)
Browse files Browse the repository at this point in the history

Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com>
  • Loading branch information
fredrikekre and mortenpi authored Mar 23, 2020
1 parent 2d2a855 commit 470cf55
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* ![Bugfix][badge-bugfix] Remove `only`, a new export from `Base` on Julia 1.4, from the JS search filter. ([#1264][github-1264])

* ![Bugfix][badge-bugfix] Fix errors in LaTeX builds due to bad escaping of certain characters. ([#1118][github-1118], [#1119][github-1119], [#1200][github-1200], [#1269][github-1269])

## Version `v0.24.6`

* ![Enhancement][badge-enhancement] Reorganize some of the internal variables in Documenter's Sass sources, to make it easier to create custom themes on top of the Documenter base theme. ([#1258][github-1258])
Expand Down Expand Up @@ -500,6 +502,8 @@
[github-1112]: https://github.com/JuliaDocs/Documenter.jl/pull/1112
[github-1113]: https://github.com/JuliaDocs/Documenter.jl/pull/1113
[github-1115]: https://github.com/JuliaDocs/Documenter.jl/pull/1115
[github-1118]: https://github.com/JuliaDocs/Documenter.jl/issues/1118
[github-1119]: https://github.com/JuliaDocs/Documenter.jl/pull/1119
[github-1121]: https://github.com/JuliaDocs/Documenter.jl/pull/1121
[github-1137]: https://github.com/JuliaDocs/Documenter.jl/pull/1137
[github-1144]: https://github.com/JuliaDocs/Documenter.jl/pull/1144
Expand All @@ -516,13 +520,15 @@
[github-1189]: https://github.com/JuliaDocs/Documenter.jl/pull/1189
[github-1194]: https://github.com/JuliaDocs/Documenter.jl/pull/1194
[github-1195]: https://github.com/JuliaDocs/Documenter.jl/pull/1195
[github-1200]: https://github.com/JuliaDocs/Documenter.jl/issues/1200
[github-1212]: https://github.com/JuliaDocs/Documenter.jl/issues/1212
[github-1216]: https://github.com/JuliaDocs/Documenter.jl/pull/1216
[github-1222]: https://github.com/JuliaDocs/Documenter.jl/pull/1222
[github-1223]: https://github.com/JuliaDocs/Documenter.jl/pull/1223
[github-1232]: https://github.com/JuliaDocs/Documenter.jl/pull/1232
[github-1258]: https://github.com/JuliaDocs/Documenter.jl/pull/1258
[github-1264]: https://github.com/JuliaDocs/Documenter.jl/pull/1264
[github-1269]: https://github.com/JuliaDocs/Documenter.jl/pull/1269

[documenterlatex]: https://github.com/JuliaDocs/DocumenterLaTeX.jl
[documentermarkdown]: https://github.com/JuliaDocs/DocumenterMarkdown.jl
Expand Down
36 changes: 29 additions & 7 deletions src/Writers/LaTeXWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,19 +435,41 @@ function latex(io::IO, code::Markdown.Code)
language = isempty(code.language) ? "none" : code.language
# the julia-repl is called "jlcon" in Pygments
language = (language == "julia-repl") ? "jlcon" : language
escape = '' code.code
if language in LEXER
_print(io, "\n\\begin{minted}[escapeinside=\\%\\%]")
_print(io, "\n\\begin{minted}")
if escape
_print(io, "[escapeinside=\\#\\%]")
end
_println(io, "{", language, "}")
_print_code_escapes(io, code.code)
_println(io, "\\end{minted}\n")
if escape
_print_code_escapes_minted(io, code.code)
else
_print(io, code.code)
end
_println(io, "\n\\end{minted}\n")
else
_println(io, "\n\\begin{lstlisting}[escapeinside=\\%\\%]")
_print_code_escapes(io, code.code)
_println(io, "\\end{lstlisting}\n")
_print(io, "\n\\begin{lstlisting}")
if escape
_println(io, "[escapeinside=\\%\\%]")
_print_code_escapes_lstlisting(io, code.code)
else
_println(io)
_print(io, code.code)
end
_println(io, "\n\\end{lstlisting}\n")
end
end

function _print_code_escapes(io, s::AbstractString)
function _print_code_escapes_minted(io, s::AbstractString)
for ch in s
ch === '#' ? _print(io, "##%") :
ch === '%' ? _print(io, "#%%") : # Note: "#\\%%" results in pygmentize error...
ch === '' ? _print(io, "#\\unicodeveebar%") :
_print(io, ch)
end
end
function _print_code_escapes_lstlisting(io, s::AbstractString)
for ch in s
ch === '%' ? _print(io, "%\\%%") :
ch === '' ? _print(io, "%\\unicodeveebar%") :
Expand Down
12 changes: 12 additions & 0 deletions test/examples/src.latex_simple/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@

This build only contains a single paragraph of text to make sure that a
near-empty LaTeX builds passes.

```julia-repl
julia> 127 % Int8
127
```
## Issue 1119

```julia
1 % 2
1 2
1 | 2
```

0 comments on commit 470cf55

Please sign in to comment.