Update blog builder to use KaTeX and statically render math.
This commit is contained in:
		
							parent
							
								
									f58fadf388
								
							
						
					
					
						commit
						13cf6263b0
					
				
							
								
								
									
										33
									
								
								blog/convert.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								blog/convert.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
require "open3"
 | 
			
		||||
require "nokogiri"
 | 
			
		||||
 | 
			
		||||
def perform_katex_sub(content)
 | 
			
		||||
  rendered = content.gsub /\\\(((?:[^\\]|\\[^\)])*)\\\)/ do |match|
 | 
			
		||||
    puts "  Rendering #{$~[1]}"
 | 
			
		||||
    Open3.popen3("$(npm bin)/katex") do |i, o, e, t|
 | 
			
		||||
      i.write $~[1]
 | 
			
		||||
      i.close
 | 
			
		||||
      o.read.force_encoding(Encoding::UTF_8).strip
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  rendered = rendered.gsub /\$\$((?:[^\$]|$[^\$])*)\$\$/ do |match|
 | 
			
		||||
    puts "  Rendering display #{$~[1][0..30].strip}"
 | 
			
		||||
    Open3.popen3("$(npm bin)/katex -d") do |i, o, e, t|
 | 
			
		||||
      i.write $~[1]
 | 
			
		||||
      i.close
 | 
			
		||||
      o.read.force_encoding(Encoding::UTF_8).strip
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  return rendered
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
files = ARGV[0..-1]
 | 
			
		||||
 | 
			
		||||
files.each do |file|
 | 
			
		||||
  puts "Rendering file: #{file}"
 | 
			
		||||
  document = Nokogiri::HTML.parse(File.open(file))
 | 
			
		||||
  document.search('//text()').each do |t|
 | 
			
		||||
    t.replace(perform_katex_sub(t.content))
 | 
			
		||||
  end
 | 
			
		||||
  File.write(file, document.to_html)
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user