Class: WikiCloth::WikiCloth

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

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ WikiCloth

Returns a new instance of WikiCloth.



16
17
18
19
20
21
# File 'lib/wikicloth.rb', line 16

def initialize(opt={})
  self.options[:link_handler] = opt[:link_handler] unless opt[:link_handler].nil?
  self.load(opt[:data],opt[:params]) unless opt[:data].nil?
  @current_line = 1
  @current_row = 0
end

Instance Method Details

#add_current_char(buffer, c) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/wikicloth.rb', line 94

def add_current_char(buffer,c)
  if c == "\n"
    @current_line += 1
    @current_row = 1
  else
    @current_row += 1
  end
  buffer.add_char(c)
end


107
108
109
# File 'lib/wikicloth.rb', line 107

def link_handler
  self.options[:link_handler] ||= WikiLinkHandler.new
end

#load(data, p = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wikicloth.rb', line 23

def load(data,p={})
  depth = 1
  count = 0
  root = [self.sections]

  # parse wiki document into sections
  data.each_line do |line|
    if line =~ /^([=]{1,6})\s*(.*?)\s*(\1)/
      root << root.last[-1].children if $1.length > depth
      root.pop if $1.length < depth
      depth = $1.length
      root.last << Section.new(line, get_id_for($2.gsub(/\s+/,'_')))
      count += 1
    else
      root.last[-1] << line
    end
  end

  # if we find template variables assume document is
  # a template
  self.sections.first.template = true if data =~ /\{\{\{\s*([A-Za-z0-9]+)\s*\}\}\}/

  # If there are more than four sections enable automatic
  # table of contents
  self.sections.first.auto_toc = true unless count < 4 || data =~ /__(NO|)TOC__/

  self.params = p
end

#paramsObject



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

def params
  @page_params ||= {}
end

#render(opt = {}) ⇒ Object



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

def render(opt={})
  noedit = false
  self.params.merge!({ 'WIKI_VERSION' => ::WikiCloth::VERSION, 'RUBY_VERSION' => RUBY_VERSION })
  self.options = { :fast => true, :output => :html, :link_handler => self.link_handler, :params => self.params, :sections => self.sections }.merge(opt)
  self.options[:link_handler].params = options[:params]
  data = self.sections.collect { |s| s.render(self.options) }.join
  data.gsub!(/<!--(.|\s)*?-->/,"")
  data << "\n" if data.last(1) != "\n"
  data << "garbage"

  buffer = WikiBuffer.new("",options)

  begin
    if self.options[:fast]
      until data.empty?
        case data
        when /\A\w+/
          data = $'
          @current_row += $&.length
          buffer.add_word($&)
        when /\A[^\w]+(\w|)/m
          data = $'
          $&.each_char { |c| add_current_char(buffer,c) }
        end
      end
    else
      data.each_char { |c| add_current_char(buffer,c) }
    end
  rescue => err
    debug_tree = buffer.buffers.collect { |b| b.debug }.join("-->")
    puts "Unknown error on line #{@current_line} row #{@current_row}: #{debug_tree}"
    raise err
  end

  buffer.eof()
  buffer.to_s
end

#sectionsObject



52
53
54
# File 'lib/wikicloth.rb', line 52

def sections
  @sections ||= [Section.new]
end

#to_html(opt = {}) ⇒ Object



103
104
105
# File 'lib/wikicloth.rb', line 103

def to_html(opt={})
  self.render(opt)
end