caramelize 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,9 +1 @@
1
- require 'bundler'
2
- require 'rdoc/task'
3
-
4
- Bundler::GemHelper.install_tasks
5
-
6
- Rake::RDocTask.new do |rd|
7
- rd.main = "README.rdoc"
8
- rd.rdoc_files.include( "lib/**/*.rb")
9
- end
1
+ require "bundler/gem_tasks"
@@ -14,11 +14,11 @@ Gem::Specification.new do |s|
14
14
  s.description = %q{By defining the connectors from the input wiki you can migrate any wiki to git-based Gollum wiki repositories.}
15
15
 
16
16
  s.bindir = 'bin'
17
-
17
+
18
18
  s.add_dependency('mysql2')
19
19
  s.add_dependency('cmdparse')
20
20
  s.add_dependency('ruby-progressbar')
21
- s.add_dependency('gollum', '>= 1.3.0') # grit dependency implicit through gollum
21
+ s.add_dependency('gollum-lib', '>= 1.0.0') # grit dependency implicit through gollum
22
22
 
23
23
  s.rubyforge_project = "caramelize"
24
24
 
@@ -1,3 +1,4 @@
1
+ #Encoding: UTF-8
1
2
  require 'caramelize/wiki/wiki'
2
3
  require 'caramelize/wiki/wikkawiki'
3
4
  require 'caramelize/wiki/redmine_wiki'
@@ -37,7 +38,6 @@ def customized_wiki
37
38
  end
38
39
  @titles.uniq!
39
40
  @revisions
40
-
41
41
  end
42
42
  end
43
43
 
@@ -57,6 +57,8 @@ def predefined_wiki
57
57
 
58
58
 
59
59
  # For connection to a Redmine-Database use this Connector
