Remove KaTeX CSS includes if we don't need them.
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
parent
36e4feb668
commit
d258886a64
15
convert.rb
15
convert.rb
@ -25,13 +25,16 @@ class KatexRenderer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def substitute(content)
|
def substitute(content)
|
||||||
|
found_any = false
|
||||||
rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match|
|
rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match|
|
||||||
|
found_any = true
|
||||||
render(false, $~[1])
|
render(false, $~[1])
|
||||||
end
|
end
|
||||||
rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match|
|
rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match|
|
||||||
|
found_any = true
|
||||||
render(true, $~[1])
|
render(true, $~[1])
|
||||||
end
|
end
|
||||||
return rendered
|
return rendered, found_any
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -58,8 +61,16 @@ renderer = KatexRenderer.new(katex)
|
|||||||
files.each do |file|
|
files.each do |file|
|
||||||
puts "Rendering file: #{file}"
|
puts "Rendering file: #{file}"
|
||||||
document = Nokogiri::HTML.parse(File.open(file))
|
document = Nokogiri::HTML.parse(File.open(file))
|
||||||
|
found_any = false
|
||||||
document.search('//*[not(ancestor-or-self::code or ancestor-or-self::script)]/text()').each do |t|
|
document.search('//*[not(ancestor-or-self::code or ancestor-or-self::script)]/text()').each do |t|
|
||||||
t.replace(renderer.substitute(t.content))
|
rendered, found_any = renderer.substitute(t.content)
|
||||||
|
t.replace(rendered)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# If we didn't find any mathematical equations, no need to include KaTeX CSS.
|
||||||
|
unless found_any
|
||||||
|
document.css('link[href$="katex.css"], link[href$="katex.min.css"]').each(&:remove)
|
||||||
|
end
|
||||||
|
|
||||||
File.write(file, document.to_html(encoding: 'UTF-8'))
|
File.write(file, document.to_html(encoding: 'UTF-8'))
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user