/
slidedown.rb
111 lines (92 loc) · 2.75 KB
/
slidedown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'rubygems'
require 'optparse'
require 'nokogiri'
require 'makers-mark'
require 'erb'
require File.join(File.dirname(__FILE__), 'slide')
$SILENT = true
class SlideDown
USAGE = "The SlideDown command line interface takes a .md (Markdown) file as its only required argument. It will convert the file to HTML in standard out. Options:
-t, --template [TEMPLATE] the .erb files in /templates directory. Default is -t default, which prints stylesheets and javascripts inline. The import template uses link and script tags. This can also accept an absolute path for templates outside the /templates directory."
attr_accessor :stylesheets, :title
attr_reader :classes
def self.run!(argv = ARGV)
args = argv.dup
if args.empty?
puts USAGE
else
source = args[0]
if args.length == 1
render(source)
else
option_parser(source).parse!(args)
end
end
end
def self.option_parser(source)
OptionParser.new do |opts|
opts.on('-h', '--help') { puts USAGE }
opts.on('-t', '--template TEMPLATE') do |template|
render(source, template)
end
end
end
def self.render(source_path, template = "default")
if source_path
slideshow = new(File.read(source_path))
puts slideshow.render(template)
end
end
# Ensures that the first slide has proper !SLIDE declaration
def initialize(raw, opts = {})
@raw = raw =~ /\A!SLIDE/ ? raw : "!SLIDE\n#{raw}"
extract_classes!
self.stylesheets = opts[:stylesheets] || local_stylesheets
self.title = opts[:title] || "Slides"
end
def slides
@slides ||= lines.map { |text| Slide.new(text, *@classes.shift) }
end
def read(path)
File.read(File.join(File.dirname(__FILE__), '..', "templates", path))
end
def render(name)
if is_absolute_path?(name)
template = File.read("#{name}.erb")
else
directory = File.join(File.dirname(__FILE__), "..", "templates")
path = File.join(directory, "#{name}.erb")
template = File.read(path)
end
ERB.new(template).result(binding)
end
private
def lines
@lines ||= @raw.split(/^!SLIDE\s*([a-z\s]*)$/).reject { |line| line.empty? }
end
def local_stylesheets
Dir[Dir.pwd + '/*.stylesheets']
end
def javascripts
Dir[Dir.pwd + '/*.javascripts'].map { |path| File.read(path) }
end
def extract_classes!
@classes = []
@raw.gsub!(/^!SLIDE\s*([a-z\s]*)$/) do |klass|
@classes << klass.to_s.chomp.gsub('!SLIDE', '')
"!SLIDE"
end
@classes
end
def extract_notes!
@raw.gsub!(/^!NOTES\s*(.*)!SLIDE$/m) do |note|
'!SLIDE'
end
@raw.gsub!(/^!NOTES\s*(.*\n)$/m) do |note|
''
end
end
def is_absolute_path?(path)
path == File.expand_path(path)
end
end