Franklin.jl: when Julia scripting with HTML, how can I get its insertions to work?

I have this in my utils.jl file:

function resolve_inserts(html::String)::String
    insert_pattern = r"\{\{\s*insert\s+([a-zA-Z0-9_./\-]+)\s*\}\}"

    return replace(html, insert_pattern => s -> begin
        filename = match(insert_pattern, s).captures[1]
        filepath = joinpath(@__DIR__, "_layout", filename)
        if isfile(filepath)
            included = read(filepath, String)
            resolve_inserts(included)  # recursive resolution
        else
            "<!-- Could not find: $filename -->"
        end
    end)
end

function hfun_render_footlist()
    index = locvar("index")
    if @isdefined(index)
        return """"""
    else
      mainfile = joinpath(@__DIR__, "_layout", "pages_list.html")
      content = read(mainfile, String)
      expanded = resolve_inserts(content)

      return """
      <button type='button' class='collapsible'>Show content pages list</button>
      <div class='content'><p>
      $expanded
      </p></div>
      """
    end
end

This works to an extent as it does insert the right HTML when the @isdefined condition is activated. But @isdefined is not working as I had hoped. I wanted this to return an empty string if index is defined and return the contents of pages_list.html (and files referenced in it) otherwise. But instead it seems to returning an empty string regardless of whether index is defined within a page. How do I get the @isdefined(index) condition to work? If you know a way to get it to activate only when the file being rendered is index.md without even needing index defined, that’ll be fine, too!

Furthermore, maths blocks are not rendered in pages_list.html which is surprising. Even in index.md which has:

~~~
{{ insert pages_list.html
~~~

in it. By maths blocks I mean \(...maths...\) like \(\displaystyle \int_{-\infty}^{\infty} \dfrac{x^2\cos{mx}}{(x^4+a^4)^2} dx\). Is there a way to fix this?

I have answered my first query, namely how I get hfun_render_footlist to return an empty string if the document I have opened is index.md. I found this code:

function resolve_inserts(html::String)::String
    insert_pattern = r"\{\{\s*insert\s+([a-zA-Z0-9_./\-]+)\s*\}\}"

    return replace(html, insert_pattern => s -> begin
        filename = match(insert_pattern, s).captures[1]
        filepath = joinpath(@__DIR__, "_layout", filename)
        if isfile(filepath)
            included = read(filepath, String)
            resolve_inserts(included)  # recursive resolution
        else
            "<!-- Could not find: $filename -->"
        end
    end)
end

function hfun_render_footlist()
    rpath = locvar("fd_rpath");
    if (rpath == "index.md")
        return """"""
    else
      mainfile = joinpath(@__DIR__, "_layout", "pages_list.html")
      content = read(mainfile, String)
      expanded = resolve_inserts(content)

      return """
      <button type='button' class='collapsible'>Show content pages list</button>
      <div class='content'><p>
      $expanded
      </p></div>
      """
    end
end`

does the trick. I still cannot get the maths blocks in index.md rendered. Even though it is rendered in the footnotes (where hfun_render_footlist() is run).

Never mind. I just needed to set hasmath to true in my index.md file.

1 Like