|
| 1 | +#! /usr/bin/env ruby |
| 2 | +require 'json' |
| 3 | +require 'safe_yaml' |
| 4 | +require 'optparse' |
| 5 | +require 'sskatex' |
| 6 | + |
| 7 | +me = File.basename(__FILE__) |
| 8 | + |
| 9 | +# Useful as Hash.merge argument |
| 10 | +merge_dicts = lambda do |key, left_val, right_val| |
| 11 | + if left_val.class == Hash and right_val.class == Hash |
| 12 | + left_val.merge(right_val, &merge_dicts) |
| 13 | + else |
| 14 | + right_val |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +cmd_opts = {} |
| 19 | +cfg_opts = {} |
| 20 | +config_file = nil |
| 21 | +display_mode = nil |
| 22 | +OptionParser.new do |opts| |
| 23 | + opts.banner = <<USAGE |
| 24 | +Usage: #{me} [options] [INFILE [OUTFILE]] |
| 25 | +
|
| 26 | +Processes a TeX math fragment from INFILE (default stdin) and |
| 27 | +writes corresponding HTML+MathML to OUTFILE (default stdout). |
| 28 | +Needs katex.min.js and a Javascript engine to do that. |
| 29 | +
|
| 30 | +Options: |
| 31 | +USAGE |
| 32 | + opts.on('-C CONFIG_FILE', '--config_file=CONFIG_FILE', |
| 33 | + 'Specify path to YAML configuration file') do |cf| |
| 34 | + config_file = cf |
| 35 | + end |
| 36 | + opts.on('-D', '--[no-]display', |
| 37 | + 'format math in display style') do |dm| |
| 38 | + display_mode = dm |
| 39 | + end |
| 40 | + opts.on('-J KATEX_JS', '--katex_js=KATEX_JS', |
| 41 | + 'Specify path to katex.js') do |katex_js| |
| 42 | + cmd_opts[:katex_js] = katex_js |
| 43 | + end |
| 44 | + opts.on('-K JSON', '--katex_opts=JSON', |
| 45 | + 'Specify KaTeX options') do |katex_opts| |
| 46 | + cmd_opts[:katex_opts] = JSON.parse(katex_opts, symbolize_names: true) |
| 47 | + end |
| 48 | + opts.on('-L JS_DIR', '--js_dir=JS_DIR', |
| 49 | + 'Specify dir with JS helper files') do |js_dir| |
| 50 | + cmd_opts[:js_dir] = js_dir |
| 51 | + end |
| 52 | + opts.on('-l JS_LIB,...', '--js_libs=JS_LIB,...', Array, |
| 53 | + 'Specify JS helper files, relative to JS_DIR') do |js_libs| |
| 54 | + cmd_opts[:js_libs] = js_libs |
| 55 | + end |
| 56 | + opts.on('-R JS_RUN', '--js_run=JS_RUN', |
| 57 | + 'Specify JS engine to use (-v lists choices)') do |js_run| |
| 58 | + cmd_opts[:js_run] = js_run |
| 59 | + end |
| 60 | + opts.on('-d', '--[no-]debug', |
| 61 | + 'Log JS engine config and usage to stderr') do |d| |
| 62 | + cmd_opts[:debug] = d |
| 63 | + end |
| 64 | + opts.on('-v', '--[no-]verbose', |
| 65 | + 'Log JS engine configuration to stderr') do |v| |
| 66 | + cmd_opts[:verbose] = v |
| 67 | + end |
| 68 | + opts.on("-h", "--help", "Prints this help and exits") do |h| |
| 69 | + if h |
| 70 | + puts opts |
| 71 | + exit |
| 72 | + end |
| 73 | + end |
| 74 | +end.parse! |
| 75 | + |
| 76 | +if ARGV.size > 2 |
| 77 | + warn opts |
| 78 | + exit 2 |
| 79 | +end |
| 80 | + |
| 81 | +cfg_opts = JSON.parse(YAML.load(config_file, safe: true).to_json, |
| 82 | + symbolize_names: true) if config_file |
| 83 | +options = cfg_opts.merge(cmd_opts, &merge_dicts) |
| 84 | +loglevel = options[:debug] ? :debug : options[:verbose] ? :verbose : nil |
| 85 | +display_mode = !!display_mode |
| 86 | + |
| 87 | +conv = SsKaTeX.new(options, &SsKaTeX.warn_logger(loglevel)) |
| 88 | +tex = if ARGV.size == 0 |
| 89 | + $stdin.set_encoding Encoding::UTF_8 |
| 90 | + $stdin.read |
| 91 | +else |
| 92 | + IO.read(ARGV.shift, encoding: Encoding::UTF_8) |
| 93 | +end.chomp |
| 94 | +html = conv.call(tex, display_mode) + "\n" |
| 95 | +if ARGV.size == 0 |
| 96 | + $stdout.set_encoding Encoding::UTF_8 |
| 97 | + $stdout.write html |
| 98 | +else |
| 99 | + IO.write(ARGV.shift, html, encoding: Encoding::UTF_8) |
| 100 | +end |
0 commit comments