Sha256: 04f2eb91f293fac21aaeacfbe4e4e4e5764ffddc44bfc36f72fff829370f6af6

Contents?: true

Size: 1.91 KB

Versions: 25

Compression:

Stored size: 1.91 KB

Contents

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

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

examples = %(
The polytexnic script converts Markdown to HTML or LaTeX
using the PolyTeXnic HTML pipeline.

Examples:
    polytexnic example.md example.html
    polytexnic example.md > example.html
    polytexnic < example.md > example.html
    cat example.md | polytexnic > example.html
    polytexnic -i example.md > example.html
    polytexnic -i example.md -o example.tex
    polytexnic -f tex example.md)

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

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

  opts.on("-o", "--output=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

# Defines a valid HTML template.
def html_template(title, body)
%(<!DOCTYPE html>
<html>
  <head>
    <title>#{title}</title>
    <meta charset="utf-8">
  </head>
  <body>
    #{body}
  </body>
</html>
)
end

if (infile = options[:infile] || ARGV.shift)
puts infile
  input = File.read(infile)
else
  input = STDIN.read
end
outfile  = options[:outfile] || ARGV.shift
puts outfile
pipeline = Polytexnic::Pipeline.new(input, article: true)
format   = options[:format] || format(outfile) || "html"
if format == "html"
  output = html_template("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

25 entries across 25 versions & 1 rubygems

Version Path
polytexnic-1.10.8 bin/polytexnic
polytexnic-1.10.7 bin/polytexnic
polytexnic-1.10.6 bin/polytexnic
polytexnic-1.10.5 bin/polytexnic
polytexnic-1.10.4 bin/polytexnic
polytexnic-1.10.3 bin/polytexnic
polytexnic-1.10.2 bin/polytexnic
polytexnic-1.10.1 bin/polytexnic
polytexnic-1.10.0 bin/polytexnic
polytexnic-1.9.2 bin/polytexnic
polytexnic-1.9.1 bin/polytexnic
polytexnic-1.9.0 bin/polytexnic
polytexnic-1.8.2 bin/polytexnic
polytexnic-1.8.1 bin/polytexnic
polytexnic-1.8.0 bin/polytexnic
polytexnic-1.7.5 bin/polytexnic
polytexnic-1.7.4 bin/polytexnic
polytexnic-1.7.3 bin/polytexnic
polytexnic-1.7.2 bin/polytexnic
polytexnic-1.7.1 bin/polytexnic