Sha256: 99dd284a1a7de2878e2fc31f9ca28749471bfdc0dd27caebc916edf2e6d4e9cd

Contents?: true

Size: 1.9 KB

Versions: 33

Compression:

Stored size: 1.9 KB

Contents

require 'fileutils'
require 'cgi'

class WikiEntry
  ENTRIES_DIR = __DIR__('../mkd')
  class << self
    def [](name)
      if File.exist?(ENTRIES_DIR/File.basename(File.expand_path(name)))
        new(name)
      end
    end
    def titles
      Dir[ENTRIES_DIR/'*'].entries
    end
  end

  attr_reader :history, :current, :name

  def initialize(name)
    # avoid tampering with the path
    @name = File.basename(File.expand_path(name))
    update
  end

  def update
    @current = "#{base}/current.mkd"
    @history = Dir["#{base}/*_*.mkd"]
  end

  def save newtext
    FileUtils.mkdir_p(base)

    if content != newtext
      history_name = "#{base}/#{timestamp}.mkd"
      FileUtils.mv(@current, history_name) if exists?
      File.open(@current, "w+"){|fp| fp.print(newtext) }
    end
  end

  def rename to
    FileUtils.mv(base, "mkd/#{to}")
  end

  def delete
    FileUtils.rm_rf(base)
  end

  def revert
    return if not exists? or @history.empty?
    FileUtils.mv(@current, @current + ".bak")
    FileUtils.mv(@history.last, @current) unless @history.empty?
    update
  end

  def unrevert
    bakfile = @current + '.bak'
    return unless File.exists?(bakfile)
    FileUtils.mv(bakfile, @current)
    update
  end

  def exists?
    File.exists?(@current)
  end

  def base
    ENTRIES_DIR/@name
  end

  def content
    CGI.unescapeHTML(File.read(@current)) if exists?
  end

  def timestamp
    Time.now.strftime("%Y-%m-%d_%H-%M-%S")
  end

  def escape_path(path)
    File.basename(File.expand_path(path))
  end
end

class EntryView
  include Ramaze::Helper::Methods
  helper :cgi, :link

  class << self

    def render content
      mkd2html(content || "No Entry")
    end

    def mkd2html text
      html = BlueCloth.new(text).to_html
      html.gsub!(/\[\[(.*?)\]\]/) do |m|
        exists = WikiEntry[$1] ? 'exists' : 'nonexists'
        A(h($1), :href => $1, :class => exists)
      end
      html
    end
  end
end

Version data entries

33 entries across 33 versions & 5 rubygems

Version Path
Pistos-ramaze-2008.12 examples/app/wiktacular/src/model.rb
Pistos-ramaze-2009.01 examples/app/wiktacular/src/model.rb
Pistos-ramaze-2009.02 examples/app/wiktacular/src/model.rb
Pistos-ramaze-2009.04.08 examples/app/wiktacular/src/model.rb
Pistos-ramaze-2009.06.12 examples/app/wiktacular/src/model.rb
manveru-ramaze-2008.12 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.01 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.04.01 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.04.08 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.04.18 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.04.22 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.04 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.05.08 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.05 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.06.04 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.06.12 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.06 examples/app/wiktacular/src/model.rb
manveru-ramaze-2009.07 examples/app/wiktacular/src/model.rb
ptomato-ramaze-2009.02.1 examples/app/wiktacular/src/model.rb
ptomato-ramaze-2009.02 examples/app/wiktacular/src/model.rb