#!/usr/bin/env ruby require "rubygems" require 'thor' require 'date' module LatexDocument class NewDocument < Thor::Group include Thor::Actions DEFAULT_AUTHOR = 'TODO: Author' argument :name, :desc => 'Name of the new document.' class_option :author, :desc => 'Author of the new document.', :default => DEFAULT_AUTHOR def self.source_root File.expand_path('../../', __FILE__) end def gen_root empty_directory name end def fill_root create_file_from_template "Makefile" end protected def title title_string = name.dup title_string[0] = name[0].upcase title_string.gsub(/(\w)([A-Z])/) { "#{$1} #{$2}" } # Camel case title_string.gsub(/[_\.\s](.)/) { " #{$1.upcase}" } # Snake case end def author options[:author] end def date Date.today.strftime('%B %-d, %Y') end def file_path(file_name) File.join name, file_name end def create_file_from_shared_template(file_name) template( "templates/#{file_name}.tt", file_path(file_name) ) end end class Tex < NewDocument def fill_root super empty_directory file_path('src') empty_directory file_path('include') end def src template("templates/tex/src/document.tex.tt", file_path("src/#{name}.tex")) template("templates/references.bib.tt", file_path("src/references.bib")) end def include create_file_from_template("include/mydefault.sty") end protected def create_file_from_template(file_name) template( "templates/tex/#{file_name}.tt", file_path(file_name) ) end end class Md < NewDocument def fill_root super template("templates/md/document.tex.md.tt", file_path("#{name}.tex.md")) create_file_from_shared_template 'references.bib' create_file_from_template 'template.pandoc' end protected def create_file_from_template(file_name) template( "templates/md/#{file_name}.tt", file_path(file_name) ) end end class LatexDocument < Thor map 't' => :tex register( Tex, 'tex', "tex NAME [author=AUTHOR]", "Creates a new latex document with name NAME and author AUTHOR, the latter of which defaults to \"#{Tex::DEFAULT_AUTHOR}\"." ) tasks["tex"].options = Tex.class_options map 'm' => :md register( Md, 'md', "md NAME [author=AUTHOR]", "Creates a new markdown/latex hybrid document with name NAME and author AUTHOR, the latter of which defaults to \"#{Md::DEFAULT_AUTHOR}]\"." ) tasks["md"].options = Md.class_options end end LatexDocument::LatexDocument.start(ARGV)