Class: ShowOffUtils
- Inherits:
-
Object
- Object
- ShowOffUtils
- Defined in:
- lib/showoff_utils.rb
Constant Summary collapse
- SHOWOFF_JSON_FILE =
'showoff.json'
- TYPES =
{ :default => lambda { |t,size,source,type| (t,"#{size} #{type}",source) }, 'title' => lambda { |t,size,dontcare| (t,size) }, 'bullets' => lambda { |t,size,dontcare| (t,"#{size} bullets incremental",["bullets","go","here"])}, 'smbullets' => lambda { |t,size,dontcare| (t,"#{size} smbullets incremental",["bullets","go","here","and","here"])}, 'code' => lambda { |t,size,src| (t,size,blank?(src) ? " @@@ Ruby\n code_here()" : src) }, 'commandline' => lambda { |t,size,dontcare| (t,"#{size} commandline"," $ command here\n output here")}, 'full-page' => lambda { |t,size,dontcare| (t,"#{size} full-page","![Image Description](image/ref.png)")}, }
- EXTENSIONS =
{ 'pl' => 'perl', 'rb' => 'ruby', 'erl' => 'erlang', # so not exhaustive, but probably good enough for now }
Class Method Summary collapse
-
.add_new_dir(dir) ⇒ Object
Adds the given directory to this presentation, appending it to the end of showoff.json as well.
-
.add_slide(options) ⇒ Object
Adds a new slide to a given dir, giving it a number such that it falls after all slides in that dir.
-
.adjust_size(lines, width) ⇒ Object
Determines a more optimal value for the size (e.g. small vs. smaller) based upon the size of the code being formatted.
- .blank?(string) ⇒ Boolean
- .create(dirname, create_samples, dir = 'one') ⇒ Object
- .determine_filename(slide_dir, slide_name, number) ⇒ Object
- .determine_size_and_source(code) ⇒ Object
- .determine_title(title, slide_name, code) ⇒ Object
-
.find_next_number(slide_dir) ⇒ Object
Finds the next number in the given dir to name a slide as the last slide in the dir.
- .heroku(name) ⇒ Object
- .lang(source_file) ⇒ Object
-
.make_slide(title, classes = "", content = nil) ⇒ Object
Makes a slide as a string.
-
.read_code(source_file) ⇒ Object
Reads the code from the source file, returning the code, indented for markdown, as well as the number of lines and the width of the largest line.
- .showoff_sections(dir = '.') ⇒ Object
- .showoff_title(dir = '.') ⇒ Object
- .write_file(filename, slide) ⇒ Object
Class Method Details
.add_new_dir(dir) ⇒ Object
Adds the given directory to this presentation, appending it to the end of showoff.json as well
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/showoff_utils.rb', line 126 def self.add_new_dir(dir) puts "Creating #{dir}..." Dir.mkdir dir showoff_json = JSON.parse(File.read(SHOWOFF_JSON_FILE)) showoff_json << { "section" => dir } File.open(SHOWOFF_JSON_FILE,'w') do |file| file.puts JSON.generate(showoff_json) end puts "#{SHOWOFF_JSON_FILE} updated" end |
.add_slide(options) ⇒ Object
Adds a new slide to a given dir, giving it a number such that it falls after all slides in that dir. Options are:
- :dir
-
dir where we put the slide (if omitted, slide is output to $stdout)
-
- :name
-
name of the file, without the number prefix. (if omitted, a default is used)
-
- :title
-
title in the slide. If not specified the source file name is used. If THAT is not specified, uses the value of
:name
. If THAT is not specified, a suitable default is used
-
- :code
-
path to a source file to use as content (force :type to be ‘code’)
-
- :number
-
true if numbering should be done, false if not
-
- :type
-
the type of slide to create
-
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/showoff_utils.rb', line 100 def self.() add_new_dir([:dir]) if [:dir] && !File.exists?([:dir]) [:type] = 'code' if [:code] title = determine_title([:title],[:name],[:code]) [:name] = 'new_slide' if ![:name] size,source = determine_size_and_source([:code]) type = [:type] || :default = TYPES[type].call(title,size,source) if [:dir] filename = determine_filename([:dir],[:name],[:number]) write_file(filename,) else puts puts end end |
.adjust_size(lines, width) ⇒ Object
Determines a more optimal value for the size (e.g. small vs. smaller) based upon the size of the code being formatted.
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/showoff_utils.rb', line 194 def self.adjust_size(lines,width) size = "" # These values determined empircally size = "small" if width > 50 size = "small" if lines > 15 size = "smaller" if width > 57 size = "smaller" if lines > 19 puts "warning, some lines are too long and the code may be cut off" if width > 65 puts "warning, your code is too long and the code may be cut off" if lines > 23 size end |
.blank?(string) ⇒ Boolean
138 139 140 |
# File 'lib/showoff_utils.rb', line 138 def self.blank?(string) string.nil? || string.strip.length == 0 end |
.create(dirname, create_samples, dir = 'one') ⇒ Object
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 |
# File 'lib/showoff_utils.rb', line 4 def self.create(dirname,create_samples,dir='one') Dir.mkdir(dirname) if !File.exists?(dirname) Dir.chdir(dirname) do if create_samples # create section Dir.mkdir(dir) # create markdown file File.open("#{dir}/01_slide.md", 'w+') do |f| f.puts ("My Presentation") f.puts ("Bullet Points","bullets incremental",["first point","second point","third point"]) end end # create showoff.json File.open(SHOWOFF_JSON_FILE, 'w+') do |f| f.puts "{ \"name\": \"My Preso\", \"sections\": [ {\"section\":\"#{dir}\"} ]}" end if create_samples puts "done. run 'showoff serve' in #{dirname}/ dir to see slideshow" else puts "done. add slides, modify #{SHOWOFF_JSON_FILE} and then run 'showoff serve' in #{dirname}/ dir to see slideshow" end end end |
.determine_filename(slide_dir, slide_name, number) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/showoff_utils.rb', line 159 def self.determine_filename(,,number) filename = "#{}/#{}.md" if number max = find_next_number() filename = "#{}/#{max}_#{}.md" end filename end |
.determine_size_and_source(code) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/showoff_utils.rb', line 142 def self.determine_size_and_source(code) size = "" source = "" if code source,lines,width = read_code(code) size = adjust_size(lines,width) end [size,source] end |
.determine_title(title, slide_name, code) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/showoff_utils.rb', line 183 def self.determine_title(title,,code) if blank?(title) title = title = File.basename(code) if code end title = "Title here" if blank?(title) title end |
.find_next_number(slide_dir) ⇒ Object
Finds the next number in the given dir to name a slide as the last slide in the dir.
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/showoff_utils.rb', line 170 def self.find_next_number() max = 0 Dir.open().each do |file| if file =~ /(\d+).*\.md/ num = $1.to_i max = num if num > max end end max += 1 max = "0#{max}" if max < 10 max end |
.heroku(name) ⇒ Object
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 |
# File 'lib/showoff_utils.rb', line 31 def self.heroku(name) if !File.exists?(SHOWOFF_JSON_FILE) puts "fail. not a showoff directory" return false end # create .gems file File.open('.gems', 'w+') do |f| f.puts "bluecloth" f.puts "nokogiri" f.puts "showoff" f.puts "gli" end if !File.exists?('.gems') # create config.ru file File.open('config.ru', 'w+') do |f| f.puts 'require "showoff"' f.puts 'run ShowOff.new' end if !File.exists?('config.ru') puts "herokuized. run something like this to launch your heroku presentation: heroku create #{name} git add .gems config.ru git commit -m 'herokuized' git push heroku master " end |
.lang(source_file) ⇒ Object
255 256 257 258 |
# File 'lib/showoff_utils.rb', line 255 def self.lang(source_file) ext = File.extname(source_file).gsub(/^\./,'') EXTENSIONS[ext] || ext end |
.make_slide(title, classes = "", content = nil) ⇒ Object
Makes a slide as a string.
- title
-
title of the slide
- classes
-
any “classes” to include, such as ‘smaller’, ‘transition’, etc.
- content
-
slide content. Currently, if this is an array, it will make a bullet list. Otherwise the string value of this will be put in the slide as-is
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/showoff_utils.rb', line 64 def self.(title,classes="",content=nil) = "!SLIDE #{classes}\n" << "# #{title} #\n" << "\n" if content if content.kind_of? Array content.each { |x| << "* #{x.to_s}\n" } else << content.to_s end end end |
.read_code(source_file) ⇒ Object
Reads the code from the source file, returning the code, indented for markdown, as well as the number of lines and the width of the largest line
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/showoff_utils.rb', line 209 def self.read_code(source_file) code = " @@@ #{lang(source_file)}\n" lines = 0 width = 0 File.open(source_file) do |code_file| code_file.readlines.each do |line| code += " #{line}" lines += 1 width = line.length if line.length > width end end [code,lines,width] end |
.showoff_sections(dir = '.') ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/showoff_utils.rb', line 223 def self.showoff_sections(dir = '.') index = File.join(dir, ShowOffUtils::SHOWOFF_JSON_FILE ) order = nil if File.exists?(index) data = JSON.parse(File.read(index)) pp data if data.is_a?(Hash) order = data['sections'] else order = data end order = order.map { |s| s['section'] } end order end |
.showoff_title(dir = '.') ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/showoff_utils.rb', line 239 def self.showoff_title(dir = '.') index = File.join(dir, ShowOffUtils::SHOWOFF_JSON_FILE ) order = nil if File.exists?(index) data = JSON.parse(File.read(index)) data.is_a?(Hash) && data['name'] || "Presentation" end end |
.write_file(filename, slide) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/showoff_utils.rb', line 152 def self.write_file(filename,) File.open(filename,'w') do |file| file.puts end puts "Wrote #{filename}" end |