Class: Slideshow::Slide

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/slideshow/models/slide.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, header_level: 2) ⇒ Slide

def to_drop() SlideDrop.new( self ); end – add - why? why not??



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/slideshow/models/slide.rb', line 20

def initialize( content, header_level: 2 )
  ## options
  @header_level = 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.



15
16
17
# File 'lib/slideshow/models/slide.rb', line 15

def classes
  @classes
end

#contentObject

NB: unparsed html source (use content_without_header

for content without option header)


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

def content
  @content
end

#content_without_headerObject

Returns the value of attribute content_without_header.



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

def content_without_header
  @content_without_header
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

Instance Method Details

#data_attributesObject



88
89
90
91
92
93
94
95
96
# File 'lib/slideshow/models/slide.rb', line 88

def data_attributes
  return nil if @data.empty?   ## note: return nil for empty hash
  
  buf = ""
  @data.each do | key,value |
    buf << "data-#{key}='#{value}' "
  end
  buf
end

#parseObject



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
80
81
82
83
84
85
# File 'lib/slideshow/models/slide.rb', line 34

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 == 1
    pattern = /^(.*?)(<h1.*?>.*?<\/h1>)(.*)/m
  else # assume header level 2
    ## note: header_level 2 also incl. header_level 1 slides
    pattern = /^(.*?)(<(?:h1|h2).*?>.*?<\/(?:h1|h2)>)(.*)/m
  end

  if @content =~ pattern
    @header  = $2
    @content_without_header = ($1 ? $1 : '') + ($3 ? $3 : '')
    logger.debug "  adding slide with header:\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



101
102
103
104
105
106
107
108
109
# File 'lib/slideshow/models/slide.rb', line 101

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



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/slideshow/models/slide.rb', line 111

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