2023-12-24 22:44:07 +01:00
|
|
|
# Made by me :)
|
|
|
|
# Feel free to use if you give credit
|
|
|
|
|
|
|
|
require "base64"
|
|
|
|
|
|
|
|
module Jekyll
|
|
|
|
module Tags
|
|
|
|
class CDTag < Liquid::Block
|
|
|
|
|
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
super
|
|
|
|
arr = markup.split("s:")
|
|
|
|
if arr[1] != nil
|
|
|
|
@scale = arr[1].to_i
|
|
|
|
else
|
|
|
|
@scale = 60
|
|
|
|
end
|
|
|
|
@caption = arr[0].strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
|
|
|
site = context.registers[:site]
|
|
|
|
|
|
|
|
converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
|
|
|
|
caption = converter.convert(@caption).gsub(/<\/?p[^>]*>/, '').chomp
|
|
|
|
|
|
|
|
code = super(context)
|
|
|
|
|
|
|
|
latex = <<EOF
|
|
|
|
\\documentclass[tikz]{standalone}
|
|
|
|
\\usepackage{tikz-cd}
|
2023-12-24 22:57:54 +01:00
|
|
|
\\usepackage{amssymb}
|
|
|
|
\\usepackage{amsmath}
|
2023-12-24 22:44:07 +01:00
|
|
|
\\begin{document}
|
|
|
|
\\begin{tikzcd}#{code}\\end{tikzcd}
|
|
|
|
\\end{document}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
File.write("#{dir}/cd.tex", latex)
|
|
|
|
raise "pdflatex" unless system("pdflatex -output-directory #{dir} #{dir}/cd.tex")
|
|
|
|
raise "inkscape" unless system("inkscape -l --export-filename=#{dir}/cd.svg #{dir}/cd.pdf")
|
|
|
|
raise "scour" unless system("scour -i #{dir}/cd.svg -o #{dir}/optimized_cd.svg --enable-viewboxing --enable-id-stripping --enable-comment-stripping --shorten-ids --indent=none")
|
|
|
|
svg = File.read("#{dir}/optimized_cd.svg")
|
|
|
|
return <<EOF
|
|
|
|
<figure class="cdfig">
|
2023-12-24 22:44:22 +01:00
|
|
|
<img class="cd" style="width: #{@scale}%;" src="data:image/svg+xml;base64,#{Base64.encode64(svg)}" alt="A commutative diagram"/>
|
2023-12-24 22:44:07 +01:00
|
|
|
<figcaption>#{@caption}</figcaption>
|
|
|
|
</figure>
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
"<details><summary>#{caption}</summary>#{body}</details>"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('cd', Jekyll::Tags::CDTag)
|