Skip to content

Loading…

Split ALL the h1s #133

Open
wants to merge 2 commits into from

2 participants

@alexch
Collaborator

Testing and refactoring the first part of the "process_markdown" method. I stuck in an implementation of my pet "Split ALL the H1s" feature but I can take it out if you just want the tests for the moment. Or just ignore this pull request until we figure out (and I implement) how to configure it from the top level, or whether we want the feature on all the time (in which case I'd un-break the example preso and stuff).

ALL

@alexch
Collaborator

ping... this is pretty much working, might could use some more tests but I'm pretty ok with it as is

@alexch
Collaborator

Just fixed some residual weirdness -- i think i hadn't completely renamed --split_all to simply --split.

Note that because this hasn't been merged yet I'm making my own gem called showoff-alexch so my students and disciples-- uh, I mean colleagues -- can "gem install showoff-alexch" if they want to run my slides with splitting active. I'll try to keep this evil twin gem rebased and up to date with the main one.

@alexch
Collaborator

I did some major rebasing (and squashing) but somehow this got tied in with my other PR (which was 134 but is now #154 )

Anyway, just look at alexch@5f78b23 which includes just the simple --split option and unit tests.

@goncalossilva
Collaborator

I like this. However, I believe it would make more sense for it to be an option specified in showoff.json, like this: #152

Are you willing to make the change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 23, 2011
  1. @alexch
  2. @alexch

    readme improvements

    alexch committed
Showing with 178 additions and 23 deletions.
  1. +3 −0 README.rdoc
  2. +11 −2 bin/showoff
  3. +35 −21 lib/showoff.rb
  4. +129 −0 test/split_test.rb
View
3 README.rdoc
@@ -427,6 +427,7 @@ These options are specified *after* the command.
[<tt>-f, --pres_file=arg</tt>] Presentation file <i>(default: <tt>showoff.json</tt>)</i>
[<tt>-h, --host=arg</tt>] Host or ip to run on <i>( default: <tt>localhost</tt>)</i>
[<tt>-p, --port=arg</tt>] Port on which to run <i>( default: <tt>9090</tt>)</i>
+[<tt>-s, --split</tt>] Split slides on every H1 <i>( default: <tt>false</tt>)</i>
== <tt>showoff static name</tt>
@@ -510,6 +511,8 @@ So far, ShowOff has been used in the following presentations (and many others):
http://github.com/alexch/workshop
* SDRuby Lightning Talk - Readable Regexps - Ian Young
https://github.com/iangreenleaf/sdruby-lightningtalk-tregexp
+* "Ruby Notes" tutorial slides - Alex Chaffee
+ https://github.com/alexch/ruby_notes
If you use it for something, please let me know so I can add it.
View
13 bin/showoff
@@ -100,12 +100,16 @@ command :serve do |c|
c.desc 'Host or ip to run on'
c.default_value "localhost"
- c.flag [:h,:host]
+ c.flag [:h,:host]
c.desc 'JSON file used to describe presentation'
c.default_value "showoff.json"
c.flag [:f, :pres_file]
+ c.desc 'every H1 (lone #) in the markdown file will become a new slide'
+ c.default_value false
+ c.switch [:s, :split]
+
c.action do |global_options,options,args|
url = "http://#{options[:h]}:#{options[:p].to_i}"
@@ -121,7 +125,12 @@ To run it from presenter view, go to: [ #{url}/presenter ]
-------------------------
"
- ShowOff.run! :host => options[:h], :port => options[:p].to_i, :pres_file => options[:f], :pres_dir => args[0], :verbose => options[:verbose]
+ ShowOff.run! :host => options[:h],
+ :port => options[:p].to_i,
+ :pres_file => options[:f],
+ :pres_dir => args[0],
+ :verbose => options[:verbose],
+ :split => options[:split]
end
end
View
56 lib/showoff.rb
@@ -33,6 +33,7 @@ class ShowOff < Sinatra::Application
set :verbose, false
set :pres_dir, '.'
set :pres_file, 'showoff.json'
+ set :split, false
def initialize(app=nil)
super(app)
@@ -89,13 +90,44 @@ def js_files
Dir.glob("#{settings.pres_dir}/*.js").map { |path| File.basename(path) }
end
-
def preshow_files
Dir.glob("#{settings.pres_dir}/_preshow/*").map { |path| File.basename(path) }.to_json
end
- # todo: move more behavior into this class
class Slide
+
+ # given a chunk of Markdown text, splits it into an array of Slide objects
+ def self.split content, options = {}
+ split_all_the_h1s = options[:split_all_the_h1s]
+ unless content =~ /^\<?!SLIDE/m
+ content = content.gsub(/^# /m, "<!SLIDE>\n# ")
+ end
+
+ lines = content.split("\n")
+ slides = []
+ slides << (slide = Slide.new)
+ until lines.empty?
+ line = lines.shift
+ if line =~ /^<?!SLIDE(.*)>?/
+ slides << (slide = Slide.new($1))
+
+ elsif split_all_the_h1s and line =~ /^# / and !slide.empty?
+ # every H1 defines a new slide, unless there's a !SLIDE before it
+ slides << (slide = Slide.new())
+ slide << line
+
+ else
+ slide << line
+ end
+ end
+
+ slides.delete_if {|slide| slide.empty? }
+
+ slides
+ end
+
+ ####
+
attr_reader :classes, :text
def initialize classes = ""
@classes = ["content"] + classes.strip.chomp('>').split
@@ -110,29 +142,11 @@ def empty?
end
end
-
def process_markdown(name, content, static=false, pdf=false)
-
- # if there are no !SLIDE markers, then make every H1 define a new slide
- unless content =~ /^\<?!SLIDE/m
- content = content.gsub(/^# /m, "<!SLIDE>\n# ")
- end
-
- # todo: unit test
lines = content.split("\n")
@logger.debug "#{name}: #{lines.length} lines"
- slides = []
- slides << (slide = Slide.new)
- until lines.empty?
- line = lines.shift
- if line =~ /^<?!SLIDE(.*)>?/
- slides << (slide = Slide.new($1))
- else
- slide << line
- end
- end
- slides.delete_if {|slide| slide.empty? }
+ slides = Slide.split content, :split_all_the_h1s => settings.split
final = ''
if slides.size > 1
View
129 test/split_test.rb
@@ -0,0 +1,129 @@
+require File.expand_path "../test_helper", __FILE__
+
+context "splitting Markdown into slides" do
+
+ def app
+ opt = {:verbose => false, :pres_dir => "test/fixtures/simple", :pres_file => 'showoff.json'}
+ ShowOff.set opt
+ @showoff = ShowOff.new
+ end
+
+ setup do
+ end
+
+ test "from an empty string" do
+ slides = ShowOff::Slide.split ""
+ assert_empty slides
+ end
+
+ test "from a simple chunk of Markdown" do
+ slides = ShowOff::Slide.split <<-MARKDOWN
+# One
+* uno
+ MARKDOWN
+ assert_equal 1, slides.size
+ assert_equal ["content"], slides.first.classes
+ assert_equal "# One\n* uno\n", slides.first.text
+ end
+
+ test "from a simple chunk of Markdown with slide markers" do
+ slides = ShowOff::Slide.split <<-MARKDOWN
+!SLIDE cool
+# One
+* uno
+
+!SLIDE neat
+# Two
+* dos
+ MARKDOWN
+ assert_equal 2, slides.size
+ slide = slides.first
+ assert_equal ["content", "cool"], slide.classes
+ assert_equal "# One\n* uno\n\n", slides.first.text
+ slide = slides[1]
+ assert_equal ["content", "neat"], slide.classes
+ assert_equal "# Two\n* dos\n", slide.text
+ end
+
+ test "from a simple chunk of Markdown with comment-style slide markers" do
+ slides = ShowOff::Slide.split <<-MARKDOWN
+<!SLIDE cool>
+# One
+* uno
+
+<!SLIDE neat>
+# Two
+* dos
+ MARKDOWN
+ assert_equal 2, slides.size
+ slide = slides.first
+ assert_equal ["content", "cool"], slide.classes
+ assert_equal "# One\n* uno\n\n", slides.first.text
+ slide = slides[1]
+ assert_equal ["content", "neat"], slide.classes
+ assert_equal "# Two\n* dos\n", slide.text
+ end
+
+
+ test "omits empty slides" do
+ slides = ShowOff::Slide.split <<-MARKDOWN
+!SLIDE
+# One
+* uno
+
+!SLIDE
+
+!SLIDE
+# Two
+* dos
+ MARKDOWN
+ assert_equal 2, slides.size
+ end
+
+ test "if there are no !SLIDE markers at all, then every H1 defines a new slide" do
+ slides = ShowOff::Slide.split <<-MARKDOWN
+# One
+* uno
+
+# Two
+* dos
+ MARKDOWN
+ assert_equal 2, slides.size
+ slide = slides.first
+ assert_equal ["content"], slide.classes
+ assert_equal "# One\n* uno\n\n", slides.first.text
+ slide = slides[1]
+ assert_equal ["content"], slide.classes
+ assert_equal "# Two\n* dos\n", slide.text
+ end
+
+ test "every H1 defines a new slide" do
+ # Until we figure out how to specify this setting at the app level,
+ # this option will only be set from inside this test. This test
+ # will probably become simpler then.
+ varied = <<-MARKDOWN
+<!SLIDE>
+# One
+* uno
+
+# Two
+* dos
+ MARKDOWN
+
+ slides = ShowOff::Slide.split varied
+ assert_equal 1, slides.size
+
+ slides = ShowOff::Slide.split varied, :split_all_the_h1s => false
+ assert_equal 1, slides.size
+
+ slides = ShowOff::Slide.split varied, :split_all_the_h1s => true
+ assert_equal 2, slides.size
+ slide = slides.first
+ assert_equal ["content"], slide.classes
+ assert_equal "# One\n* uno\n\n", slides.first.text
+ slide = slides[1]
+ assert_equal ["content"], slide.classes
+ assert_equal "# Two\n* dos\n", slide.text
+ end
+
+end
Something went wrong with that request. Please try again.