caramelize 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -53
  3. data/Gemfile +3 -3
  4. data/LICENSE.md +1 -1
  5. data/README.md +25 -25
  6. data/Rakefile +10 -1
  7. data/bin/caramelize +2 -9
  8. data/caramelize.gemspec +27 -23
  9. data/lib/caramelize/caramel.rb +22 -24
  10. data/lib/caramelize/cli.rb +33 -26
  11. data/lib/caramelize/cli/create_command.rb +4 -20
  12. data/lib/caramelize/content_transferer.rb +126 -68
  13. data/lib/caramelize/database_connector.rb +3 -6
  14. data/lib/caramelize/filters/remove_table_tab_line_endings.rb +11 -0
  15. data/lib/caramelize/filters/swap_wiki_links.rb +11 -9
  16. data/lib/caramelize/filters/wikka_to_markdown.rb +8 -8
  17. data/lib/caramelize/gollum_output.rb +50 -37
  18. data/lib/caramelize/page.rb +23 -10
  19. data/lib/caramelize/version.rb +1 -1
  20. data/lib/caramelize/wiki/redmine_wiki.rb +25 -32
  21. data/lib/caramelize/wiki/wiki.rb +28 -15
  22. data/lib/caramelize/wiki/wikkawiki.rb +16 -24
  23. data/spec/fixtures/markup/swap-links-input.textile +57 -0
  24. data/spec/fixtures/markup/swap-links-output.textile +57 -0
  25. data/spec/fixtures/markup/table-tab-line-endings-input.textile +145 -0
  26. data/spec/fixtures/markup/table-tab-line-endings-output.textile +145 -0
  27. data/spec/lib/caramelize/filters/remove_table_tab_line_endings_spec.rb +35 -0
  28. data/spec/lib/caramelize/filters/swap_wiki_links_spec.rb +33 -0
  29. data/spec/lib/caramelize/filters/wikka_to_markdown_spec.rb +88 -0
  30. data/spec/lib/caramelize/gollum_output_spec.rb +64 -0
  31. data/spec/lib/caramelize/wiki/wiki_spec.rb +31 -0
  32. data/spec/spec_helper.rb +8 -0
  33. metadata +136 -33
  34. data/lib/caramelize/author.rb +0 -8
  35. data/test/helper.rb +0 -18
  36. data/test/test_caramelize.rb +0 -7
@@ -1,5 +1,5 @@
1
- #Encoding: UTF-8
2
1
  require 'caramelize/cli'
2
+ require 'fileutils'
3
3
 
4
4
  module Caramelize::CLI
5
5
 
@@ -25,28 +25,12 @@ module Caramelize::CLI
25
25
 
26
26
  # Create a caramelize config file.
27
27
  def execute(args)
28
- begin
29
-
30
- # create dummy config file
31
- target_file = @config_file.nil? ? "caramel.rb" : @config_file
32
-
33
-
34
- #puts args[0]
35
- require 'fileutils'
36
-
37
- FileUtils.cp(File.dirname(__FILE__) +"/../caramel.rb", target_file)
38
-
39
- rescue
40
- #require 'fileutils'
41
- #FileUtils.rm_rf(args[0])
42
- raise
43
- end
28
+ # create dummy config file
29
+ target_file = @config_file.nil? ? "caramel.rb" : @config_file
30
+ FileUtils.cp(File.dirname(__FILE__) +"/../caramel.rb", target_file)
44
31
  if commandparser.verbosity == :normal
45
32
  puts "Created new configuration file: #{target_file}"
46
- #puts paths.sort.join("\n")
47
33
  end
48
34
  end
49
-
50
35
  end
51
-
52
36
  end
@@ -1,97 +1,155 @@
1
- #Encoding: UTF-8
2
-
3
1
  require 'gollum-lib'
4
2
  require 'grit'
5
3
  require 'ruby-progressbar'
6
4
 
7
5
  module Caramelize
