Sha256: 944c6092dd89601289b23ba6902732666d1032b655f447495d82e4e14b43d1e7

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

require 'nokogiri'
require 'rwikibot'

class RwikibotPageDrone
  attr_accessor :wiki
  attr_accessor :page
  attr_accessor :content
  attr_accessor :original_content

  def initialize(wiki, title)
    self.wiki = wiki
    self.page = self.wiki.page(title)
    self.content = self.get_content
    self.original_content = self.content.clone
  end

  def save(verbose=false)
    if self.modified?
      self.original_content = self.content.clone
      puts "Saved: #{self.page.title}" if verbose
      return self.page.save(self.content)
    else
      return false
    end
  end

  def append(*strings)
    strings = strings.flatten
    strings.each do |string|
      unless self.content.include?(string)
        self.content << "#{string}\n"
      end
    end
  end

  def replace(pattern, string)
    if self.content.match(pattern)
      return self.content.gsub!(pattern, string)
    else
      return self.append(string)
    end
  end

  def replace_span(identifier, string)
    doc = Nokogiri::HTML(self.content)
    element = doc.css("span##{identifier}")
    if element.size == 0
      self.append(%{<span id="#{identifier}">#{string}</span>})
    else
      element.to_html = string
      self.content = doc.to_s
    end
  end

  def modified?
    return content != original_content
  end

  def get_content
    if self.page.exists?
      if self.page.content.kind_of?(Hash) and value = self.page.content['content']
        return value
      else
        return self.page.content
      end
    else
      return ''
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
open_conference_ware-1.0.0.pre4 lib/rwikibot_page_drone.rb
open_conference_ware-1.0.0.pre3 lib/rwikibot_page_drone.rb
open_conference_ware-1.0.0.pre2 lib/rwikibot_page_drone.rb
open_conference_ware-1.0.0.pre1 lib/rwikibot_page_drone.rb