-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* adding experimental integration for literate, closes #256
- Loading branch information
Showing
9 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const LITERATE_JULIA_FENCE = "```julia" | ||
const LITERATE_JULIA_FENCE_L = length(LITERATE_JULIA_FENCE) | ||
const LITERATE_JULIA_FENCE_R = Regex(LITERATE_JULIA_FENCE) | ||
|
||
""" | ||
$SIGNATURES | ||
Take a markdown string generated by literate and post-process it to number each code block | ||
and mark them as eval-ed ones. | ||
""" | ||
function literate_post_process(s::String)::String | ||
isempty(s) && return s | ||
em = eachmatch(LITERATE_JULIA_FENCE_R, s) | ||
buf = IOBuffer() | ||
head = 1 | ||
c = 1 | ||
for m in em | ||
write(buf, SubString(s, head, prevind(s, m.offset))) | ||
write(buf, "```julia:ex$c\n") | ||
head = nextind(s, m.offset + LITERATE_JULIA_FENCE_L) | ||
c += 1 | ||
end | ||
lis = lastindex(s) | ||
head < lis && write(buf, SubString(s, head, lis)) | ||
return String(take!(buf)) | ||
end | ||
|
||
|
||
""" | ||
$SIGNATURES | ||
Take a Literate.jl script and transform it into a JuDoc-markdown file. | ||
""" | ||
function literate_to_judoc(fpath::String)::String | ||
outpath = joinpath(PATHS[:assets], "literate") | ||
isdir(outpath) || mkdir(outpath) | ||
# don't show infos | ||
Logging.disable_logging(Logging.LogLevel(Logging.Info)) | ||
markdown(fpath, outpath, documenter=false, | ||
postprocess=literate_post_process, credit=false) | ||
# bring back logging | ||
Logging.disable_logging(Logging.LogLevel(Logging.Debug)) | ||
fname = splitdir(fpath)[2] | ||
return joinpath(outpath, splitext(fname)[1] * ".md") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
scripts = joinpath(J.PATHS[:folder], "scripts") | ||
cd(td); J.set_paths!(); mkpath(scripts) | ||
|
||
@testset "Literate-a" begin | ||
# Post processing: numbering of julia blocks | ||
s = raw""" | ||
A | ||
```julia | ||
B | ||
``` | ||
C | ||
```julia | ||
D | ||
``` | ||
""" | ||
@test J.literate_post_process(s) == """ | ||
A | ||
```julia:ex1 | ||
B | ||
``` | ||
C | ||
```julia:ex2 | ||
D | ||
``` | ||
""" | ||
end | ||
|
||
@testset "Literate-b" begin | ||
# Literate to JuDoc | ||
s = raw""" | ||
# # Rational numbers | ||
# | ||
# In julia rational numbers can be constructed with the `//` operator. | ||
# Lets define two rational numbers, `x` and `y`: | ||
## Define variable x and y | ||
x = 1//3 | ||
y = 2//5 | ||
# When adding `x` and `y` together we obtain a new rational number: | ||
z = x + y | ||
""" | ||
path = joinpath(scripts, "tutorial.jl") | ||
write(path, s) | ||
opath = J.literate_to_judoc(path) | ||
@test endswith(opath, joinpath(J.PATHS[:assets], "literate", "tutorial.md")) | ||
out = read(opath, String) | ||
@test out == """ | ||
# Rational numbers | ||
In julia rational numbers can be constructed with the `//` operator. | ||
Lets define two rational numbers, `x` and `y`: | ||
```julia:ex1 | ||
# Define variable x and y | ||
x = 1//3 | ||
y = 2//5 | ||
``` | ||
When adding `x` and `y` together we obtain a new rational number: | ||
```julia:ex2 | ||
z = x + y | ||
``` | ||
""" | ||
|
||
# Use of `\literate` command | ||
h = raw""" | ||
@def hascode = true | ||
@def showall = true | ||
\literate{/scripts/tutorial.jl} | ||
""" |> jd2html_td | ||
@test isapproxstr(h, """ | ||
<h1 id="rational_numbers"><a href="/index.html#rational_numbers">Rational numbers</a></h1> | ||
<p>In julia rational numbers can be constructed with the <code>//</code> operator. Lets define two rational numbers, <code>x</code> and <code>y</code>:</p> | ||
<pre><code class="language-julia"># Define variable x and y | ||
x = 1//3 | ||
y = 2//5</code></pre> | ||
<div class="code_output"><pre><code>2//5</code></pre></div> | ||
<p>When adding <code>x</code> and <code>y</code> together we obtain a new rational number:</p> | ||
<pre><code class="language-julia">z = x + y</code></pre> | ||
<div class="code_output"><pre><code>11//15</code></pre></div> | ||
""") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters