Skip to content

Commit

Permalink
adding a \show method for cell result, closes #254
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart committed Oct 9, 2019
1 parent a261aeb commit 95d0797
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/converter/lx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ $(SIGNATURES)
Internal function to read the content of a script file. See also [`resolve_lx_input`](@ref).
"""
function resolve_lx_input_hlcode(rpath::AS, lang::AS)::String
fpath, _, _ = check_input_rpath(rpath; code=true)
fpath, = check_input_rpath(rpath; code=true)
# Read the file while ignoring lines that are flagged with something like `# HIDE`
_, comsym = CODE_LANG[lang]
hide = Regex(raw"(?:^|[^\S\r\n]*?)#(\s)*?(?i)hide(all)?")
Expand Down Expand Up @@ -234,11 +234,7 @@ function resolve_lx_input_hlcode(rpath::AS, lang::AS)::String
endswith(code, "\n") && (code = chop(code, tail=1))
html = html_code(code, lang)
if LOCAL_PAGE_VARS["showall"].first
fd, fn = splitdir(fpath)
res = read(joinpath(fd, "output", splitext(fn)[1] * ".res"), String)
if !isempty(res)
html *= html_div("code_output", html_code(res))
end
html *= show_res(rpath)
end
return html
end
Expand All @@ -251,11 +247,24 @@ Internal function to read the content of a script file and highlight it using `h
also [`resolve_lx_input`](@ref).
"""
function resolve_lx_input_othercode(rpath::AS, lang::AS)::String
fpath, _, _ = check_input_rpath(rpath, code=true)
fpath, = check_input_rpath(rpath, code=true)
return html_code(read(fpath, String), lang)
end


"""
$SIGNATURES
Internal function to read a result file and show it.
"""
function show_res(rpath::AS)::String
fpath, = check_input_rpath(rpath; code=true)
fd, fn = splitdir(fpath)
res = read(joinpath(fd, "output", splitext(fn)[1] * ".res"), String)
isempty(res) && return ""
return html_div("code_output", html_code(res))
end

"""
$(SIGNATURES)
Expand Down
14 changes: 14 additions & 0 deletions src/converter/lx_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ function resolve_lx_output(lxc::LxCom; reprocess::Bool=false)::String
end


"""
$SIGNATURES
Internal function to resolve a `\\show{rpath}` (finds the result of a code block and shows it).
No reprocess for this.
"""
function resolve_lx_show(lxc::LxCom; reprocess::Bool=false)::String
rpath = strip(content(lxc.braces[1]))
show_res(rpath)
end



"""
$SIGNATURES
Expand Down Expand Up @@ -179,6 +192,7 @@ const LXCOM_SIMPLE = LittleDict{String, Function}(
"\\figalt" => resolve_lx_figalt, # include a figure (may or may not have been generated)
"\\file" => resolve_lx_file, # include a file
"\\tableinput" => resolve_lx_tableinput, # include table from a csv file
"\\show" => resolve_lx_show, # show result of a code block
)


Expand Down
1 change: 1 addition & 0 deletions src/jd_vars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ the site. See [`resolve_lxcom`](@ref).
GLOBAL_LXDEFS["\\codeoutput"] = LxDef("\\codeoutput", 1, subs("@@code_output \\output{#1}@@"))
GLOBAL_LXDEFS["\\textoutput"] = LxDef("\\textoutput", 1, EMPTY_SS)
GLOBAL_LXDEFS["\\textinput"] = LxDef("\\textinput", 1, EMPTY_SS)
GLOBAL_LXDEFS["\\show"] = LxDef("\\show", 1, EMPTY_SS)
GLOBAL_LXDEFS["\\figalt"] = LxDef("\\figalt", 2, EMPTY_SS)
GLOBAL_LXDEFS["\\fig"] = LxDef("\\fig", 1, subs("\\figalt{}{#1}"))
GLOBAL_LXDEFS["\\file"] = LxDef("\\file", 2, subs("[#1]()"))
Expand Down
17 changes: 17 additions & 0 deletions test/converter/eval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,20 @@ end
</code></pre></p>
""")
end


@testset "show" begin
h = raw"""
@def hascode = true
```julia:ex
a = 5
a *= 2
```
\show{ex}
""" |> jd2html_td
@test isapproxstr(h, """
<pre><code class="language-julia">a = 5
a *= 2</code></pre>
<div class="code_output"><pre><code>10</code></pre></div>
""")
end
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ println("🍺")
println("CONVERTER/HTML")
include("converter/html.jl")
println("🍺")

println("CONVERTER/LX")
println("CONVERTER/EVAL")
include("converter/eval.jl")
println("🍺")
println("CONVERTER/LX")
include("converter/lx_input.jl")
include("converter/lx_simple.jl")
println("🍺")
Expand Down

0 comments on commit 95d0797

Please sign in to comment.