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

Constant Summary

BOLD_ITALIC_MAP =
{
  0 => {
    :bold        => [10, "<b>"],
    :italic      => [20, "<i>"],
    :bold_italic => [40, "<i><b>"],
    :four        => [10, "'<b>"],
    :finish      => [0, ""]
  },
  10 => {
    :bold        => [0, "</b>"],
    :italic      => [30, "<i>"],
    :bold_italic => [20, "</b><i>"],
    :four        => [0, "'</b>"],
    :finish      => [0, "</b>"]
  },
  20 => {
    :bold        => [40, "<b>"],
    :italic      => [0, "</i>"],
    :bold_italic => [10, "</i><b>"],
    :four        => [40, "'<b>"],
    :finish      => [0, "</i>"]
  },
  30 => {
    :bold        => [20, "</i></b><i>"],
    :italic      => [10, "</i>"],
    :bold_italic => [0, "</i></b>"],
    :four        => [20, "'</i></b><i>"],
    :finish      => [0, "</i></b>"]
  },
  40 => {
    :bold        => [20, "</b>"],
    :italic      => [10, "</b></i><b>"],
    :bold_italic => [0, "</b></i>"],
    :four        => [20, "'</b>"],
    :finish      => [0, "</b></i>"]
    },
}

Instance Method Summary (collapse)

Constructor Details

- (WikiBuffer) initialize(data = "", options = {})

Returns a new instance of WikiBuffer



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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
  @previous_line_empty = false
  @paragraph_open = false
end

Instance Method Details

- (Object) add_char(c)



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/wikicloth/wiki_buffer.rb', line 151

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.send("to_#{@options[:output]}")
      # any data left in the buffer we feed into the parent
      unless tmp.data.nil?
        tmp.data.each_char { |x| self.add_char(x) }
      end
    end
  end
end

- (Object) add_word(w)



128
129
130
131
132
# File 'lib/wikicloth/wiki_buffer.rb', line 128

def add_word(w)
  self.previous_char = w[-2,1]
  self.current_char = w[-1,1]
  @buffers[-1].data += w
end

- (Object) buffer_type



62
63
64
# File 'lib/wikicloth/wiki_buffer.rb', line 62

def buffer_type
  @buffer_type
end

- (Object) buffers



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

def buffers
  @buffers
end

- (Object) check_globals



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
121
122
123
124
125
126
# File 'lib/wikicloth/wiki_buffer.rb', line 70

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

  if previous_char == "\n" || previous_char == ""
    if @indent == @buffers[-1].object_id && current_char != " "
      @indent = nil
      # 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) }
      return true
    end
    if current_char == " " && @indent.nil? && ![WikiBuffer::HTMLElement,WikiBuffer::Var].include?(@buffers[-1].class)
      "\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! if @buffers[-1].data[-1,1] == '{'
        @buffers << WikiBuffer::Var.new("",@options)
      end
      return true

    # start link
    when current_char == '[' && previous_char != '[' && !@buffers[-1].skip_links?
      @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

- (Object) data



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

def data
  @data ||= ""
end

- (Object) debug



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

def debug
  self.params[0].blank? ? self.class.to_s : self.params[0]
end

- (Object) eof



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/wikicloth/wiki_buffer.rb', line 134

def eof()
  return if @buffers.size == 1

  if self.class == WikiBuffer
    while @buffers.size > 1
      @buffers[-1].eof()
      tmp = @buffers.pop
      @buffers[-1].data += tmp.send("to_#{@options[:output]}")
      unless tmp.data.blank?
        tmp.data.each_char { |x| self.add_char(x) }
      end
    end
  else
    # default cleanup tasks
  end
end

- (Object) get_param(name, default = nil)



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

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

- (Boolean) in_template?

Returns:

  • (Boolean)


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

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

- (Object) params



43
44
45
# File 'lib/wikicloth/wiki_buffer.rb', line 43

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

- (Boolean) run_globals?

Returns:

  • (Boolean)


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

def run_globals?
  true
end

- (Boolean) skip_html?

Returns:

  • (Boolean)


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

def skip_html?
  false
end

- (Boolean) skip_links?

Returns:

  • (Boolean)


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

def skip_links?
  false
end

- (Object) to_html



66
67
68
# File 'lib/wikicloth/wiki_buffer.rb', line 66

def to_html
  self.params.join("\n") + (@list_data.empty? ? "" : render_list_data()) + (@paragraph_open ? "</p>" : "")
end