#!/usr/bin/env ruby require 'rubygems' require 'redcarpet' require 'fileutils' require 'impress_renderer' require 'trollop' require 'tempfile' require 'launchy' def log(x) puts "\033[94m" + x + "\033[0m" end def base_dir File.dirname(__FILE__) + "/../lib/" end opts = Trollop::options do banner <<-EOS Usage: mdpress [filename] [options] where [options] are: EOS opt :automatic, "Keeps running and automatically updates the presentation to reflect changes to markdown file." opt :stylesheet, "Specify what stylesheet to use.", :default => "default" opt :list, "List all available stylesheets." opt :run, "Run presentation (automatically compiles to a tmp directory and opens in a browser window)" end if opts[:list] log "Available stylesheets:" Dir.glob(base_dir + "impress_css/*").each do |file| puts File.basename(file, ".css") end exit end def render text = File.read(FILENAME) # ugly hack to get attributes for impress.js # TODO make this pretty lines = text.split("\n") lines.drop_while { |l| l =~ /^\s*$/ } attrs = [""] new_lines = [] lines.each_with_index do |line, i| if line =~ /^=(.*)$/ && (i == 0 || lines[i-1] =~ /^(-\s*){3,}$/) line =~ /^=(.*)$/ attrs[attrs.size-1] = $~.to_a[1] next elsif line =~ /^(-\s*){3,}$/ attrs << "" end new_lines << line end text = new_lines.join("\n") # now use those attributes and render the file include Redcarpet ImpressRenderer.init_with_attrs attrs m = Markdown.new(ImpressRenderer, :autolink => true) log "rendering presentation" f = File.open(DIRNAME + "/index.html", "w+") f.write(m.render(text)) f.close end STYLESHEET = base_dir + "impress_css/#{opts[:stylesheet]}.css" raise Trollop::HelpNeeded if ARGV.empty? # show help screen unless File.exist?(STYLESHEET) puts opts[:stylesheet] + " is not a valid stylesheet. See available stylesheets with `mdpress -l`." exit end if opts[:run] file = ARGV[0] tmp = Tempfile.new("mdpress") tmp.write(File.read(file)) tmp.flush FILENAME = tmp.path DIRNAME = FILENAME + "_dir" else FILENAME = ARGV[0] DIRNAME = File.basename(FILENAME, File.extname(FILENAME)) end log "making directory" Dir.mkdir(DIRNAME) render log "copying files" FileUtils.cp_r(base_dir + "js", DIRNAME) FileUtils.cp_r(base_dir + "css", DIRNAME) FileUtils.cp(STYLESHEET, DIRNAME + "/css/style.css") def auto while true sleep 2 if FileUtils.uptodate?(FILENAME, DIRNAME + "/index.html") log "updating from #{FILENAME}" render end end end if opts[:automatic] log "waiting for updates..." auto elsif opts[:run] log "opening in browser." Launchy.open(DIRNAME + "/index.html", :application => :browser) else log "done." end