lib/showoff.rb in showoff-0.20.1 vs lib/showoff.rb in showoff-0.20.2

- old
+ new

@@ -26,11 +26,11 @@ # nop end require 'tilt' -class ShowOff < Sinatra::Application +class Showoff < Sinatra::Application attr_reader :cached_image_size # Set up application variables @@ -90,17 +90,17 @@ @keycode_dictionary = Keymap.keycodeDictionary @keycode_shifted_keys = Keymap.shiftedKeyDictionary settings.pres_dir = File.expand_path(settings.pres_dir) if (settings.pres_file and settings.pres_file != 'showoff.json') - ShowOffUtils.presentation_config_file = settings.pres_file + ShowoffUtils.presentation_config_file = settings.pres_file end # Load configuration for page size and template from the # configuration JSON file - if File.exist?(ShowOffUtils.presentation_config_file) - showoff_json = JSON.parse(File.read(ShowOffUtils.presentation_config_file)) + if File.exist?(ShowoffUtils.presentation_config_file) + showoff_json = JSON.parse(File.read(ShowoffUtils.presentation_config_file)) settings.showoff_config = showoff_json # Set options for encoding, template and page size settings.encoding = showoff_json["encoding"] || 'UTF-8' settings.page_size = showoff_json["page-size"] || "Letter" @@ -203,20 +203,20 @@ if @interactive # flush stats to disk periodically Thread.new do loop do sleep 30 - ShowOff.flush + Showoff.flush end end end # Initialize Markdown Configuration MarkdownConfig::setup(settings.pres_dir) # Process renderer config options - @engine_options = ShowOffUtils.showoff_renderer_options(settings.pres_dir) + @engine_options = ShowoffUtils.showoff_renderer_options(settings.pres_dir) end # save stats to disk def self.flush begin @@ -243,11 +243,11 @@ end end def self.pres_dir_current opt = {:pres_dir => Dir.pwd} - ShowOff.set opt + Showoff.set opt end def require_ruby_files Dir.glob("#{settings.pres_dir}/*.rb").map { |path| require path } end @@ -367,11 +367,11 @@ @tpl = "default" @classes = [] # Parse the context string for options and content classes if context and context.match(/(\[(.*?)\])?(.*)/) - options = ShowOffUtils.parse_options($2) + options = ShowoffUtils.parse_options($2) @tpl = options["tpl"] if options["tpl"] @bg = options["bg"] if options["bg"] @classes += $3.strip.chomp('>').split if $3 end @@ -571,11 +571,25 @@ # replace with form rendering placeholder result.gsub!(/~~~FORM:([^~]*)~~~/, '<div class="form wrapper" title="\1"></div>') # Now check for any kind of options content.scan(/(~~~CONFIG:(.*?)~~~)/).each do |match| - result.gsub!(match[0], settings.showoff_config[match[1]]) if settings.showoff_config.key?(match[1]) + parts = match[1].split('.') # Use dots ('.') to separate Hash keys + if parts.size > 1 + value = settings.showoff_config.dig(parts[0]).to_h.dig(*parts[1..-1]) + else + value = settings.showoff_config.fetch(parts[0],nil) + end + + unless value.is_a?(String) + msg = "#{match[0]} refers to a non-String data type (#{value.class})" + msg = "#{match[0]}: not found in settings data" if value.nil? + @logger.warn(msg) + next + end + + result.gsub!(match[0], value) end # Load and replace any file tags content.scan(/(~~~FILE:([^:~]*):?(.*)?~~~)/).each do |match| # make a list of code highlighting classes to include @@ -1149,11 +1163,11 @@ end def get_slides_html(opts={:static=>false, :pdf=>false, :toc=>false, :supplemental=>nil, :section=>nil}) sections = nil Dir.chdir(get_locale_dir('locales', @locale)) do - sections = ShowOffUtils.showoff_sections(settings.pres_dir, settings.showoff_config, @logger) + sections = ShowoffUtils.showoff_sections(settings.pres_dir, settings.showoff_config, @logger) end if sections data = '' sections.each do |section, slides| @@ -1217,13 +1231,13 @@ inline_js(Dir.entries(File.join(File.dirname(__FILE__), '..', jses_directory)).find_all{|filename| filename.length > 2 }, jses_directory) end def index(static=false) if static - @title = ShowOffUtils.showoff_title(settings.pres_dir) + @title = ShowoffUtils.showoff_title(settings.pres_dir) @slides = get_slides_html(:static=>static) - @pause_msg = ShowOffUtils.pause_msg + @pause_msg = ShowoffUtils.pause_msg end # Display favicon in the window if configured @favicon = settings.showoff_config['favicon'] @@ -1301,11 +1315,11 @@ return @@cache[@locale] if (@@cache[@locale] and params['cache'] != 'clear') @logger.info "Generating locale: #{@locale}" # If we're displaying from a repository, let's update it - ShowOffUtils.update(settings.verbose) if settings.url + ShowoffUtils.update(settings.verbose) if settings.url @@slide_titles = [] content = get_slides_html(:static=>static, :merged=>merged) # allow command line cache disabling @@ -1448,11 +1462,11 @@ "<img src=\"file:\/\/#{settings.pres_dir}\/#{$1}" end # PDFKit.new takes the HTML and any options for wkhtmltopdf # run `wkhtmltopdf --extended-help` for a full list of options - kit = PDFKit.new(html, ShowOffUtils.showoff_pdf_options(settings.pres_dir)) + kit = PDFKit.new(html, ShowoffUtils.showoff_pdf_options(settings.pres_dir)) # Save the PDF to a file kit.to_file(name) end @@ -1462,15 +1476,15 @@ def self.do_static(args, opts = {}) args ||= [] # handle nil arguments what = args[0] || "index" opt = args[1] - ShowOffUtils.presentation_config_file = opts[:f] + ShowoffUtils.presentation_config_file = opts[:f] # Sinatra now aliases new to new! # https://github.com/sinatra/sinatra/blob/v1.3.3/lib/sinatra/base.rb#L1369 - showoff = ShowOff.new! + showoff = Showoff.new! name = showoff.instance_variable_get(:@pres_name) path = showoff.instance_variable_get(:@root_path) logger = showoff.instance_variable_get(:@logger) @@ -1502,10 +1516,12 @@ # Now copy all the js and css my_path = File.join( File.dirname(__FILE__), '..', 'public') ["js", "css"].each { |dir| FileUtils.copy_entry("#{my_path}/#{dir}", "#{out}/#{dir}", false, false, true) } + + # @todo: uh. I don't know how this ever worked. my_path is showoff and name is presentation. # And copy the directory Dir.glob("#{my_path}/#{name}/*").each { |subpath| base = File.basename(subpath) next if "static" == base next unless File.directory?(subpath) || base.match(/\.(css|js)$/) @@ -1517,14 +1533,19 @@ FileUtils.makedirs(file_dir) pres_dir = showoff.settings.pres_dir # ..., copy all user-defined styles and javascript files showoff.css_files.each { |path| - dest = File.join(file_dir, path) + dest = File.join(out, path) FileUtils.mkdir_p(File.dirname(dest)) FileUtils.copy(path, dest) } + showoff.js_files.each { |path| + dest = File.join(out, path) + FileUtils.mkdir_p(File.dirname(dest)) + FileUtils.copy(path, dest) + } # ... and copy all needed image files [/img src=[\"\'].\/file\/(.*?)[\"\']/, /style=[\"\']background(?:-image): url\(\'file\/(.*?)'/].each do |regex| data.scan(regex).flatten.each do |path| dir = File.dirname(path) @@ -1948,12 +1969,12 @@ end # gawd, this whole routing scheme is bollocks get %r{/([^/]*)/?([^/]*)} do @locale = locale(request.cookies['locale']) - @title = ShowOffUtils.showoff_title(settings.pres_dir) - @pause_msg = ShowOffUtils.pause_msg + @title = ShowoffUtils.showoff_title(settings.pres_dir) + @pause_msg = ShowoffUtils.pause_msg what = params[:captures].first opt = params[:captures][1] what = 'index' if "" == what if settings.showoff_config['protected'].include? what @@ -1964,10 +1985,12 @@ begin if (what != "favicon.ico") if ['supplemental', 'print'].include? what data = send(what, opt) + elsif File.file? what + data = File.open(what) else data = send(what) end if data.is_a?(File) send_file data.path @@ -1987,9 +2010,9 @@ @env = request.env erb :'404' end at_exit do - ShowOff.flush + Showoff.flush end end