60
+ # Additional options:
61
+ # :create_namespace_home => true/false (Default: true) - Creates a new wikipage at /home as root page for Gollum wiki
60
62
  return Caramelize::RedmineWiki.new(:host => "localhost",
61
63
  :username => "root",
62
64
  :password => "root",
@@ -39,8 +39,9 @@ module Caramelize
39
39
 
40
40
  # Finds the configuration file, if it exists in a known location.
41
41
  def detect_configuration_file(config_path = nil)
42
- possible_files = KNOWN_CONFIG_LOCATIONS
42
+ possible_files = []
43
43
  possible_files << config_path if config_path
44
+ possible_files |= KNOWN_CONFIG_LOCATIONS
44
45
  possible_files.detect{|f| File.exists?(f)}
45
46
  end
46
47
 
@@ -51,13 +52,14 @@ module Caramelize
51
52
  file = detect_configuration_file config_file
52
53
  puts "Read config file: #{file}" if @verbosity == :verbose
53
54
  if file && File.exists?(file)
54
- instance_eval(File.read(file), file || '<eval>')
55
+ instance_eval(File.read(file))
55
56
  original_wiki = input_wiki
56
57
 
57
- ContentTransferer.execute(original_wiki, {:verbosity => @verbosity})
58
+ options = original_wiki.options
59
+ options[:verbosity] = @verbosity
60
+ ContentTransferer.execute(original_wiki, options)
58
61
 
59
62
  time_end = Time.now
60
-
61
63
  puts "Time required: #{time_end - time_start} s" if @verbosity == :verbose
62
64
  else
63
65
  puts "No config file found."
@@ -1,6 +1,6 @@
1
1
  #Encoding: UTF-8
2
2
 
3
- require 'gollum'
3
+ require 'gollum-lib'
4
4
  require 'grit'
5
5
  require 'ruby-progressbar'
6
6
 
@@ -17,8 +17,6 @@ module Caramelize
17
17
 
18
18
  # Execute the content migration
19
19
  def self.execute(original_wiki, options={})
20
-
21
- options[:markup] = :markdown if !options[:markup]
22
20
  options[:default_author] = "Caramelize" if !options[:default_author]
23
21
 
24
22
  # read page revisions from wiki
@@ -28,6 +26,9 @@ module Caramelize
28
26
  # initiate new wiki
29
27
  output_wiki = GollumOutput.new('wiki.git') # TODO make wiki_path an option
30
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
+
31
32
  # setup progressbar
32
33
  progress_revisions = ProgressBar.create(:title => "Revisions", :total => @revisions.count, :format => '%a %B %p%% %t')
33
34
 
@@ -42,38 +43,55 @@ module Caramelize
42
43
  end
43
44
  end
44
45
 
46
+
47
+ # TODO reorder interwiki links: https://github.com/gollum/gollum/wiki#bracket-tags
48
+
49
+ # init list of filters to perform on the latest wiki pages
50
+ filters = []
51
+
52
+ original_wiki.filters.each do |filter|
53
+ filters << filter
54
+ end
55
+
45
56
  # if wiki needs to convert syntax, do so
46
57
  puts "From markup: " + original_wiki.markup.to_s if options[:verbosity] == :verbose
47
58
  puts "To markup: " + options[:markup].to_s if options[:verbosity] == :verbose
48
59
  if original_wiki.convert_markup? options[:markup] # is wiki in target markup
60
+
61
+
62
+ end # end convert_markup?
63
+
64
+ puts "Latest revisions:" if options[:verbosity] == :verbose
65
+
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')
68
+
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
49
73
 
50
- #setup progress for markup conversion
51
- progress_markup = ProgressBar.create(:title => "Markup", :total => original_wiki.latest_revisions.count, :format => '%a %B %p%% %t')
52
-
53
- puts "Latest revisions:" if options[:verbosity] == :verbose
54
- # take each latest revision
55
- for rev in original_wiki.latest_revisions
56
- puts "Updated syntax: #{rev.title} #{rev.time}" if options[:verbosity] == :verbose
57
- progress_markup.increment
74
+ # run filters
75
+ body_new = rev.body
76
+ filters.each do |filter|
77
+ body_new = filter.run body_new
78
+ end
79
+
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
58
85
 
59
- # parse markup & convert to new syntax
60
- if options[:markup] == :markdown
61
- body_new = original_wiki.to_markdown rev.body
62
- else
63
- body_new = original_wiki.to_textile rev.body
64
- end
65
-
66
- unless body_new.eql? rev.body
67
- rev.body = body_new
68
- rev.author_name = options[:markup]
69
- rev.time = Time.now
70
- rev.author = nil
71
-
72
- # commit as latest page revision
73
- output_wiki.commit_revision rev, options[:markup]
74
- end
86
+ # commit as latest page revision
87
+ output_wiki.commit_revision rev, options[:markup]
75
88
  end
76
89
  end
90
+
91
+
92
+ if options[:create_namespace_home]
93
+ output_wiki.create_namespace_home(original_wiki.namespaces)
94
+ end
77
95
  end # end execute
78
96
  end
79
97
  end
@@ -1,4 +1,4 @@
1
- require 'gollum'
1
+ require 'gollum-lib'
2
2
 
3
3
  module Gollum
4
4
  class Committer
@@ -0,0 +1,20 @@
1
+ #Encoding: UTF-8
2
+ module Caramelize
3
+ class SwapWikiLinks
4
+
5
+ # take an input stream and convert all wikka syntax to markdown syntax
6
+ def run body
7
+ body = body.dup
8
+
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
15
+ end
16
+
17
+ body
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ #Encoding: UTF-8
2
+ module Caramelize
3
+ class Trac2Markdown
4
+
5
+ def run body
6
+ body = body.dup
7
+ body.gsub!(/\r/, '')
8
+ body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '@\1@')
9
+ body.gsub!(/\{\{\{\n#!([^\n]+?)(.+?)\}\}\}/m, '<pre><code class="\1">\2</code></pre>')
10
+ body.gsub!(/\{\{\{(.+?)\}\}\}/m, '<pre>\1</pre>')
11
+ # macro
12
+ body.gsub!(/\[\[BR\]\]/, '')
13
+ body.gsub!(/\[\[PageOutline.*\]\]/, '{{toc}}')
14
+ body.gsub!(/\[\[Image\((.+?)\)\]\]/, '!\1!')
15
+ # header
16
+ body.gsub!(/=====\s(.+?)\s=====/, "== #{'\1'} ==\n\n")
17
+ body.gsub!(/====\s(.+?)\s====/, "=== #{'\1'} ===\n\n")
18
+ body.gsub!(/===\s(.+?)\s===/, "==== #{'\1'} ====\n\n")
19
+ body.gsub!(/==\s(.+?)\s==/, "===== #{'\1'} =====\n\n")
20
+ body.gsub!(/=\s(.+?)\s=[\s\n]*/, "====== #{'\1'} ======\n\n")
21
+ # table
22
+ body.gsub!(/\|\|/, "|")
23
+ # link
24
+ body.gsub!(/\[(http[^\s\[\]]+)\s([^\[\]]+)\]/, ' "\2":\1' )
25
+ body.gsub!(/\[([^\s]+)\s(.+)\]/, ' [[\1 | \2]] ')
26
+ body.gsub!(/([^"\/\!])(([A-Z][a-z0-9]+){2,})/, ' \1[[\2]] ')
27
+ body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1')
28
+ # text decoration
29
+ body.gsub!(/'''(.+)'''/, '*\1*')
30
+ body.gsub!(/''(.+)''/, '_\1_')
31
+ body.gsub!(/`(.+)`/, '@\1@')
32
+ # itemize
33
+ body.gsub!(/^\s\s\s\*/, '***')
34
+ body.gsub!(/^\s\s\*/, '**')
35
+ body.gsub!(/^\s\*/, '*')
36
+ body.gsub!(/^\s\s\s\d\./, '###')
37
+ body.gsub!(/^\s\s\d\./, '##')
38
+ body.gsub!(/^\s\d\./, '#')
39
+ body
40
+ end
41
+ end
42
+ end
@@ -1,8 +1,9 @@
1
+ #Encoding: UTF-8
1
2
  module Caramelize
2
- module WikkaConverter
3
+ class Wikka2Markdown
3
4
 
4
5
  # take an input stream and convert all wikka syntax to markdown syntax
5
- def to_markdown body
6
+ def run body
6
7
  body = body.dup
7
8
  body.gsub!(/(======)(.*?)(======)/ ) {|s| '# ' + $2 } #h1
8
9
  body.gsub!(/(=====)(.*?)(=====)/) {|s| '## ' + $2 } #h2
@@ -3,7 +3,7 @@
3
3
  module Caramelize
4
4
  class GollumOutput
5
5
 
6
- def supported_markup
6
+ def supported_markup
7
7
  [:markdown, :textile]
8
8
  end
9
9
 
@@ -16,14 +16,14 @@ module Caramelize
16
16
 
17
17
  # Commit the given page into the gollum-wiki-repository.
18
18
  # Make sure the target markup is correct before calling this method.
19
- def commit_revision(page, markup)
19
+ def commit_revision(page, markup)
20
20
  message = page.message.empty? ? "Edit in page #{page.title}" : page.message
21
21
 
22
22
  if page.author
23
23
  author = page.author
24
24
  else
25
25
  author = Author.new
26
- author.name = page.author_name
26
+ author.name = page.author_name ? page.author_name : "Caramelize"
27
27
  author.email = "mail@example.com"
28
28
  end
29
29
 
@@ -44,13 +44,23 @@ module Caramelize
44
44
 
45
45
  # Commit all revisions of the given history into this gollum-wiki-repository.
46
46
  def commit_history(revisions, options={}, &block)
47
- options[:markup] = :markdown if options[:markup].nil? # target markup
47
+ options[:markup] = :markdown if !options[:markup] # target markup
48
48
  revisions.each_with_index do |page, index|
49
49
  # call debug output from outside
50
50
  block.call(page, index) if block_given?
51
51
  commit_revision(page, options[:markup])
52
52
  end
53
53
  end
54
+
55
+ def create_namespace_home namespaces, options={}
56
+ options[:markup] = :markdown if options[:markup].nil? # target markup
57
+ body = "## Overview of namespaces" + "\n" + "\n"
58
+ namespaces.each do |namespace|
59
+ body << "* [[#{namespace[:name]}|#{namespace[:identifier]}/Wiki]] \n" # change wiki as configurable default home
60
+ end
61
+ page = Page.new({:title => "Home", :body => body, :message => 'Create Namespace Home', :latest => true })
62
+ commit_revision(page, options[:markup])
63
+ end
54
64
 
55
65
  end
56
66
  end
@@ -5,12 +5,12 @@ module Caramelize
5
5
 
6
6
  def initialize page={}
7
7
  @id = page[:id]
8
- @title = page[:title]
9
- @body = page[:body]
8
+ @title = page[:title] ? page[:title] : ""
9
+ @body = page[:body] ? page[:body] : ""
10
10
  @syntax = page[:markup]
11
- @latest = page[:latest]
12
- @time = page[:time]
13
- @message = page[:message]
11
+ @latest = page[:latest] ? page[:latest] : false
12
+ @time = page[:time] ? page[:time] : Time.now
13
+ @message = page[:message] ? page[:message] : ""
14
14
  @author = page[:author]
15
15
  @author_name = page[:author_name]
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Caramelize
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,21 +1,57 @@
1
1
  #Encoding: UTF-8
2
2
  module Caramelize
3
3
  autoload :DatabaseConnector, 'caramelize/database_connector'
4
+ autoload :SwapWikiLinks, 'caramelize/filters/swap_wiki_links'
4
5
 
5
6
  class RedmineWiki < Wiki
6
7
  include DatabaseConnector
7
8
 
9
+ def initialize options={}
10
+ super(options)
11
+ @options[:markup] = :textile
12
+ @options[:create_namespace_home] = true unless @options[:create_namespace_home]
13
+ @options[:swap_interwiki_links] = true
14
+ @options[:filters] << Caramelize::SwapWikiLinks.new
15
+ end
8
16
 
9
17
  # after calling this action, I expect the @titles and @revisions to be filled
10
18
  def read_pages
11
- sql = "SELECT id, title FROM wiki_pages;"
12
19
  @revisions = []
13
20
  @titles = []
14
21
  @latest_revisions = {}
15
- results_pages = database.query(sql)
22
+
23
+ # get all projects
24
+ results_projects = database.query("SELECT id, identifier, name FROM projects;")
25
+ results_projects.each do |row_project|
26
+ #collect all namespaces
27
+ @namespaces << {:identifier => row_project["identifier"], :name => row_project["name"]}
28
+ end
29
+
30
+ # get all wikis
31
+ results_wikis = database.query("SELECT id, project_id FROM wikis;")
32
+
33
+ # get all lemmas
34
+ results_pages = database.query("SELECT id, title, wiki_id FROM wiki_pages;")
16
35
  results_pages.each do |row_page|
17
36
  results_contents = database.query("SELECT * FROM wiki_content_versions WHERE page_id='#{row_page["id"]}' ORDER BY updated_on;")
18
- title = row_page["title"]
37
+
38
+ # get wiki for page
39
+ wiki_row = nil
40
+ project_row = nil
41
+ results_wikis.each do |wiki|
42
+ wiki_row = wiki if wiki["id"] == row_page["wiki_id"]
43
+ end
44
+
45
+ if wiki_row
46
+ # get project from wiki-id
47
+ results_projects.each do |project|
48
+ project_row = project if project["id"] == wiki_row["project_id"]
49
+ end
50
+ end
51
+
52
+ project_identifier = project_row ? project_row["identifier"] + '/' : ""
53
+
54
+ title = project_identifier + row_page["title"]
19
55
  @titles << title
20
56
 
21
57
  results_contents.each do |row_content|
@@ -36,8 +72,7 @@ module Caramelize
36
72
  @titles.uniq!
37
73
  @latest_revisions.each { |rev| rev[1].set_latest }
38
74
  @revisions.sort! { |a,b| a.time <=> b.time }
39
-
40
-
75
+
41
76
  # TODO find latest revision for each limit
42
77
 
43
78
  @revisions
@@ -56,5 +91,5 @@ module Caramelize
56
91
  end
57
92
  @authors
58
93
  end
59
- end
94
+ end
60
95
  end
@@ -1,11 +1,14 @@
1
+ #Encoding: UTF-8
1
2
  module Caramelize
2
3
  autoload :DatabaseConnector, 'caramelize/database_connector'
3
4
  class Wiki
4
5
  include DatabaseConnector
5
- attr_accessor :revisions, :wiki_title, :titles, :description
6
+ attr_accessor :revisions, :wiki_title, :titles, :description, :namespaces, :options
6
7
 
7
8
  def initialize options={}
8
9
  @options = options
10
+ @options[:filters] = []
11
+ @namespaces = []
9
12
  end
10
13
 
11
14
  def revisions_by_title title
@@ -20,10 +23,15 @@ module Caramelize
20
23
  def read_authors
21
24
  return []
22
25
  end
26
+
23
27
 
24
28
  def convert_markup? to_markup
25
29
  markup != to_markup
26
30
  end
31
+
32
+ def filters
33
+ @options[:filters]
34
+ end
27
35
 
28
36
  def latest_revisions
29
37
  @latest_revisions = []
@@ -37,5 +45,6 @@ module Caramelize
37
45
  def markup
38
46
  @options[:markup]
39
47
  end
48
+
40
49
  end
41
50
  end
@@ -1,15 +1,16 @@
1
1
  #Encoding: UTF-8
2
2
  module Caramelize
3
3
  autoload :DatabaseConnector, 'caramelize/database_connector'
4
- autoload :WikkaConverter, 'caramelize/wiki/wikka_converter'
4
+ autoload :Wikka2Markdown, 'caramelize/filters/wikka_to_markdown'
5
5
 
6
6
  class WikkaWiki < Wiki
7
7
  include DatabaseConnector
8
- include WikkaConverter
9
8
 
10
9
  def initialize options={}
11
10
  super(options)
12
- options[:markup] = :wikka
11
+ @options[:markup] = :wikka
12
+ @options[:swap_interwiki_links] = false
13
+ @options[:filters] << Caramelize::Wikka2Markdown.new
13
14
  end
14
15
 
15
16
  # after calling this action, I expect the @titles and @revisions to be filled
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caramelize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-13 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mysql2
@@ -60,13 +60,13 @@ dependencies:
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
- name: gollum
63
+ name: gollum-lib
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
- version: 1.3.0
69
+ version: 1.0.0
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
- version: 1.3.0
77
+ version: 1.0.0
78
78
  description: By defining the connectors from the input wiki you can migrate any wiki
79
79
  to git-based Gollum wiki repositories.
80
80
  email:
@@ -100,13 +100,15 @@ files:
100
100
  - lib/caramelize/content_transferer.rb
101
101
  - lib/caramelize/database_connector.rb
102
102
  - lib/caramelize/ext.rb
103
+ - lib/caramelize/filters/swap_wiki_links.rb
104
+ - lib/caramelize/filters/trac_to_markdown.rb
105
+ - lib/caramelize/filters/wikka_to_markdown.rb
103
106
  - lib/caramelize/gollum_output.rb
104
107
  - lib/caramelize/page.rb
105
108
  - lib/caramelize/version.rb
106
109
  - lib/caramelize/wiki/redmine_wiki.rb
107
110
  - lib/caramelize/wiki/trac_converter.rb
108
111
  - lib/caramelize/wiki/wiki.rb
109
- - lib/caramelize/wiki/wikka_converter.rb
110
112
  - lib/caramelize/wiki/wikkawiki.rb
111
113
  - test/helper.rb
112
114
  - test/test_caramelize.rb