8
- autoload :Wiki, 'caramelize/wiki/wiki'
9
- autoload :WikkaWiki, 'caramelize/wiki/wikkawiki'
10
- autoload :RedmineWiki, 'caramelize/wiki/redmine_wiki'
11
- autoload :GollumOutput, 'caramelize/gollum_output'
12
- autoload :Author, 'caramelize/author'
13
- autoload :Page, 'caramelize/page'
14
-
6
+
7
+ require 'caramelize/page'
8
+ require 'caramelize/ext'
9
+ require 'caramelize/content_transferer'
10
+ require 'caramelize/database_connector'
11
+ require 'caramelize/gollum_output'
12
+ require 'caramelize/wiki/redmine_wiki'
13
+ require 'caramelize/wiki/wikkawiki'
14
+ require 'caramelize/wiki/wiki'
15
+
15
16
  # Controller for the content migration
16
17
  class ContentTransferer
17
-
18
- # Execute the content migration
19
- def self.execute(original_wiki, options={})
20
- options[:default_author] = "Caramelize" if !options[:default_author]
21
-
22
- # read page revisions from wiki
23
- # store page revisions
24
- original_wiki.read_authors
25
- @revisions = original_wiki.read_pages
26
- # initiate new wiki
27
- output_wiki = GollumOutput.new('wiki.git') # TODO make wiki_path an option
28
-
29
- # see if original wiki markup is among any gollum supported markups
30
- options[:markup] = output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown
31
-
32
- # setup progressbar
33
- progress_revisions = ProgressBar.create(:title => "Revisions", :total => @revisions.count, :format => '%a %B %p%% %t')
34
-
35
- # TODO ask if we should replace existing paths
36
-
37
- # commit page revisions to new wiki
38
- output_wiki.commit_history(@revisions, options) do |page, index|
39
- if options[:verbosity] == :verbose
40
- puts "(#{index+1}/#{@revisions.count}) #{page.time} #{page.title}"
41
- else
42
- progress_revisions.increment
18
+
19
+ class << self
20
+
21
+ # Execute the content migration
22
+ def execute(original_wiki, options={})
23
+ @options = options
24
+ @original_wiki = original_wiki
25
+
26
+ options[:default_author] = "Caramelize" unless options[:default_author]
27
+ # see if original wiki markup is among any gollum supported markups
28
+ options[:markup] = target_markup
29
+
30
+ original_wiki.read_authors
31
+ initialize_page_filters
32
+ commit_history
33
+
34
+ # if wiki needs to convert syntax, do so
35
+ if verbose?
36
+ puts "From markup: #{original_wiki.markup.to_s}"
37
+ puts "To markup: #{markup.to_s}"
43
38
  end
39
+
40
+ puts "Latest revisions:" if verbose?
41
+
42
+ migrate_markup_on_last_revision
43
+ create_overview_page_of_namespaces if options[:create_namespace_overview]
44
44
  end
45
45
 
46
+ private
47
+
48
+ def target_markup
49
+ output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown
50
+ end
46
51
 
47
- # TODO reorder interwiki links: https://github.com/gollum/gollum/wiki#bracket-tags
52
+ def revisions
53
+ @revisions ||= original_wiki.read_pages
54
+ end
48
55
 
49
- # init list of filters to perform on the latest wiki pages
50
- filters = []
56
+ def filters
57
+ @filters ||= []
58
+ end
51
59
 
52
- original_wiki.filters.each do |filter|
53
- filters << filter
60
+ def initialize_page_filters
61
+ filters << original_wiki.filters
62
+ filters.flatten!
54
63
  end
55
64
 
56
- # if wiki needs to convert syntax, do so
57
- puts "From markup: " + original_wiki.markup.to_s if options[:verbosity] == :verbose
58
- puts "To markup: " + options[:markup].to_s if options[:verbosity] == :verbose
59
- if original_wiki.convert_markup? options[:markup] # is wiki in target markup
65
+ def create_overview_page_of_namespaces
66
+ puts 'Create Namespace Overview' if verbose?
67
+ output_wiki.create_namespace_overview(original_wiki.namespaces)
68
+ end
60
69
 
70
+ def migrate_markup_on_last_revision
71
+ if original_wiki.convert_markup? markup # is wiki in target markup
61
72
 
62
- end # end convert_markup?
63
73
 
64
- puts "Latest revisions:" if options[:verbosity] == :verbose
74
+ end # end convert_markup?
65
75
 
