#!/usr/bin/env ruby require "rubygems" require 'thor' require 'date' require 'latex_document/version' 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 Md < NewDocument def fill_root super template("templates/md/document.md.tt", file_path("#{name}.md")) create_file_from_shared_template 'references.bib' end protected def create_file_from_template(file_name) template( "templates/md/#{file_name}.tt", file_path(file_name) ) end end class LatexDocument < Thor # Thanks to Adam Prescott # (http://stackoverflow.com/questions/22809972/adding-a-version-option-to-a-ruby-thor-cli) # for this. map %w[--version -v] => :__print_version desc "--version, -v", "Show version information" def __print_version puts VERSION end 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)