Class: WikiCloth::WikiBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/wikicloth/wiki_buffer.rb

Direct Known Subclasses

HTMLElement, Link, Table, Var

Defined Under Namespace

Classes: HTMLElement, Link, Table, Var

Instance Method Summary collapse

Constructor Details

#initialize(data = "", options = {}) ⇒ WikiBuffer

Returns a new instance of WikiBuffer.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/wikicloth/wiki_buffer.rb', line 4

def initialize(data="",options={})
  @options = options
  @options[:buffer] ||= self
  self.data = data
  self.buffer_type = nil
  @section_count = 0
  @buffers ||= [ ]
  @buffers << self
  @list_data = []
  @check_new_tag = false
  @indent = nil
end

Instance Method Details

#add_char(c) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/wikicloth/wiki_buffer.rb', line 122

def add_char(c)
  self.previous_char = self.current_char
  self.current_char = c

  if self.check_globals() == false
    case
    when @buffers.size == 1
      return self.new_char()
    when @buffers[-1].add_char(c) == false && self.class == WikiBuffer
      tmp = @buffers.pop
      @buffers[-1].data += tmp.to_s
      # any data left in the buffer we feed into the parent
      unless tmp.data.blank?
        tmp.data.each_char { |x| self.add_char(x) }
      end
    end
  end
end

#buffer_typeObject



56
57
58
# File 'lib/wikicloth/wiki_buffer.rb', line 56

def buffer_type
  @buffer_type
end

#buffersObject



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

def buffers
  @buffers
end

#check_globalsObject



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

def check_globals()
  return false if self.class != WikiBuffer

  if previous_char == "\n"
    if @indent == @buffers[-1].object_id && current_char != " " && current_char != "\n"
      # close pre tag
      cc_temp = current_char
      "</pre>\n".each_char { |c| self.add_char(c) }
      # get the parser back on the right track
      "\n#{cc_temp}".each_char { |c| @buffers[-1].add_char(c) }
      @indent = nil
      return true
    end
    if current_char == " " && @indent.nil? && @buffers[-1].class != WikiBuffer::HTMLElement
      "\n<pre> ".each_char { |c| @buffers[-1].add_char(c) }
      @indent = @buffers[-1].object_id
      return true
    end
  end

  if @buffers[-1].run_globals?
    # new html tag
    if @check_new_tag == true && current_char =~ /([a-z])/ && !@buffers[-1].skip_html?
      @buffers[-1].data.chop!
      parent = @buffers[-1].element_name if @buffers[-1].class == WikiBuffer::HTMLElement
      @buffers << WikiBuffer::HTMLElement.new("",@options,parent)
    end
    @check_new_tag = current_char == '<' ? true : false

    # global
    case
    # start variable
    when previous_char == '{' && current_char == '{'
      if @buffers[-1].instance_of?(WikiBuffer::Var) && @buffers[-1].tag_start == true
        @buffers[-1].tag_size += 1
      else
        @buffers[-1].data.chop!
        @buffers << WikiBuffer::Var.new("",@options)
      end
      return true

    # start link
    when current_char == '[' && previous_char != '['
      @buffers << WikiBuffer::Link.new("",@options)
      return true

    # start table
    when previous_char == '{' && current_char == "|"
      @buffers[-1].data.chop!
      @buffers << WikiBuffer::Table.new("",@options)
      return true

    end
  end

  return false
end

#dataObject



33
34
35
# File 'lib/wikicloth/wiki_buffer.rb', line 33

def data
  @data ||= ""
end

#debugObject



17
18
19
# File 'lib/wikicloth/wiki_buffer.rb', line 17

def debug
  self.params[0]
end

#get_param(name, default = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/wikicloth/wiki_buffer.rb', line 41

def get_param(name,default=nil)
  ret = nil
  self.params.each do |param|
    ret = param[:value] if param.instance_of?(Hash) && param[:name] == name
  end
  ret.nil? ? default : ret
end

#in_template?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/wikicloth/wiki_buffer.rb', line 49

def in_template?
  @options[:buffer].buffers.each do |b|
    return true if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
  end
  false
end

#paramsObject



37
38
39
# File 'lib/wikicloth/wiki_buffer.rb', line 37

def params
  @params ||= [ "" ]
end

#run_globals?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/wikicloth/wiki_buffer.rb', line 21

def run_globals?
  true
end

#skip_html?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/wikicloth/wiki_buffer.rb', line 29

def skip_html?
  false
end

#to_sObject



60
61
62
# File 'lib/wikicloth/wiki_buffer.rb', line 60

def to_s
  "<p>" + self.params.join("\n") + "</p>"
end