Class: Slideshow::Slide

Inherits:
Object
  • Object
show all
Defined in:
lib/slideshow/slide.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, options) ⇒ Slide

Returns a new instance of Slide.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slideshow/slide.rb', line 15

def initialize( content, options )
  @logger       = options.logger
  @header_level = options.header_level
  
  @content = content
  
  @header                 = nil
  @content_without_header = nil
  @classes                = nil   # NB: style classes as all in one string
  @data    = {}
  
  parse()
end

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



13
14
15
# File 'lib/slideshow/slide.rb', line 13

def classes
  @classes
end

#contentObject

NB: unparsed html source (use content_without_header

for content without option header)


10
11
12
# File 'lib/slideshow/slide.rb', line 10

def content
  @content
end

#content_without_headerObject

Returns the value of attribute content_without_header.



11
12
13
# File 'lib/slideshow/slide.rb', line 11

def content_without_header
  @content_without_header
end

#headerObject

Returns the value of attribute header.



12
13
14
# File 'lib/slideshow/slide.rb', line 12

def header
  @header
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/slideshow/slide.rb', line 5

def logger
  @logger
end

Instance Method Details

#data_attributesObject



82
83
84
85
86
87
88
# File 'lib/slideshow/slide.rb', line 82

def data_attributes
  buf = ""
  @data.each do | key,value |
    buf << "data-#{key}='#{value}' "
  end
  buf
end

#parseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/slideshow/slide.rb', line 29

def parse
  ## pass 1) check for (css style) classes and data attributes
  ## check for css style classes
  from = 0
  while( pos = @content.index( /<!-- _S9(SLIDE|STYLE)_(.*?)-->/m, from ))
    logger.debug "  adding css classes from pi >#{$1.downcase}<: >#{$2.strip}<"

    from = Regexp.last_match.end(0)  # continue search later from here
    
    values = $2.strip.dup
    
    # remove data values (eg. x=-20 scale=4) and store in data hash
    values.gsub!( /([-\w]+)[ \t]*=[ \t]*([-\w\.]+)/ ) do |_|
      logger.debug "    adding data pair: key=>#{$1.downcase}< value=>#{$2}<"
      @data[ $1.downcase.dup ] = $2.dup
      " "  # replace w/ space
    end
    
    values.strip!  # remove spaces  # todo: use squish or similar and check for empty string
            
    if @classes.nil?
      @classes = values
    else
      @classes << " #{values}"
    end
  end

  ## pass 2) split slide source into header (optional) and content/body

  ## todo: add option split on h1/h2 etc.

  # try to extract first header using non-greedy .+? pattern;
  # tip test regex online at rubular.com
  #  note/fix: needs to get improved to also handle case for h1 wrapped into div

  if @header_level == 2
    pattern = /^(.*?)(<h2.*?>.*?<\/h2>)(.*)/m
  else # assume header level 1
    pattern = /^(.*?)(<h1.*?>.*?<\/h1>)(.*)/m
  end

  if @content =~ pattern
    @header  = $2
    @content_without_header = ($1 ? $1 : '') + ($3 ? $3 : '')
    logger.debug "  adding slide with header (h1):\n#{@header}"
  else
    @header = nil    # todo: set to '' empty string? why not?
    @content_without_header = @content
    logger.debug "  adding slide with *no* header:\n#{@content}"
  end
end

#to_classic_htmlObject

convenience helpers



93
94
95
96
97
98
99
100
101
# File 'lib/slideshow/slide.rb', line 93

def to_classic_html
  buf  = ""
  buf << "<div class='slide "
  buf << classes    if classes
  buf << "'>\n"
  buf << content
  buf << "</div>\n"
  buf
end

#to_google_html5Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/slideshow/slide.rb', line 103

def to_google_html5
  buf  = ""
  buf << "<div class='slide'>\n"
  buf << "<header>#{header}</header>\n"   if header
  buf << "<section "
  buf << "class='#{classes}'"             if classes
  buf << "'>\n"
  buf << content_without_header           if content_without_header
  buf << "</section>\n"
  buf << "</div>\n"
  buf
end