diff bin/mkreport @ 89:016c657f0c31

Supercharge mkreport script
author zegervdv <zegervdv@me.com>
date Sat, 02 Aug 2014 17:44:15 +0200
parents 3bc2e4f5cc59
children
line wrap: on
line diff
--- a/bin/mkreport	Sat Aug 02 11:08:43 2014 +0200
+++ b/bin/mkreport	Sat Aug 02 17:44:15 2014 +0200
@@ -1,11 +1,46 @@
-#!/bin/sh
-# Create a new latex report from template
+#!/usr/bin/env ruby
+
+# 2014, Zeger Van de Vannet
+#
+# mkreport: Create a new latex report from template
+
+require 'optparse'
+require 'ostruct'
+require 'erb'
+
+TEMPLATE_DIR = File.expand_path(".templates", "~")
+LANGS = {
+  "nl" => 'dutch',
+  "en" => 'english'
+}
 
-if [ $1 ]; then
-  name=$1
+options = OpenStruct.new
+# defaults
+options.title = "TITLE HERE"
+options.lang = "english"
+options.class_opts = ["a4paper"]
+
+OptionParser.new do |opts|
+  opts.banner = "Usage: mkreport [options] [filename]"
+  opts.on('-t', '--title TITLE', 'Set the title') { |opt| options.title = opt }
+  opts.on('-l','--language LANG', 'Set the language' ) { |opt| options.lang = LANGS[opt] || options.lang }
+  opts.on('--toc', 'Add Table of contents') { |opt| options.toc = true }
+  opts.on('-v', '--verbose', 'Show more (debugging) information') { |opt| options.verbose = true }
+  opts.on('--titlepage', 'Set a title page') { |opt| options.class_opts << 'titlepage' }
+end.parse!
+
+if ARGV.empty?
+  options.filename = 'report.tex'
 else
-  name='report.tex'
-fi
+  options.filename = "#{ARGV.first}.tex"
+end
+
+puts options.inspect if options.verbose
 
-cp $HOME/.templates/report.tex $name
-echo "Created $name"
+template = File.read(File.expand_path('report.tex.erb', TEMPLATE_DIR))
+
+File.open(options.filename, 'w') do |report|
+  @options = options
+  report.puts ERB.new(template).result()
+  puts "Created #{options.filename}"
+end