Created
February 18, 2014 12:24
-
-
Save plexus/9070011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'slippery' | |
require 'fileutils' | |
require 'tempfile' | |
require_relative 'watch_task' | |
namespace :slippery do | |
extend Slippery::ProcessorHelpers::ClassMethods | |
NAME = 'learn_to_learn' | |
desc 'watch the presentation and theme for changes and rebuild' | |
watch ["#{NAME}.md", 'stylesheets/stylesheet.css', 'images', 'javascripts'] do | |
dest = Tempfile.new("#{NAME}.html") | |
File.open("#{NAME}.html") { |src| FileUtils.copy_stream(src, dest) } | |
dest.close | |
Rake::Task['slippery:build'].execute | |
puts "="*80 | |
print `diff -u #{dest.path} #{NAME}.html` if File.exist? "#{NAME}.html" | |
end | |
task :profile do | |
require 'perftools' | |
PerfTools::CpuProfiler.start("/tmp/slippery_profile") do | |
Rake::Task['slippery:build'].execute | |
end | |
end | |
task :benchmark do | |
require 'benchmark' | |
10.times do | |
puts Benchmark.measure { | |
Rake::Task['slippery:build'].execute | |
} | |
end | |
end | |
desc 'build the presentation html' | |
task :build do | |
Dir["#{NAME}.md"].each do |infile| | |
doc = markdown_to_doc(infile) | |
File.write(infile.sub(/md$/, 'html'), doc.to_html) | |
end | |
end | |
task :toc do | |
doc = markdown_to_doc('presentation.md').select('.heading') do |heading| | |
puts "- #{heading.text.strip}" | |
end | |
end | |
def markdown_to_doc(infile) | |
include Slippery::Processors | |
doc = Slippery::Document.new(File.read(infile)) | |
doc = Slippery::Presentation.new(doc, | |
type: :reveal_js, | |
theme: 'beige', | |
controls: false, | |
backgroundTransition: 'slide', | |
history: true, | |
plugins: [:notes] | |
) | |
doc.process( | |
GraphvizDot.new('.language-dot'), | |
add_custom_assets, | |
init_highlighting, | |
set_prettyprint_classes, | |
set_heading_background_color, | |
split_subsection_slides, | |
split_bigsection_slides, | |
override_svg_font, | |
notes, | |
SelfContained | |
) | |
end | |
processor :set_prettyprint_classes, '[class~=language-ruby] code' do |code| | |
code.add_class('prettyprint ruby') | |
end | |
processor :set_heading_background_color, '.heading' do |section| | |
section.attr('data-background-color',"#4d7e65") | |
end | |
processor :split_subsection_slides, '.subsections' do |section| | |
section.add_class('fragmented').map_children.with_index do |ch, idx| | |
(ch.tag == :p && idx > 1) ? ch.add_class('fragment') : ch | |
end | |
end | |
processor :notes, '.language-notes' do |notes| | |
notes.set_tag(:aside).add_class('notes').set_children(notes.text.split("\n").flat_map{|n| [n, H[:br]]}) | |
end | |
processor :split_bigsection_slides, '.bigsections p' do |para| | |
H[:section, para].add_class(:big) | |
end | |
processor :override_svg_font, 'svg text[font-family]' do |text| | |
text.attr('font-family', 'Lato') | |
end | |
processor :add_custom_assets, 'head' do |h| | |
h <<= H[:title, "Learn to Learn"] | |
h <<= H[:link, rel: 'stylesheet', href: file_uri_relative('stylesheets/stylesheet.css')] | |
h <<= H[:link, rel: 'stylesheet', href: file_uri_relative('stylesheets/highlight.css')] | |
h <<= H[:script, src: 'http://code.jquery.com/jquery-1.10.1.min.js'] | |
h <<= H[:script, src: file_uri_relative('javascripts/impress_js_addons.js')] | |
h <<= H[:script, src: file_uri_relative('javascripts/highlight.pack.js')] | |
h <<= H[:script, 'hljs.initHighlightingOnLoad();'] | |
end | |
processor :init_highlighting, 'body' do |body| | |
body.add_child(H[:script, '$("code").each(function(i,e){hljs.highlightBlock(e);});']) | |
end | |
def file_uri_relative(path) | |
"file://#{File.expand_path(File.join('..', path), __FILE__)}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rb-inotify' | |
class WatchTask | |
def initialize(name, files, &block) | |
Rake::Task.define_task name do | |
Array(files).each do |pattern| | |
notifier.watch(pattern, :modify, &block) | |
end | |
at_exit do | |
block.call | |
notifier.run | |
end | |
end | |
end | |
def notifier | |
@notifier ||= INotify::Notifier.new | |
end | |
end | |
def watch(files, name = :watch, &block) | |
WatchTask.new(name, files, &block) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment