Module: Zafu::Process::HTML

Defined in:
lib/zafu/process/html.rb

Constant Summary collapse

SAFE_SRC_REGEX =
%r{\A/[^\.]+\.[a-zA-Z]+\Z}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/zafu/process/html.rb', line 5

def self.included(base)
  base.wrap  :wrap_html
end

Instance Method Details

#compile_html_paramsObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zafu/process/html.rb', line 52

def compile_html_params
  return if @markup.done
  unless @markup.tag
    if @markup.tag = @params.delete(:tag)
      @markup.steal_html_params_from(@params)
    end
  end

  # Translate dynamic params such as <tt>class='#{visitor.lang}'</tt> in the context
  # of the current parser
  @markup.compile_params(self)
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/zafu/process/html.rb', line 48

def empty?
  super && @markup.params == {} && @markup.tag.nil?
end

#include_part(obj) ⇒ Object

Pass the caller’s ‘markup’ to the included part.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zafu/process/html.rb', line 33

def include_part(obj)
  if @markup.tag
    obj.markup = @markup.dup
  end
  @markup.tag = nil

  if sub_do
    obj.method = @blocks.first.method
    obj.params = @blocks.first.params
  elsif params[:method]
    obj.method = params[:method]
  end
  super(obj)
end

#inspectObject

def restore_markup

# restore @markup
@markup = @markup_bak

end



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zafu/process/html.rb', line 75

def inspect
  @markup.done = false
  res = super
  if @markup.tag
    if res =~ /\A\[(\w+)(.*)\/\]\Z/m
      res = "[#{$1}#{$2}]<#{@markup.tag}/>[/#{$1}]"
    elsif res =~ /\A\[([^\]]+)\](.*)\[\/(\w+)\]\Z/m
      res = "[#{$1}]#{@markup.wrap($2)}[/#{$3}]"
    end
  end
  res
end

#r_ignoreObject



88
89
90
91
# File 'lib/zafu/process/html.rb', line 88

def r_ignore
  @markup.done = true
  ''
end

#r_rename_assetObject



93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/zafu/process/html.rb', line 93

def r_rename_asset
  return expand_with unless @markup.tag
  case @markup.tag
  when 'link'
    key = :href
    return parser_error("Missing 'rel' parameter.") unless rel = @params[:rel]
    return parser_error("Missing 'href' parameter.") unless @params[:href]
    if rel.downcase == 'stylesheet'
      type = :stylesheet
    else
      type = :link
    end
  when 'style'
    return expand_with.gsub(/url\(('|")(.*?)\1\)/) do
      if $2[0..6] == 'http://'
        $&
      else
        quote   = $1
        new_src = helper.send(:template_url_for_asset, :base_path=>@options[:base_path], :src => $2)
        "url(#{quote}#{new_src}#{quote})"
      end
    end
  else
    key = :src
    type = @markup.tag.to_sym
  end

  src = @params.delete(key)
  if src && src[0..7] != 'http://'
    if new_value = helper.send(:template_url_for_asset, :src => src, :base_path => @options[:base_path], :type => type)
    elsif src =~ SAFE_SRC_REGEX
      fullpath = "#{SITES_ROOT}#{current_site.public_path}#{src}"
      if File.exist?(fullpath) && File.stat(fullpath)
        src = "#{src}?#{File.mtime(fullpath).to_i}"
      end
    end
    @markup.params[key] = new_value.blank? ? src : new_value
  end

  expand_with
end

#replace_with(new_obj) ⇒ Object

Replace the ‘original’ element in the included template with our new version.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zafu/process/html.rb', line 10

def replace_with(new_obj)
  super
  # [self = original_element]. Replace @markup with content of the new_obj (<ul do='with'>...)
  if new_obj.markup.tag
    @markup.tag = new_obj.markup.tag
  else
    # Steal 'class' param
    @markup.steal_html_params_from(new_obj.params)
  end

  @markup.params.merge!(new_obj.markup.params)

  # We do not have to merge dyn_params since these are compiled before processing (and we are in
  # the pre-processor)

  if new_obj.params[:method]
    @method   = new_obj.params[:method]
  elsif new_obj.sub_do
    @method = 'void'
  end
end

#steal_and_eval_html_params_for(markup, params) ⇒ Object



135
136
137
138
139
140
# File 'lib/zafu/process/html.rb', line 135

def steal_and_eval_html_params_for(markup, params)
  markup.steal_keys.each do |key|
    next unless value = params.delete(key)
    append_markup_attr(markup, key, value)
  end
end

#wrap_html(text) ⇒ Object



65
66
67
68
# File 'lib/zafu/process/html.rb', line 65

def wrap_html(text)
  compile_html_params
  @markup.wrap(text)
end