66
- #setup progress for markup conversion
67
- progress_markup = ProgressBar.create(:title => "Markup filters", :total => original_wiki.latest_revisions.count, :format => '%a %B %p%% %t')
76
+ create_progress_bar("Markup filters", original_wiki.latest_revisions.count) unless verbose?
77
+ original_wiki.latest_revisions.each do |revision|
78
+ migrate_markup_per_revision(revision)
79
+ end
80
+ end
68
81
 
69
- # take each latest revision
70
- for rev in original_wiki.latest_revisions
71
- puts "Filter source: #{rev.title} #{rev.time}" if options[:verbosity] == :verbose
72
- progress_markup.increment
73
-
74
- # run filters
75
- body_new = rev.body
76
- filters.each do |filter|
77
- body_new = filter.run body_new
82
+ def commit_history
83
+ # setup progressbar
84
+ create_progress_bar("Revisions", revisions.count) unless verbose?
85
+ output_wiki.commit_history(revisions, options) do |page, index|
86
+ if verbose?
87
+ puts "(#{index+1}/#{revisions.count}) #{page.time} #{page.title}"
88
+ else
89
+ @progress_bar.increment
90
+ end
78
91
  end
79
92
 
80
- unless body_new.eql? rev.body
81
- rev.body = body_new
82
- rev.author_name = options[:markup]
83
- rev.time = Time.now
84
- rev.author = nil
85
-
93
+ end
94
+
95
+ def original_wiki
96
+ @original_wiki
97
+ end
98
+
99
+ def verbose?
100
+ options[:verbosity] == :verbose
101
+ end
102
+
103
+ def markup
104
+ unless @markup
105
+ # see if original wiki markup is among any gollum supported markups
106
+ @markup = output_wiki.supported_markup.index(original_wiki.markup) ? original_wiki.markup : :markdown
107
+ end
108
+ @markup
109
+ end
110
+
111
+ def options
112
+ @options
113
+ end
114
+
115
+ def output_wiki
116
+ @output_wiki ||= GollumOutput.new('./wiki-export') # TODO make wiki_path an option
117
+ end
118
+
119
+ def migrate_markup_per_revision(revision)
120
+ if verbose?
121
+ puts "Filter source: #{revision.title} #{revision.time}"
122
+ else
123
+ @progress_bar.increment
124
+ end
125
+
126
+ # run filters
127
+ body_new = run_filters(revision.body)
128
+
129
+ unless body_new == revision.body
130
+ revision.body = body_new
131
+ revision.author_name = markup
132
+ revision.time = Time.now
133
+ revision.author = nil
134
+
86
135
  # commit as latest page revision
87
- output_wiki.commit_revision rev, options[:markup]
136
+ output_wiki.commit_revision revision, options[:markup]
88
137
  end
138
+
89
139
  end
90
-
91
140
 
92
- if options[:create_namespace_home]
93
- output_wiki.create_namespace_home(original_wiki.namespaces)
141
+ def run_filters(body)
142
+ body_new = body
143
+ filters.each do |filter|
144
+ body_new = filter.run body_new
145
+ end
146
+ body_new
94
147
  end
95
- end # end execute
148
+
149
+ def create_progress_bar(title, total)
150
+ @progress_bar = ProgressBar.create(title: title, total: total, format: '%a %B %p%% %t')
151
+ end
152
+ end
153
+
96
154
  end
97
155
  end
@@ -2,19 +2,16 @@ require 'mysql2'
2
2
 
3
3
  module Caramelize
4
4
  module DatabaseConnector
5
-
5
+
6
6
  def database
