updated to use golang chroma instead of python pygments

This commit is contained in:
Adam Piontek 2021-04-06 10:42:15 -04:00
parent 3bdfb5148b
commit a8e0b5d45d
9 changed files with 85 additions and 296 deletions

View file

@ -7,10 +7,14 @@ defmodule Home73k do
if it comes from the database, an external API or others.
"""
@app_vars Application.compile_env(:home73k, :app_global_vars, time_zone: "America/New_York")
@app_vars Application.compile_env(:home73k, :app_global_vars, [
time_zone: "America/New_York",
blog_content: "priv/content",
chroma_bin: "priv/chroma/chroma"
])
def app_vars, do: @app_vars
def app_time_zone, do: @app_vars[:time_zone]
def app_blog_content, do: @app_vars[:blog_content]
def app_pygmentize_bin, do: @app_vars[:pygmentize_bin]
def app_chroma_bin, do: @app_vars[:chroma_bin]
end

View file

@ -5,7 +5,8 @@ defmodule Home73k.Highlighter do
alias Home73k.Temp
@pygments_cmd Home73k.app_pygmentize_bin() |> Path.expand()
@chroma_bin Home73k.app_chroma_bin() |> Path.expand()
@style "solarized-dark256"
@doc """
Highlights all code block in an already generated HTML document.
@ -23,15 +24,12 @@ defmodule Home73k.Highlighter do
tmp_file = Temp.file()
File.write!(tmp_file, unescaped_code)
# pygmentize the code via temp file
pyg_args = ["-l", lang, "-f", "html", "-O", "cssclass=pygments", tmp_file]
{highlighted, _} = System.cmd(@pygments_cmd, pyg_args)
# use chroma to highlight the code via temp file
bin_args = ["-l", lang, "-f", "html", "-s", @style, "--html-only", "--html-prevent-surrounding-pre", tmp_file]
{highlighted, _} = System.cmd(@chroma_bin, bin_args)
# correct pygment wrapping markup
highlighted
|> String.replace("<span></span>", "")
|> String.replace("<div class=\"pygments\"><pre>", "<pre class=\"pygments\"><code class=\"language-#{lang}\">")
|> String.replace("</pre></div>", "</code></pre>")
# return properly wrapped highlighted code
~s(<pre class="chroma"><code class="language-#{lang}">#{highlighted}</code></pre>)
end
entities = [{"&amp;", ?&}, {"&lt;", ?<}, {"&gt;", ?>}, {"&quot;", ?"}, {"&#39;", ?'}]