Class: WikiCloth::WikiBuffer::HTMLElement

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

Constant Summary collapse

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','boder',
'cellpadding','cellspacing']
ESCAPED_TAGS =
[ 'nowiki', 'pre', 'code' ]
SHORT_TAGS =
[ 'meta','br','hr']
NO_NEED_TO_CLOSE =
['li','p'] + SHORT_TAGS

Instance Method Summary collapse

Methods inherited from WikiCloth::WikiBuffer

#add_char, #buffer_type, #check_globals, #data, #params, #skip_html?

Constructor Details

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

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 unless check.nil?
end

Instance Method Details

#element_attributesObject



87
88
89
90
91
92
93
94
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 87

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

#element_contentObject



100
101
102
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 100

def element_content
  @econtent ||= ""
end

#element_nameObject



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

def element_name
  @ename ||= ""
end

#name_attributeObject



82
83
84
85
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 82

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

#run_globals?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 26

def run_globals?
  return ESCAPED_TAGS.include?(self.element_name) ? false : true
end

#to_sObject



30
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
# File 'lib/wikicloth/wiki_buffer/html_element.rb', line 30

def to_s
  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)
    # 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

  lhandler = @options[:link_handler]
  case self.element_name
  when "ref"
    self.element_name = "sup"
    named_ref = self.name_attribute
    ref = lhandler.find_reference_by_name(named_ref) unless named_ref.nil?
    if ref.nil?
      lhandler.references << { :name => named_ref, :value => self.element_content, :count => 0 }
      ref = lhandler.references.last
    end
    ref_id = (named_ref.nil? ? "" : "#{named_ref}_") + "#{lhandler.reference_index(ref)}-#{ref[:count]}"
    self.params << { :name => "id", :value => "cite_ref-#{ref_id}" }
    self.params << { :name => "class", :value => "reference" }
    self.element_content = "[<a href=\"#cite_note-" + (named_ref.nil? ? "" : "#{named_ref}_") + 
	"#{lhandler.reference_index(ref)}\">#{lhandler.reference_index(ref)}</a>]"
    ref[:count] += 1
  when "references"
    ref_count = 0
    self.element_name = "ol"
    self.element_content = lhandler.references.collect { |r| 
      ref_count += 1
      ref_name = (r[:name].nil? ? "" : r[:name].to_slug + "_")
      ret = "<li id=\"cite_note-#{ref_name}#{ref_count}\"><b>"
      1.upto(r[:count]) { |x| ret += "<a href=\"#cite_ref-#{ref_name}#{ref_count}-#{x-1}\">" + 
(r[:count] == 1 ? "^" : (x-1).to_s(26).tr('0-9a-p', 'a-z')) + "</a> " }
      ret += "</b> #{r[:value]}</li>"
    }.to_s
  when "nowiki"
    return self.element_content
  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