7
- socket = ["/tmp/mysqld.sock",
8
- "/tmp/mysql.sock",
7
+ socket = ["/tmp/mysqld.sock",
8
+ "/tmp/mysql.sock",
9
9
  "/var/run/mysqld/mysqld.sock",
10
10
  "/opt/local/var/run/mysql5/mysqld.sock",
11
11
  "/var/lib/mysql/mysql.sock"].detect{|socket| File.exist?(socket) }
12
12
  @options[:socket] = socket
13
13
  @client = Mysql2::Client.new(@options) unless @client
14
14
  @client
15
-
16
15
  end
17
-
18
16
  end
19
-
20
17
  end
@@ -0,0 +1,11 @@
1
+ module Caramelize
2
+ class RemoveTableTabLineEndings
3
+
4
+ # take an input stream and convert all wikka syntax to markdown syntax
5
+ def run body
6
+ migrated_body = body.dup
7
+ migrated_body.gsub!(/\|[\t ]*\r?[\n]/, "|\n")
8
+ migrated_body
9
+ end
10
+ end
11
+ end
@@ -1,20 +1,22 @@
1
- #Encoding: UTF-8
2
1
  module Caramelize
3
2
  class SwapWikiLinks
4
3
 
5
4
  # take an input stream and convert all wikka syntax to markdown syntax
6
5
  def run body
7
- body = body.dup
6
+ migrated_body = body.dup
8
7
 
9
- body.gsub!(/\[\[(\S+)\|(.+?)\]\]/, '[[\2|\1]]')
10
- body.gsub!(/\[\[([\w\s-]*)\]\]/) do |s|
11
- if $1
12
- t = $1.dup
13
- s = '[[' +t + "|"+ $1.gsub(' ', '_') + "]]"
14
- end
8
+ migrated_body.gsub!(/\[\[(\S+)\|(.+?)\]\]/, '[[\2|\1]]')
9
+ migrated_body.gsub!(/\[\[([\w\s\.]*)\]\]/) do |s|
10
+ if $1
11
+ s = $1
12
+ t = $1.dup
13
+ t.gsub!(' ', '_')
14
+ t.gsub!(/\./, '')
15
+ s = "[[#{s}|#{t}]]"
16
+ end
15
17
  end
16
18
 
17
- body
19
+ migrated_body
18
20
  end
19
21
  end
20
22
  end
@@ -1,7 +1,6 @@
1
- #Encoding: UTF-8
2
1
  module Caramelize
3
2
  class Wikka2Markdown
4
-
3
+
5
4
  # take an input stream and convert all wikka syntax to markdown syntax
6
5
  def run body
7
6
  body = body.dup
@@ -9,17 +8,18 @@ module Caramelize
9
8
  body.gsub!(/(=====)(.*?)(=====)/) {|s| '## ' + $2 } #h2
10
9
  body.gsub!(/(====)(.*?)(====)/) {|s| '### ' + $2 } #h3
11
10
  body.gsub!(/(===)(.*?)(===)/) {|s| '#### ' + $2 } #h4
12
-
11
+
13
12
  body.gsub!(/(\*\*)(.*?)(\*\*)/) {|s| '**' + $2 + '**' } #bold
14
13
  body.gsub!(/(\/\/)(.*?)(\/\/)/) {|s| '_' + $2 + '_' } #italic
15
14
  #str.gsub!(/(===)(.*?)(===)/) {|s| '`' + $2 + '`'} #code
16
15
  body.gsub!(/(__)(.*?)(__)/) {|s| '<u>' + $2 + '</u>'} #underline
17
16
  body.gsub!(/(---)/, ' ') #forced linebreak
18
-
17
+
19
18
  #body.gsub!(/(.*?)(\n\t-)(.*?)/) {|s| $1 + $3 } #list
20
-
19
+
21
20
  body.gsub!(/(\t-)(.*)/, '*\2') # unordered list
22
21
  body.gsub!(/(~-)(.*)/, '*\2') # unordered list
22
+ body.gsub!(/( -)(.*)/, '*\2') # unordered list
23
23
  # TODO ordered lists
24
24
 
25
25
  # TODO images: ({{image)(url\=?)?(.*)(}})
@@ -29,10 +29,10 @@ module Caramelize
29
29
 
30
30
  body.gsub!(/(\[\[)(\w+)\s(.+?)(\]\])/, '[[\3|\2]]')
31
31
  #body.gsub!(/\[\[(\w+)\s(.+)\]\]/, ' [[\1 | \2]] ')
32
-
33
-
32
+
33
+
34
34
  # TODO more syntax conversion for links and images
35
-
35
+
36
36
  body
37
37
  end
38
38
  end
@@ -1,49 +1,35 @@
1
- #Encoding: UTF-8
2
-
1
+ require 'caramelize/ext'
3
2
  module Caramelize
4
3
  class GollumOutput
5
-
4
+
6
5
  def supported_markup
7
6
  [:markdown, :textile]
8
7
  end
9
-
8
+
10
9
  # Initialize a new gollum-wiki-repository at the given path.
11
- def initialize wiki_path
10
+ def initialize(new_wiki_path)
12
11
  # TODO use sanitized name as wiki-repository-title
13
- repo = Grit::Repo.init(wiki_path) unless File.exists?(wiki_path)
14
- @gollum = Gollum::Wiki.new(wiki_path)
12
+ @wiki_path = new_wiki_path
13
+ initialize_repository
15
14
  end
16
-
15
+
16
+ def wiki_path
17
+ @wiki_path
18
+ end
19
+
17
20
  # Commit the given page into the gollum-wiki-repository.
18
21
  # Make sure the target markup is correct before calling this method.
19
22
  def commit_revision(page, markup)
20
- message = page.message.empty? ? "Edit in page #{page.title}" : page.message
21
-
22
- if page.author
23
- author = page.author
24
- else
25
- author = Author.new
26
- author.name = page.author_name ? page.author_name : "Caramelize"
27
- author.email = "mail@example.com"
28
- end
29
-
30
- commit = {:message => message,
31
- :name => author.name,
32
- :email => author.email,
33
- :authored_date => page.time,
34
- :committed_date => page.time
35
- }
36
-
37
- gollum_page = @gollum.page(page.title)
23
+ gollum_page = gollum.page(page.title)
38
24
  if gollum_page
39
- @gollum.update_page(gollum_page, gollum_page.name, gollum_page.format, page.body, commit)
25
+ gollum.update_page(gollum_page, gollum_page.name, gollum_page.format, page.body, build_commit(page))
40
26
  else
41
- @gollum.write_page(page.title, markup, page.body, commit)
27
+ gollum.write_page(page.title, markup, page.body, build_commit(page))
42
28
  end
43
29
  end
44
-
30
+
45
31
  # Commit all revisions of the given history into this gollum-wiki-repository.
46
- def commit_history(revisions, options={}, &block)
32
+ def commit_history(revisions, options = {}, &block)
47
33
  options[:markup] = :markdown if !options[:markup] # target markup
48
34
  revisions.each_with_index do |page, index|
49
35
  # call debug output from outside
@@ -52,15 +38,42 @@ module Caramelize
52
38
  end
53
39
  end
54
40
 
55
- def create_namespace_home namespaces, options={}
56
- options[:markup] = :markdown if options[:markup].nil? # target markup
57
- body = "## Overview of namespaces" + "\n" + "\n"
41
+ def create_namespace_overview(namespaces)
42
+ body = "## Overview of namespaces\n\n"
58
43
  namespaces.each do |namespace|
59
- body << "* [[#{namespace[:name]}|#{namespace[:identifier]}/Wiki]] \n" # change wiki as configurable default home
44
+ # TODO change wiki as configurable default home
45
+ # TODO support other markup syntaxes
46
+ body << "* [[#{namespace[:name]}|#{namespace[:identifier]}/Wiki]] \n"
60
47
  end
61
- page = Page.new({:title => "Home", :body => body, :message => 'Create Namespace Home', :latest => true })
62
- commit_revision(page, options[:markup])
48
+ page = Page.new(title: "Home",
49
+ body: body,
50
+ message: 'Create Namespace Overview',
51
+ latest: true)
52
+ commit_revision(page, :markdown)
53
+ page
63
54
  end
64
-
55
+
56
+
57
+ private
58
+
59
+ def gollum
60
+ @gollum ||= Gollum::Wiki.new(wiki_path)
61
+ end
62
+
63
+ def initialize_repository
64
+ # TODO ask if we should replace existing paths
65
+ Grit::Repo.init(wiki_path) unless File.exists?(wiki_path)
66
+ end
67
+
68
+ def build_commit(page)
69
+ message = page.message.empty? ? "Edit in page #{page.title}" : page.message
70
+
71
+ { message: message,
72
+ name: page.author_name,
73
+ email: page.author_email,
74
+ authored_date: page.time,
75
+ committed_date: page.time }
76
+ end
77
+
65
78
  end
66
79
  end