lib/caramelize/content_transferer.rb in caramelize-0.3.0 vs lib/caramelize/content_transferer.rb in caramelize-0.4.0
- old
+ new
@@ -6,150 +6,117 @@
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'
+ require 'caramelize/output_wiki/gollum'
+ require 'caramelize/input_wiki/redmine_wiki'
+ require 'caramelize/input_wiki/wikkawiki'
- # Controller for the content migration
class ContentTransferer
+ attr_reader :input_wiki, :options, :filter_processor
- class << self
+ def initialize(input_wiki, options)
+ @input_wiki = input_wiki
+ @options = options
- # Execute the content migration
- def execute(original_wiki, options={})
- @options = options
- @original_wiki = original_wiki
+ options[:default_author] = options.fetch(:default_author, "Caramelize")
+ options[:target_directory] = './wiki-export'
+ options[:markup] = target_markup
+ end
- options[:default_author] = "Caramelize" unless options[:default_author]
- # see if original wiki markup is among any gollum supported markups
- options[:markup] = target_markup
+ def execute
+ input_wiki.read_authors
- original_wiki.read_authors
- initialize_page_filters
- commit_history
+ 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]
+ if verbose?
+ puts "From markup: #{input_wiki.markup.to_s}"
+ puts "To markup: #{target_markup.to_s}"
+ puts "Convert latest revisions:"
end
- private
+ migrate_markup_of_latest_revisions
- def target_markup
- output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown
- end
+ puts 'Create Namespace Overview' if verbose?
+ create_overview_page_of_namespaces if options[:create_namespace_overview]
+ end
- def revisions
- @revisions ||= original_wiki.read_pages
- end
+ private
- def filters
- @filters ||= []
- end
+ def target_markup
+ @target_markup ||=
+ needs_conversion_to_target_markup? ? input_wiki.markup : :markdown
+ end
- def initialize_page_filters
- filters << original_wiki.filters
- filters.flatten!
- end
+ def needs_conversion_to_target_markup?
+ output_wiki.supported_markup.index(input_wiki.markup)
+ end
- def create_overview_page_of_namespaces
- puts 'Create Namespace Overview' if verbose?
- output_wiki.create_namespace_overview(original_wiki.namespaces)
- end
+ def revisions
+ @revisions ||= input_wiki.read_pages
+ end
- def migrate_markup_on_last_revision
- if original_wiki.convert_markup? markup # is wiki in target markup
+ def output_wiki
+ @output_wiki ||= OutputWiki::Gollum.new(options[:target_directory])
+ end
+ def filter_processor
+ @filter_processor ||= FilterProcessor.new(input_wiki)
+ end
- end # end convert_markup?
+ def verbose?
+ options[:verbose]
+ end
- 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
+ def revisions_count
+ revisions.count
+ end
- 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
+ def create_overview_page_of_namespaces
+ output_wiki.commit_namespace_overview(input_wiki.namespaces)
+ end
- end
+ def migrate_markup_of_latest_revisions
+ progress_bar = ProgressBar.create(title: "Markup filters",
+ total: revisions_count)
- def original_wiki
- @original_wiki
+ input_wiki.latest_revisions.each do |revision|
+ migrate_markup_of_revision(revision, progress_bar)
end
+ end
- def verbose?
- options[:verbosity] == :verbose
- end
+ def commit_history
+ puts revisions_count
+ progress_bar = ProgressBar.create(title: "Revisions",
+ total: revisions_count)
- 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
+ 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
- @markup
end
+ end
- def options
- @options
+ def migrate_markup_of_revision(revision, progress_bar)
+ if verbose?
+ puts "Filter source: #{revision.title} #{revision.time}"
+ else
+ progress_bar.increment
end
- def output_wiki
- @output_wiki ||= GollumOutput.new('./wiki-export') # TODO make wiki_path an option
- end
+ body_new = filter_processor.run(revision.body)
- def migrate_markup_per_revision(revision)
- if verbose?
- puts "Filter source: #{revision.title} #{revision.time}"
- else
- @progress_bar.increment
- end
+ unless body_new == revision.body
+ revision.body = body_new
+ revision.author_name = target_markup
+ revision.time = Time.now
+ revision.author = nil
- # 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 revision, options[:markup]
- end
-
+ # commit as latest page revision
+ output_wiki.commit_revision(revision, options[:markup])
end
-
- def run_filters(body)
- body_new = body
- filters.each do |filter|
- body_new = filter.run body_new
- end
- body_new
- end
-
- 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
+end