Class: MagicReveal::IndexLibber

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_reveal/index_libber.rb

Overview

Mad Libs with Reveal.JS’s index.html

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_text = nil) ⇒ IndexLibber

Returns a new instance of IndexLibber.



10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



8
9
10
# File 'lib/magic_reveal/index_libber.rb', line 8

def html
  @html
end

Instance Method Details

#add_github_forkme(project) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/magic_reveal/index_libber.rb', line 67

def add_github_forkme project
  a = Nokogiri::XML::Node.new('a', html)
  a[:class] = 'fork-me'
  a[:style] = 'display: none;'
  a[:href] = "https://github.com/#{project}"
  img = Nokogiri::XML::Node.new('img', html)
  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"
  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 << img
  parent << script
end

#author=(author) ⇒ Object



37
38
39
# File 'lib/magic_reveal/index_libber.rb', line 37

def author= author
  set_meta 'author', author
end

#description=(description) ⇒ Object



41
42
43
# File 'lib/magic_reveal/index_libber.rb', line 41

def description= description
  set_meta 'description', description
end

#set_meta(name, content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/magic_reveal/index_libber.rb', line 21

def set_meta name, content
  meta = html.at_css("meta[@name='#{name}']")
  if !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) ⇒ Object



59
60
61
62
63
64
65
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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")
  if !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) ⇒ Object



33
34
35
# File 'lib/magic_reveal/index_libber.rb', line 33

def title= title
  html.title = title
end

#to_sObject



85
86
87
# File 'lib/magic_reveal/index_libber.rb', line 85

def to_s
  html.to_html
end