Class: WikiCloth::WikiBuffer

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

Direct Known Subclasses

HTMLElement, Link, Table, Template, Var

Defined Under Namespace

Classes: HTMLElement, Link, Table, Template, 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
# File 'lib/wikicloth/wiki_buffer.rb', line 4

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

Instance Method Details

#add_char(c) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wikicloth/wiki_buffer.rb', line 92

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 { |c| self.add_char(c) }
      end
    end
  end
end

#buffer_typeObject



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

def buffer_type
  @buffer_type
end

#check_globalsObject



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
81
82
83
84
85
86
87
88
89
90
# File 'lib/wikicloth/wiki_buffer.rb', line 38

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 == '{'
      @buffers[-1].data.chop!
      @buffers << WikiBuffer::Var.new("",@options)
      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



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

def data
  @data ||= ""
end

#paramsObject



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

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

#run_globals?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/wikicloth/wiki_buffer.rb', line 14

def run_globals?
  true
end

#skip_html?Boolean

Returns:

  • (Boolean)


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

def skip_html?
  false
end

#to_sObject



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

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