class MagicReveal::IndexLibber

Mad Libs with Reveal.JS's index.html

Attributes

html[R]

Public Class Methods

new(html_text = nil) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 10
def initialize(html_text = nil)
  if html_text.nil?
    template_path = Pathname.new(__FILE__).dirname + 'template.html'
    html_text = template_path.read
  end
  @html = Nokogiri::HTML(html_text, &:noblanks)
  # Add autogenerated comment
  # html.children.first.add_previous_sibling(Nokogiri::XML::Comment.new("Generated at #{Time.now}"))
  html.root.children.first.before Nokogiri::XML::Comment.new(html, "Generated at #{Time.now}")
end

Public Instance Methods

add_github_forkme(project) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 83
def add_github_forkme(project)
  a = github_fork_link project
  script = Nokogiri::XML::Node.new('script', html)
  script.content = "if( !navigator.userAgent.match( /iphone|ipod|android|ipad|blackberry/gi  ) && !!document.querySelector  ) { document.querySelector('.fork-me' ).style.display = 'block'; }"

  parent = (body = html.at('body')) ? body : html.root
  parent << a
  a << github_banner_img
  parent << script
end
author=(author) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 37
def author=(author)
  set_meta 'author', author
end
description=(description) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 41
def description=(description)
  set_meta 'description', description
end
github_banner_img() click to toggle source
# File lib/magic_reveal/index_libber.rb, line 67
def github_banner_img
  Nokogiri::XML::Node.new('img', html).tap do |img|
    img[:style] = 'position: absolute; top: 0; right: 0; border: 0;'
    img[:src] = 'https://a248.e.akamai.net/camo.github.com/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67'
    img[:alt] = 'Fork me on GitHub'
  end
end
set_meta(name, content) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 21
def set_meta(name, content)
  meta = html.at_css("meta[@name='#{name}']")
  unless meta
    meta = Nokogiri::XML::Node.new('meta', html)
    meta[:name]    = name

    parent = (head = html.at('head')) ? head : html.root
    parent << meta
  end
  meta[:content] = content
end
slides=(text) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 59
def slides=(text)
  slides = Nokogiri::HTML.fragment(text).children
  container = html.at_css('div.reveal div.slides')
  container.children = slides
  headers = slides.css('h1', 'h2', 'h3', 'h4', 'h5', 'h6')
  self.title = headers.first.text unless headers.nil? || headers.empty?
end
theme=(theme) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 45
def theme=(theme)
  # <link rel="stylesheet" href="css/theme/default.css" id="theme">
  node = html.at_css('link#theme')
  unless node
    node = Nokogiri::XML::Node.new('link', html)
    node[:id]   = 'theme'
    node[:rel]  = 'stylesheet'

    parent = (head = html.at('head')) ? head : html.root
    parent << node
  end
  node[:href] = "css/theme/#{theme}.css"
end
title=(title) click to toggle source
# File lib/magic_reveal/index_libber.rb, line 33
def title=(title)
  html.title = title
end
to_s() click to toggle source
# File lib/magic_reveal/index_libber.rb, line 94
def to_s
  html.to_html
end