Class: Eggshell::Bundles::Basic::TextBlocks

Inherits:
Object
  • Object
show all
Includes:
Eggshell::BlockHandler, Eggshell::BlockHandler::BlockParams, Eggshell::BlockHandler::HtmlUtils
Defined in:
lib/eggshell/bundles/basics.rb

Constant Summary collapse

HTML_BLOCK =
TODO:

what else should be treated?

HTML tags that have end-block checks. any block starting with one of these tags will have its contents passed through until end of the tag (essentially, raw)

/^<(style|script|table|dl|select|textarea|\!--|\?)/
HTML_BLOCK_END =
{
  '<!--' => '-->',
  '<?' => '\\?>'
}.freeze
HTML_PASSTHRU =

For lines starting with only these tags, accept as-is

/^\s*<(\/?(html|head|meta|link|title|body|br|section|div|blockquote|p|pre))/
START_TEXT =
/^(p|bq|pre|raw|div)[(.]/

Constants included from Eggshell::BlockHandler::HtmlUtils

Eggshell::BlockHandler::HtmlUtils::HASH_HTML_ESCAPE

Constants included from Eggshell::BlockHandler::BlockParams

Eggshell::BlockHandler::BlockParams::ATTRIBUTES, Eggshell::BlockHandler::BlockParams::CLASS, Eggshell::BlockHandler::BlockParams::ID, Eggshell::BlockHandler::BlockParams::KEYS, Eggshell::BlockHandler::BlockParams::STYLE

Constants included from Eggshell::BlockHandler

Eggshell::BlockHandler::COLLECT, Eggshell::BlockHandler::COLLECT_RAW, Eggshell::BlockHandler::DONE, Eggshell::BlockHandler::RETRY

Instance Method Summary collapse

Methods included from Eggshell::BlockHandler::HtmlUtils

#attrib_string, #create_tag, #css_string, #html_escape, #inject_attribs

Methods included from Eggshell::BlockHandler::BlockParams

#get_block_params, #set_block_params

Methods included from Eggshell::BlockHandler

#current_type, #equal?, #reset, #set_processor

Methods included from Eggshell::BaseHandler

#set_processor

Constructor Details

#initializeTextBlocks

Returns a new instance of TextBlocks.



42
43
44
# File 'lib/eggshell/bundles/basics.rb', line 42

def initialize
  @block_types = ['p', 'bq', 'pre', 'div', 'raw', 'html_pass', 'html_block']
end

Instance Method Details

#can_handle(line) ⇒ Object



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
# File 'lib/eggshell/bundles/basics.rb', line 48

def can_handle(line)
  match = START_TEXT.match(line)
  if match
    @block_type = match[1]
    if @block_type == 'pre' || @block_type == 'raw'
      return BH::COLLECT_RAW
    else
      return BH::COLLECT
    end
  end
  
  if line.match(HTML_PASSTHRU)
    @block_type = 'html_pass'
    return BH::DONE
  end
  
  html = line.match(HTML_BLOCK)
  if html
    @block_type = 'html_block'
    end_html = HTML_BLOCK_END["<#{html[1]}"]
    end_html = "</#{html[1]}>" if !end_html
    if line.match(end_html)
      return BH::DONE
    end

    @end_html = end_html
    return BH::COLLECT_RAW
  end

  return BH::RETRY
end

#continue_with(line) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/eggshell/bundles/basics.rb', line 80

def continue_with(line)
  if @block_type == 'html_block'
    done = line.match(@end_html)
    if done
      return BH::DONE
    else
      return BH::COLLECT
    end
  else
    if @block_type == 'pre' || @block_type == 'raw'
      if line && line != ''
        return BH::COLLECT
      end
    elsif line
      if line == '' || line.match(HTML_PASSTHRU) != nil || line.match(HTML_BLOCK) != nil
        return BH::RETRY
      end
      return BH::COLLECT
    end
  end

  return BH::RETRY
end

#process(type, args, lines, out, call_depth = 0) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/eggshell/bundles/basics.rb', line 104

def process(type, args, lines, out, call_depth = 0)
  buff = []

  if type == 'html_pass' || type == 'html_block'
    out << @eggshell.expand_expr(lines.join("\n"))
  else
    # @todo if pre starts with blank line, remove it
    tagname = type == 'bq' ? 'blockquote' : type
    args = [] if !args
    bp = get_block_params(type, args[0])
    raw = type == 'pre' || type == 'raw'

    line_break = raw ? '' : '<br />'
    lines.each do |line|
      str = line.is_a?(Eggshell::Line) ? line.to_s : line
      str.chomp!
      buff << str
    end

    # @todo don't call expand_expr if raw && args[0]['no_expand']?
    buff = buff.join("#{line_break}\n")
    buff = @eggshell.expand_formatting(buff) if !raw
    buff = @eggshell.unescape(@eggshell.expand_expr(buff))
    
    if type != 'raw'
      out << [create_tag(type, bp), buff, "</#{type}>"].join('')
    else
      out << buff
    end
  end
end