Class: Slideshow::Slide
- Inherits:
-
Object
- Object
- Slideshow::Slide
- Defined in:
- lib/slideshow/slide.rb
Instance Attribute Summary collapse
-
#classes ⇒ Object
Returns the value of attribute classes.
-
#content ⇒ Object
NB: unparsed html source (use content_without_header for content without option header).
-
#content_without_header ⇒ Object
Returns the value of attribute content_without_header.
-
#header ⇒ Object
Returns the value of attribute header.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #data_attributes ⇒ Object
-
#initialize(content, options) ⇒ Slide
constructor
A new instance of Slide.
- #parse ⇒ Object
-
#to_classic_html ⇒ Object
convenience helpers.
- #to_google_html5 ⇒ Object
Constructor Details
#initialize(content, options) ⇒ Slide
Returns a new instance of Slide.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/slideshow/slide.rb', line 17 def initialize( content, ) @logger = .logger @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
#classes ⇒ Object
Returns the value of attribute classes.
15 16 17 |
# File 'lib/slideshow/slide.rb', line 15 def classes @classes end |
#content ⇒ Object
NB: unparsed html source (use content_without_header
for content without option header)
12 13 14 |
# File 'lib/slideshow/slide.rb', line 12 def content @content end |
#content_without_header ⇒ Object
Returns the value of attribute content_without_header.
13 14 15 |
# File 'lib/slideshow/slide.rb', line 13 def content_without_header @content_without_header end |
#header ⇒ Object
Returns the value of attribute header.
14 15 16 |
# File 'lib/slideshow/slide.rb', line 14 def header @header end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/slideshow/slide.rb', line 7 def logger @logger end |
Instance Method Details
#data_attributes ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/slideshow/slide.rb', line 84 def data_attributes buf = "" @data.each do | key,value | buf << "data-#{key}='#{value}' " end buf end |
#parse ⇒ Object
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 80 81 |
# File 'lib/slideshow/slide.rb', line 31 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_html ⇒ Object
convenience helpers
95 96 97 98 99 100 101 102 103 |
# File 'lib/slideshow/slide.rb', line 95 def to_classic_html buf = "" buf << "<div class='slide " buf << classes if classes buf << "'>\n" buf << content buf << "</div>\n" buf end |
#to_google_html5 ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/slideshow/slide.rb', line 105 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 |