Skip to content

Instantly share code, notes, and snippets.

@knomedia
Last active August 29, 2015 14:20
Show Gist options
  • Save knomedia/6ec432afb41c4df67b1d to your computer and use it in GitHub Desktop.
Save knomedia/6ec432afb41c4df67b1d to your computer and use it in GitHub Desktop.
build files from notes for cli-present
#!/usr/bin/env ruby
# encoding: utf-8
=begin
quick little script to generate individual files from a single file such that
they can be used within https://github.com/cmatheson/cli-present
syntax notes:
* lines starting with `##` are formated as headings
* lines starting with `sh` are created as shell scripts
* `\\` is broken into multiple lines within the same outputted file
./build.rb file_with_notes output_dir
replace `output_dir` with `-p` to print to stdout
=end
BUFFER = 12
def heading? line
line.match /##/
end
def breaks? line
line.match /\\/
end
def shell? line
line.match /^sh\s/
end
def shellit line
line = line[3..-1]
"#!/usr/bin/env bash\n#{line}"
end
def breakem line
parts = line.split /\\/
parts.each &:strip!
parts.join("\n")
end
def buffer length
b = "|"
length.times {|x| b += ' '}
b += "|\n"
b
end
def center line
pad = (BUFFER / 2)
b = ''
pad.times {|x| b += ' '}
"|#{b}#{line}#{b}|\n"
end
def heading_box line
line = line.strip
line = line[3..-1]
l = line.length + BUFFER
new_line = "+"
l.times {|x| new_line += '-'}
new_line += "+\n"
b = buffer(l)
output = "#{new_line}#{b}#{center(line)}#{b}#{new_line}"
end
def build_name(count, shell)
ext = shell ? '.sh' : ''
fname = "%04d_slide%s" % [count, ext]
"#{SLIDES_DIR}/#{fname}"
end
NOTES_PATH = ARGV[0] || 'notes.txt'
SLIDES_DIR = ARGV[1] || 'slides'
write = true
if SLIDES_DIR == '-p'
write = false
end
if write
`rm -rf #{SLIDES_DIR}`
`mkdir #{SLIDES_DIR}`
end
count = 0
lines = File.readlines(NOTES_PATH).each do |line|
count += 1
shell = false
line = line.strip
if line.size > 0
if (heading? line)
line = heading_box line
end
if (breaks? line)
line = breakem line
end
if shell? line
shell = true
line = shellit line
end
if write
name = build_name count, shell
File.open(name, 'w') {|f| f.write line }
else
puts "#{line}\n\n"
end
end
end
if write
Dir["#{SLIDES_DIR}/**/*.sh"].each do |f|
`chmod a+x #{f}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment