lib/showoff.rb in showoff-0.9.10.6 vs lib/showoff.rb in showoff-0.9.10.7

- old
+ new

@@ -207,11 +207,11 @@ else slide << line end end - slides.delete_if {|slide| slide.empty? } + slides.delete_if {|slide| slide.empty? and not slide.bg } final = '' if slides.size > 1 seq = 1 end @@ -275,11 +275,11 @@ # create html for the slide classes = content_classes.join(' ') content = "<div" content += " id=\"#{id}\"" if id - content += " style=\"background: url('file/#{slide.bg}') center no-repeat;\"" if slide.bg + content += " style=\"background-image: url('file/#{slide.bg}');\"" if slide.bg content += " class=\"slide #{classes}\" data-transition=\"#{transition}\">" # name the slide. If we've got multiple slides in this file, we'll have a sequence number # include that sequence number to index directly into that content if seq @@ -415,83 +415,84 @@ markdown.gsub(/<p>\.(.*?) /, '<p class="\1">') end # replace custom markup with html forms def build_forms(content, classes=[]) - classes.select { |cl| cl =~ /^form=(\w+)$/ } + title = classes.collect { |cl| $1 if cl =~ /^form=(\w+)$/ }.compact.first # only process slides marked as forms - return content unless $1 + return content if title.nil? begin tools = '<div class="tools">' tools << '<input type="button" class="display" value="Display Results">' tools << '<input type="submit" value="Save" disabled="disabled">' tools << '</div>' - form = "<form id='#{$1}' action='/form/#{$1}' method='POST'>#{content}#{tools}</form>" + form = "<form id='#{title}' action='/form/#{title}' method='POST'>#{content}#{tools}</form>" doc = Nokogiri::HTML::DocumentFragment.parse(form) doc.css('p').each do |p| if p.text =~ /^(\w*) ?(?:->)? ?([^\*]*)? ?(\*?)= ?(.*)?$/ - id = $1 - name = $2.empty? ? $1 : $2 + code = $1 + id = "#{title}_#{code}" + name = $2.empty? ? code : $2 required = ! $3.empty? rhs = $4 - p.replace form_element(id, name, required, rhs, p.text) + p.replace form_element(id, code, name, required, rhs, p.text) end end doc.to_html rescue Exception => e @logger.warn "Form parsing failed: #{e.message}" @logger.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}" content end end - def form_element(id, name, required, rhs, text) + def form_element(id, code, name, required, rhs, text) required = required ? 'required' : '' - str = "<div class='form element #{required}' id='#{id}'>" + str = "<div class='form element #{required}' id='#{id}' data-name='#{code}'>" str << "<label for='#{id}'>#{name}</label>" case rhs when /^\[\s+(\d*)\]$$/ # value = [ 5] (textarea) - str << form_element_textarea(id, name, $1) + str << form_element_textarea(id, code, $1) when /^___+(?:\[(\d+)\])?$/ # value = ___[50] (text) - str << form_element_text(id, name, $1) + str << form_element_text(id, code, $1) when /^\(x?\)/ # value = (x) option one () opt2 () opt3 -> option 3 (radio) - str << form_element_radio(id, name, rhs.scan(/\((x?)\)\s*([^()]+)\s*/)) + str << form_element_radio(id, code, rhs.scan(/\((x?)\)\s*([^()]+)\s*/)) when /^\[x?\]/ # value = [x] option one [] opt2 [] opt3 -> option 3 (checkboxes) - str << form_element_checkboxes(id, name, rhs.scan(/\[(x?)\] ?([^\[\]]+)/)) + str << form_element_checkboxes(id, code, rhs.scan(/\[(x?)\] ?([^\[\]]+)/)) when /^\{(.*)\}$/ # value = {BOS, SFO, (NYC)} (select shorthand) - str << form_element_select(id, name, rhs.scan(/\(?\w+\)?/)) + str << form_element_select(id, code, rhs.scan(/\(?\w+\)?/)) when /^\{$/ # value = { (select) - str << form_element_select_multiline(id, name, text) + str << form_element_select_multiline(id, code, text) when '' # value = (radio/checkbox list) - str << form_element_multiline(id, name, text) + str << form_element_multiline(id, code, text) else @logger.warn "Unmatched form element: #{rhs}" end str << '</div>' end - def form_element_text(id, name, length) - "<input type='text' id='#{id}' name='#{id}' size='#{length}' />" + def form_element_text(id, code, length) + "<input type='text' id='#{id}_response' name='#{code}' size='#{length}' />" end - def form_element_textarea(id, name, rows) + def form_element_textarea(id, code, rows) rows = 3 if rows.empty? - "<textarea id='#{id}' name='#{id}' rows='#{rows}'></textarea>" + "<textarea id='#{id}_response' name='#{code}' rows='#{rows}'></textarea>" end - def form_element_radio(id, name, items) - form_element_check_or_radio_set('radio', id, name, items) + def form_element_radio(id, code, items) + form_element_check_or_radio_set('radio', id, code, items) end - def form_element_checkboxes(id, name, items) - form_element_check_or_radio_set('checkbox', id, name, items) + def form_element_checkboxes(id, code, items) + form_element_check_or_radio_set('checkbox', id, code, items) end - def form_element_select(id, name, items) - str = "<select id='#{id}' name='#{name}'>" + def form_element_select(id, code, items) + str = "<select id='#{id}_response' name='#{code}'>" str << '<option value="">----</option>' items.each do |item| if item =~ /\((\w+)\)/ item = $1 @@ -502,12 +503,12 @@ str << "<option value='#{item}' #{selected}>#{item}</option>" end str << '</select>' end - def form_element_select_multiline(id, name, text) - str = "<select id='#{id}' name='#{id}'>" + def form_element_select_multiline(id, code, text) + str = "<select id='#{id}_response' name='#{code}'>" str << '<option value="">----</option>' text.split("\n")[1..-1].each do |item| case item when /^ +\((\w+) -> (.+)\),?$/ # (NYC -> New York City) @@ -521,11 +522,11 @@ end end str << '</select>' end - def form_element_multiline(id, name, text) + def form_element_multiline(id, code, text) str = '<ul>' text.split("\n")[1..-1].each do |item| case item when /\((x?)\)\s*(\w+)\s*(?:->\s*(.*)?)?/ @@ -539,17 +540,17 @@ value = $2 label = $3 || $2 end str << '<li>' - str << form_element_check_or_radio(type, id, value, label, checked) + str << form_element_check_or_radio(type, id, code, value, label, checked) str << '</li>' end str << '</ul>' end - def form_element_check_or_radio_set(type, id, name, items) + def form_element_check_or_radio_set(type, id, code, items) str = '' items.each do |item| checked = item[0].empty? ? '': "checked='checked'" if item[1] =~ /^(\w*) -> (.*)$/ @@ -557,20 +558,20 @@ label = $2 else value = label = item[1] end - str << form_element_check_or_radio(type, id, value, label, checked) + str << form_element_check_or_radio(type, id, code, value, label, checked) end str end - def form_element_check_or_radio(type, id, value, label, checked) + def form_element_check_or_radio(type, id, code, value, label, checked) # yes, value and id are conflated, because this is the id of the parent widget - id = "#{id}[]" if type == 'checkbox' - str = "<input type='#{type}' name='#{id}' id='#{value}' value='#{value}' #{checked} />" - str << "<label for='#{value}'>#{label}</label>" + name = (type == 'checkbox') ? "#{code}[]" : code + str = "<input type='#{type}' name='#{name}' id='#{id}_#{value}' value='#{value}' #{checked} />" + str << "<label for='#{id}_#{value}'>#{label}</label>" end # TODO: deprecated def update_special_content(content, seq, name) doc = Nokogiri::HTML::DocumentFragment.parse(content)