lib/caramelize/content_transferer.rb in caramelize-0.2.0 vs lib/caramelize/content_transferer.rb in caramelize-0.3.0

- old
+ new

@@ -1,97 +1,155 @@ -#Encoding: UTF-8 - require 'gollum-lib' require 'grit' require 'ruby-progressbar' module Caramelize - autoload :Wiki, 'caramelize/wiki/wiki' - autoload :WikkaWiki, 'caramelize/wiki/wikkawiki' - autoload :RedmineWiki, 'caramelize/wiki/redmine_wiki' - autoload :GollumOutput, 'caramelize/gollum_output' - autoload :Author, 'caramelize/author' - autoload :Page, 'caramelize/page' - + + require 'caramelize/page' + require 'caramelize/ext' + require 'caramelize/content_transferer' + require 'caramelize/database_connector' + require 'caramelize/gollum_output' + require 'caramelize/wiki/redmine_wiki' + require 'caramelize/wiki/wikkawiki' + require 'caramelize/wiki/wiki' + # Controller for the content migration class ContentTransferer - - # Execute the content migration - def self.execute(original_wiki, options={}) - options[:default_author] = "Caramelize" if !options[:default_author] - # read page revisions from wiki - # store page revisions - original_wiki.read_authors - @revisions = original_wiki.read_pages - # initiate new wiki - output_wiki = GollumOutput.new('wiki.git') # TODO make wiki_path an option - - # see if original wiki markup is among any gollum supported markups - options[:markup] = output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown + class << self - # setup progressbar - progress_revisions = ProgressBar.create(:title => "Revisions", :total => @revisions.count, :format => '%a %B %p%% %t') + # Execute the content migration + def execute(original_wiki, options={}) + @options = options + @original_wiki = original_wiki - # TODO ask if we should replace existing paths + options[:default_author] = "Caramelize" unless options[:default_author] + # see if original wiki markup is among any gollum supported markups + options[:markup] = target_markup - # commit page revisions to new wiki - output_wiki.commit_history(@revisions, options) do |page, index| - if options[:verbosity] == :verbose - puts "(#{index+1}/#{@revisions.count}) #{page.time} #{page.title}" - else - progress_revisions.increment + original_wiki.read_authors + initialize_page_filters + commit_history + + # if wiki needs to convert syntax, do so + if verbose? + puts "From markup: #{original_wiki.markup.to_s}" + puts "To markup: #{markup.to_s}" end + + puts "Latest revisions:" if verbose? + + migrate_markup_on_last_revision + create_overview_page_of_namespaces if options[:create_namespace_overview] end + private - # TODO reorder interwiki links: https://github.com/gollum/gollum/wiki#bracket-tags + def target_markup + output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown + end - # init list of filters to perform on the latest wiki pages - filters = [] + def revisions + @revisions ||= original_wiki.read_pages + end - original_wiki.filters.each do |filter| - filters << filter + def filters + @filters ||= [] end - # if wiki needs to convert syntax, do so - puts "From markup: " + original_wiki.markup.to_s if options[:verbosity] == :verbose - puts "To markup: " + options[:markup].to_s if options[:verbosity] == :verbose - if original_wiki.convert_markup? options[:markup] # is wiki in target markup + def initialize_page_filters + filters << original_wiki.filters + filters.flatten! + end + def create_overview_page_of_namespaces + puts 'Create Namespace Overview' if verbose? + output_wiki.create_namespace_overview(original_wiki.namespaces) + end - end # end convert_markup? + def migrate_markup_on_last_revision + if original_wiki.convert_markup? markup # is wiki in target markup - puts "Latest revisions:" if options[:verbosity] == :verbose - #setup progress for markup conversion - progress_markup = ProgressBar.create(:title => "Markup filters", :total => original_wiki.latest_revisions.count, :format => '%a %B %p%% %t') + end # end convert_markup? - # take each latest revision - for rev in original_wiki.latest_revisions - puts "Filter source: #{rev.title} #{rev.time}" if options[:verbosity] == :verbose - progress_markup.increment - - # run filters - body_new = rev.body - filters.each do |filter| - body_new = filter.run body_new + create_progress_bar("Markup filters", original_wiki.latest_revisions.count) unless verbose? + original_wiki.latest_revisions.each do |revision| + migrate_markup_per_revision(revision) end + end - unless body_new.eql? rev.body - rev.body = body_new - rev.author_name = options[:markup] - rev.time = Time.now - rev.author = nil - + def commit_history + # setup progressbar + create_progress_bar("Revisions", revisions.count) unless verbose? + output_wiki.commit_history(revisions, options) do |page, index| + if verbose? + puts "(#{index+1}/#{revisions.count}) #{page.time} #{page.title}" + else + @progress_bar.increment + end + end + + end + + def original_wiki + @original_wiki + end + + def verbose? + options[:verbosity] == :verbose + end + + def markup + unless @markup + # see if original wiki markup is among any gollum supported markups + @markup = output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown + end + @markup + end + + def options + @options + end + + def output_wiki + @output_wiki ||= GollumOutput.new('./wiki-export') # TODO make wiki_path an option + end + + def migrate_markup_per_revision(revision) + if verbose? + puts "Filter source: #{revision.title} #{revision.time}" + else + @progress_bar.increment + end + + # run filters + body_new = run_filters(revision.body) + + unless body_new == revision.body + revision.body = body_new + revision.author_name = markup + revision.time = Time.now + revision.author = nil + # commit as latest page revision - output_wiki.commit_revision rev, options[:markup] + output_wiki.commit_revision revision, options[:markup] end + end - - if options[:create_namespace_home] - output_wiki.create_namespace_home(original_wiki.namespaces) + def run_filters(body) + body_new = body + filters.each do |filter| + body_new = filter.run body_new + end + body_new end - end # end execute + + def create_progress_bar(title, total) + @progress_bar = ProgressBar.create(title: title, total: total, format: '%a %B %p%% %t') + end + end + end end \ No newline at end of file