class WikiCloth::WikiCloth

Public Class Methods

new(opt={}) click to toggle source
# File lib/wikicloth.rb, line 24
def initialize(opt={})
  self.options.merge!(opt)
  self.options[:extensions] ||= []
  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

Public Instance Methods

load(data,p={}) click to toggle source
# File lib/wikicloth.rb, line 33
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
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/wikicloth.rb, line 124
def method_missing(method, *args)
  if self.link_handler.respond_to?(method)
    self.link_handler.send(method, *args)
  else
    super(method, *args)
  end
end
options() click to toggle source
# File lib/wikicloth.rb, line 120
def options
  @options ||= {}
end
params() click to toggle source
# File lib/wikicloth.rb, line 116
def params
  @page_params ||= {}
end
render(opt={}) click to toggle source
# File lib/wikicloth.rb, line 62
def render(opt={})
  self.options = { :noedit => false, :fast => true, :output => :html, :link_handler => self.link_handler, 
    :params => self.params, :sections => self.sections }.merge(self.options).merge(opt)
  self.options[:link_handler].params = options[:params]

  locale = self.options[:locale] || I18n.locale

  I18n.with_locale(locale) do
    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 I18n.t("unknown error on line", :line => @current_line, :row => @current_row, :tree => debug_tree)
      raise err
    end

    buffer.eof()
    buffer.send("to_#{self.options[:output]}")
  end
end
sections() click to toggle source
# File lib/wikicloth.rb, line 104
def sections
  @sections ||= [Section.new]
end
to_html(opt={}) click to toggle source
# File lib/wikicloth.rb, line 108
def to_html(opt={})
  self.render(opt.merge(:output => :html))
end

Protected Instance Methods

add_current_char(buffer,c) click to toggle source
# File lib/wikicloth.rb, line 133
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
get_id_for(val) click to toggle source
# File lib/wikicloth.rb, line 147
def get_id_for(val)
  val.gsub!(/[^A-Za-z0-9_]+/,'')
  @idmap ||= {}
  @idmap[val] ||= 0
  @idmap[val] += 1
  @idmap[val] == 1 ? val : "#{val}-#{@idmap[val]}"
end
options=(val) click to toggle source
# File lib/wikicloth.rb, line 155
def options=(val)
  @options = val
end
params=(val) click to toggle source
# File lib/wikicloth.rb, line 159
def params=(val)
  @page_params = val
end
sections=(val) click to toggle source
# File lib/wikicloth.rb, line 143
def sections=(val)
  @sections = val
end