Sha256: 22b415162c0ed5405b3ec211e6ce165c8fcba2e81e68efed4f33f259e60f857a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

#!/usr/bin/env ruby
require "polytexnic"
require 'optparse'
require 'fileutils'

# polytexnic command-line script
# The polytexnic script converts Markdown to LaTeX
# using the PolyTeXnic HTML pipeline.

examples = %(Examples:
    polytexnic example.md example.tex
    polytexnic example.md > example.tex
    polytexnic < example.md > example.tex
    cat example.md | polytexnic > example.tex
    polytexnic -f example.md > example.tex
    polytexnic -f example.md -o example.tex)

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: polytexnic [options]\n#{examples}\n\n"

  opts.on("-i", "--input", "Use file input") do |infile|
    options[:infile] = infile
  end

  opts.on("-o", "--output", "Use file output") do |outfile|
    options[:outfile] = outfile
  end

  opts.on("-f", "--format", "Use output format [html, tex]") do |format|
    options[:format] = format
  end
end.parse!

# Returns the file format based on extension.
# Should be 'html' or 'tex'.
def format(filename)
  filename.split('.').last
rescue
  nil
end

if (infile = options[:infile] || ARGV.shift)
  input = File.read(infile)
else
  input = STDIN.read
end
outfile  = options[:outfile] || ARGV.shift
pipeline = Polytexnic::Pipeline.new(input, article: true)
format   = options[:format] || format(outfile) || "html"
if format == "html"
  output = pipeline.to_html
elsif format == "tex"
  output = pipeline.to_polytex
else
  raise ArgumentError, "Invalid format: #{format}"
end
if outfile
  File.write(outfile, output)
else
  puts output.strip
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polytexnic-1.6.0 bin/polytexnic