Class: WikiCloth::WikiBuffer::HTMLElement

Inherits:
WikiCloth::WikiBuffer show all
Defined in:
lib/wikicloth/wiki_buffer/html_element.rb

Constant Summary

ALLOWED_ELEMENTS =
['a','b','i','img','div','span','sup','sub','strike','s','u','font','big','ref','tt','del',
'small','blockquote','strong','pre','code','references','ol','li','ul','dd','dt','dl','center',
'h1','h2','h3','h4','h5','h6','p','table','tr','td','th','tbody','thead','tfoot']
ALLOWED_ATTRIBUTES =
['src','id','name','style','class','href','start','value','colspan','align','border',
'cellpadding','cellspacing','name','valign','color','rowspan','nowrap','title','rel','for']
ESCAPED_TAGS =
[ 'nowiki','pre','code' ]
SHORT_TAGS =
[ 'meta','br','hr']
NO_NEED_TO_CLOSE =
['li','p'] + SHORT_TAGS
DISABLE_GLOBALS_FOR =
ESCAPED_TAGS

Constants inherited from WikiCloth::WikiBuffer

BOLD_ITALIC_MAP

Instance Method Summary (collapse)

Methods inherited from WikiCloth::WikiBuffer

#add_char, #add_word, #buffer_type, #buffers, #check_globals, #data, #eof, #get_param, #in_template?, #params, #skip_links?

Constructor Details

- (HTMLElement) initialize(d = "", options = {}, check = nil)

Returns a new instance of HTMLElement



17
18
19
20
21
22
23
24
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 17

def initialize(d="",options={},check=nil)
  super("",options)
  self.buffer_type = "Element"
  @in_quotes = false
  @in_single_quotes = false
  @start_tag = 1
  @tag_check = check
end

Instance Method Details

- (Object) debug



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 26

def debug
  case self.element_name
  when "template"
    self.get_param("__name")
  else
    ret = self.class.to_s + "["
    ret += self.element_name
    tmp = self.get_param("id")
    ret += tmp.nil? ? "" : "##{tmp}"
    ret + "]"
  end
end

- (Object) element_attributes



102
103
104
105
106
107
108
109
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 102

def element_attributes
  attr = {}
  params.each { |p| attr[p[:name]] = p[:value] if p.kind_of?(Hash) }
  if ALLOWED_ELEMENTS.include?(self.element_name.strip.downcase)
    attr.delete_if { |key,value| !ALLOWED_ATTRIBUTES.include?(key.strip) }
  end
  return attr
end

- (Object) element_content



115
116
117
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 115

def element_content
  @econtent ||= ""
end

- (Object) element_name



111
112
113
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 111

def element_name
  @ename ||= ""
end

- (Object) get_attribute_by_name(name)



97
98
99
100
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 97

def get_attribute_by_name(name)
  params.each { |p| return p[:value] if p.kind_of?(Hash) && p[:name] == name }
  nil
end

- (Boolean) run_globals?

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 44

def run_globals?
  return false if self.in_template? && self.element_name == "noinclude"
  return false if !self.in_template? && self.element_name == "includeonly"
  return Extension.run_globals?(self.element_name) if Extension.element_exists?(self.element_name)
  return DISABLE_GLOBALS_FOR.include?(self.element_name) ? false : true
end

- (Boolean) skip_html?

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 39

def skip_html?
  return Extension.skip_html?(self.element_name) if Extension.element_exists?(self.element_name)
  DISABLE_GLOBALS_FOR.include?(self.element_name) ? true : false
end

- (Object) to_html



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 51

def to_html
  if NO_NEED_TO_CLOSE.include?(self.element_name)
    return "<#{self.element_name} />" if SHORT_TAGS.include?(self.element_name)
    return "</#{self.element_name}><#{self.element_name}>" if @tag_check == self.element_name
  end

  if ESCAPED_TAGS.include?(self.element_name)
    # remove empty first line
    self.element_content = $1 if self.element_content =~ /^\s*\n(.*)$/m
    # escape all html inside this element
    self.element_content = self.element_content.gsub('<','&lt;').gsub('>','&gt;')
    # hack to fix <code><nowiki> nested mess
    self.element_content = self.element_content.gsub(/&lt;[\/]*\s*nowiki\s*&gt;/,'')
  end

  case self.element_name
  when "template"
    @options[:link_handler].cache({ :name => self.element_attributes["__name"], :content => self.element_content, :md5 => self.element_attributes["__hash"] })
    return self.element_content
  when "noinclude"
    return self.in_template? ? "" : self.element_content
  when "includeonly"
    return self.in_template? ? self.element_content : ""
  when "nowiki"
    return self.element_content
  when "a"
    if self.element_attributes['href'] =~ /:\/\//
      return @options[:link_handler].external_link(self.element_attributes['href'], self.element_content)
    elsif self.element_attributes['href'].nil? || self.element_attributes['href'] =~ /^\s*([\?\/])/
      # if a element has no href attribute, or href starts with / or ?
      return elem.tag!(self.element_name, self.element_attributes) { |x| x << self.element_content }
    end
  else
    if Extension.element_exists?(self.element_name)
      return Extension.html_elements[self.element_name][:klass].new(@options).instance_exec( self, &Extension.html_elements[self.element_name][:block] ).to_s
    end
  end

  tmp = elem.tag!(self.element_name, self.element_attributes) { |x| x << self.element_content }
  unless ALLOWED_ELEMENTS.include?(self.element_name)
    tmp.gsub!(/[\-!\|&"\{\}\[\]]/) { |r| self.escape_char(r) }
    return tmp.gsub('<', '&lt;').gsub('>', '&gt;')
  end
  tmp
end