growi-image_converter 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 39307d84e87c1e73cda70ceb019ee7136593b454ce854d20c90dd5f1b4104a4d
4
+ data.tar.gz: fc30ebba1f8b1241b552bd2f8f086f1945828fe6199a4b72179dc88c7f2a6ceb
5
+ SHA512:
6
+ metadata.gz: 7c00185b8dfcf40cda41259494c76d5cd802e5fad4d662d00ba74b657420a35ffb537df299454f968f1010f4ef2edfbfd6021fbebc361862392248540d5b5507
7
+ data.tar.gz: 9786454b470c8f861771f18d7768ed823f910a924272737ae11c466119d62c37487785b6988add54d894e1f36f7bfd93777d181c38b275d9b8ea9cc9731f50a1
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ Style/AsciiComments:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+ Exclude:
7
+ - 'spec/**/*.rb'
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - 'spec/**/*.rb'
12
+
13
+ Metrics/MethodLength:
14
+ Max: 27
15
+
16
+ Metrics/AbcSize:
17
+ Max: 30
18
+
19
+ Naming/FileName:
20
+ Exclude:
21
+ - 'lib/growi-image-converter-esa.rb'
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.4
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in growi-image_converter.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Takayuki TAMURA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Growi::ImageConverter
2
+
3
+ growi-image_converter is a converter that converts esa.io images to GROWI.
4
+
5
+ GROWI has the ability to import data from esa.io. However, images are not converted. Therefore, convert using this growi-image_converter.
6
+
7
+ You need to create an AWS S3 bucket in advance and set it to GROWI.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'growi-image_converter'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install growi-image_converter
24
+
25
+ ## Usage
26
+
27
+ Export these environments.
28
+
29
+ ```
30
+ $ export GROWI_URL=https://*****.growi.cloud
31
+ $ export GROWI_ACCESS_TOKEN=0123456789abcdef0123456789abcdef0123456789ab
32
+ ```
33
+
34
+ Perform a trial run with no changes made:
35
+
36
+ $ growi-image_converter
37
+
38
+ Perform the conversion:
39
+
40
+ $ growi-image_converter -d
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aqutam/growi-image_converter.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
5
+
6
+ require 'optparse'
7
+ require 'growi-client'
8
+ require 'growi/image_converter'
9
+
10
+ def run
11
+ options = {}
12
+ options[:dry_run] = true
13
+
14
+ OptionParser.new do |opt|
15
+ opt.banner = 'Usage: growi-image_converter [options]'
16
+ opt.version = Growi::ImageConverter::VERSION
17
+ opt.on('-d', '--no-dry-run', 'Perform the conversion') do
18
+ options[:dry_run] = false
19
+ end
20
+ end.parse!
21
+
22
+ if ENV['GROWI_URL'].nil?
23
+ puts 'Error: growi_url is required.'
24
+ puts
25
+ puts 'Export this environment:'
26
+ puts '$ export GROWI_URL=https://*****.growi.cloud'
27
+ exit 1
28
+ end
29
+
30
+ if ENV['GROWI_ACCESS_TOKEN'].nil?
31
+ puts 'Error: access_token is required.'
32
+ puts
33
+ puts 'Export this environment:'
34
+ puts '$ export GROWI_ACCESS_TOKEN=0123456789abcdef0123456789abcdef0123456789ab'
35
+ exit 1
36
+ end
37
+
38
+ puts '[DRY RUN MODE]' if options[:dry_run]
39
+
40
+ client = GrowiClient.new growi_url: ENV['GROWI_URL'], access_token: ENV['GROWI_ACCESS_TOKEN']
41
+ image_converter = Growi::ImageConverter::Esa.new client
42
+ image_converter.convert dry_run: options[:dry_run]
43
+ end
44
+
45
+ run
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'growi/image_converter/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'growi-image_converter'
9
+ spec.version = Growi::ImageConverter::VERSION
10
+ spec.authors = ['Takayuki TAMURA']
11
+ spec.email = ['tamtam.okinawa@gmail.com']
12
+
13
+ spec.summary = 'GROWI image converter'
14
+ spec.description = 'growi-image_converter is a converter that converts esa.io images to GROWI.'
15
+ spec.homepage = 'https://github.com/aqutam/growi-image-converter-esa'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'bin'
22
+ spec.executables = ['growi-image_converter']
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.17'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+
29
+ spec.add_dependency 'growi-client'
30
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ # Image Markdownとアタッチされた画像を対応させるクラス
6
+ class AttachedImageFile
7
+ def initialize(markdown_images, attached_file)
8
+ @data = attached_file
9
+ @markdown_images = markdown_images
10
+ end
11
+ attr_accessor :data, :markdown_images
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ # markdown bodyを扱うクラス
6
+ class Body
7
+ REGEX_SPACE = '[ \t]'
8
+ REGEX_URL_PREFIX_ESA = 'https?://img.esa.io/'
9
+ REGEX_SPACE_OR_RETURN = "(?:#{REGEX_SPACE}*?\\n)?#{REGEX_SPACE}*?"
10
+ REGEX_SPACE_GE_1_OR_RETURN = "(?:#{REGEX_SPACE}+?|#{REGEX_SPACE_OR_RETURN})"
11
+ REGEX_TITLE = "(?:#{REGEX_SPACE_GE_1_OR_RETURN}\".*?\")?"
12
+
13
+ FILE_PATH_PREFIX = '/attachment/'
14
+
15
+ def initialize(body)
16
+ @body = body
17
+ @markdown_images = []
18
+ end
19
+ attr_accessor :body, :markdown_images
20
+
21
+ def scan_markdown_image_esa
22
+ matches = []
23
+
24
+ # Image syntax inline
25
+ matches.concat(body.scan(
26
+ /
27
+ !\[#{REGEX_SPACE_OR_RETURN}.*?#{REGEX_SPACE_OR_RETURN}\]
28
+ \(#{REGEX_SPACE_OR_RETURN}
29
+ #{REGEX_URL_PREFIX_ESA}.*?#{REGEX_TITLE}
30
+ #{REGEX_SPACE_OR_RETURN}\)
31
+ /x
32
+ ))
33
+
34
+ # Image syntax reference-style
35
+ matches.concat(body.scan(
36
+ /
37
+ \[#{REGEX_SPACE_OR_RETURN}.*?#{REGEX_SPACE_OR_RETURN}\]:
38
+ #{REGEX_SPACE_OR_RETURN}#{REGEX_URL_PREFIX_ESA}.*
39
+ /x
40
+ ))
41
+
42
+ # Image syntax img tag
43
+ matches.concat(body.scan(/<img#{REGEX_SPACE_GE_1_OR_RETURN}.*?#{REGEX_URL_PREFIX_ESA}.*?>/m))
44
+
45
+ @markdown_images = matches.map { |match| Growi::ImageConverter::MarkdownImage.new match }
46
+ end
47
+
48
+ def group_by_url
49
+ group_by_url = {}
50
+ markdown_images.each do |markdown_image|
51
+ (group_by_url[markdown_image.url] ||= []).push(markdown_image)
52
+ end
53
+ group_by_url
54
+ end
55
+
56
+ def replace_markdown_image(attached_file)
57
+ attached_file_path = FILE_PATH_PREFIX + attached_file.data[:attachment]._id
58
+ attached_file.markdown_images.each do |markdown_image|
59
+ body.sub! markdown_image.syntax, markdown_image.replace_url(attached_file_path)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ # img.esa.io の画像を GROWI にアタッチし直すクラス
6
+ class Esa
7
+ class << self
8
+ def get_image_from_esa(url, tempdir)
9
+ tmp_file = URI.parse(url).open
10
+ image_file = File.open(tempdir + '/' + File.basename(URI.parse(url).path), 'w+b')
11
+ image_file.write(tmp_file.read)
12
+ image_file.rewind
13
+ image_file
14
+ end
15
+ end
16
+
17
+ def initialize(client)
18
+ @client = client
19
+ end
20
+
21
+ def convert(dry_run: true)
22
+ Signal.trap(:INT) { throw :exit_convert }
23
+ catch :exit_convert do
24
+ begin
25
+ page_list = get_page_list
26
+ rescue StandardError => e
27
+ puts 'Error: ' + e.message
28
+ exit 1
29
+ end
30
+
31
+ page_list.each do |page_summary|
32
+ Dir.mktmpdir do |tempdir|
33
+ begin
34
+ page = Growi::ImageConverter::Page.new(page_summary._id, @client, dry_run: dry_run)
35
+ page.body.scan_markdown_image_esa
36
+ page.attach_files(page.body.group_by_url, tempdir)
37
+ page.replace_markdown_image
38
+ page.update
39
+ rescue StandardError => e
40
+ puts 'PageID: ' + page_summary._id + ', Result: Failed to convert' + ', Message: ' + e.message
41
+ next
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def get_page_list(path_exp = '/')
51
+ req = GApiRequestPagesList.new path_exp: path_exp
52
+ api_return = @client.request(req)
53
+
54
+ raise StandardError, 'Failed to get page list.' unless api_return.ok
55
+
56
+ api_return.data
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ # Markdown syntax image
6
+ class MarkdownImage
7
+ def initialize(syntax)
8
+ match = syntax.match(%r{(?<url>https?://.*?)(?:\s+?|"|\)|$)})
9
+
10
+ @syntax = syntax
11
+ @url = match&.[](:url)
12
+ end
13
+ attr_accessor :syntax, :url
14
+
15
+ def replace_url(new_url)
16
+ syntax.sub url, new_url
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ # page を扱うクラス
6
+ class Page
7
+ def initialize(page_id, client, dry_run: true)
8
+ @dry_run = dry_run
9
+ @client = client
10
+ @data = get(page_id)
11
+ @body = Body.new(data.revision.body)
12
+ @attached_files = []
13
+ end
14
+ attr_accessor :data, :body, :attached_files
15
+
16
+ def get(page_id)
17
+ req = GApiRequestPagesGet.new page_id: page_id
18
+ api_return = @client.request(req)
19
+
20
+ raise StandardError, 'Failed to get page data.' unless api_return.ok
21
+
22
+ api_return.data
23
+ end
24
+
25
+ def attach_files(markdown_images_group_by_url, tempdir)
26
+ markdown_images_group_by_url.each do |url, markdown_images|
27
+ begin
28
+ image_file = Growi::ImageConverter::Esa.get_image_from_esa url, tempdir
29
+ attached_file = attach_file image_file
30
+ attached_files.push(Growi::ImageConverter::AttachedImageFile.new(markdown_images, attached_file))
31
+ rescue StandardError => e
32
+ puts 'PageID: ' + data._id + ', Image URL: ' + url + ', Message: ' + e.message
33
+ next
34
+ end
35
+ end
36
+ end
37
+
38
+ def attach_file(file)
39
+ if @dry_run
40
+ attachment_params = { _id: 'dry-run-' + data._id + '-' + SecureRandom.hex(10), creator: 'dry-run-user' }
41
+ return { attachment: GrowiAttachment.new(attachment_params) }
42
+ end
43
+
44
+ req = GApiRequestAttachmentsAdd.new page_id: data._id, file: file
45
+ api_return = @client.request(req)
46
+
47
+ raise StandardError, 'Failed to atach file.' unless api_return.ok
48
+
49
+ api_return.data
50
+ end
51
+
52
+ def replace_markdown_image
53
+ attached_files.each { |attached_file| body.replace_markdown_image(attached_file) }
54
+ end
55
+
56
+ def update
57
+ if attached_files.empty?
58
+ puts 'PageID: ' + data._id + ', Result: Through'
59
+ return
60
+ end
61
+
62
+ if @dry_run
63
+ puts 'PageID: ' + data._id + ', Result: Converted'
64
+ return
65
+ end
66
+
67
+ req = GApiRequestPagesUpdate.new(
68
+ page_id: data._id,
69
+ revision_id: data.revision._id,
70
+ body: body,
71
+ grant: data.grant
72
+ )
73
+ api_return = @client.request(req)
74
+
75
+ raise StandardError, 'Failed to update page.' unless api_return.ok
76
+
77
+ puts 'PageID: ' + data._id + ', Result: Converted'
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Growi
4
+ module ImageConverter
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'growi-client'
4
+ require 'open-uri'
5
+
6
+ require 'growi/image_converter/attached_image_file'
7
+ require 'growi/image_converter/body'
8
+ require 'growi/image_converter/esa'
9
+ require 'growi/image_converter/markdown_image'
10
+ require 'growi/image_converter/page'
11
+ require 'growi/image_converter/version'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: growi-image_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takayuki TAMURA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: growi-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: growi-image_converter is a converter that converts esa.io images to GROWI.
70
+ email:
71
+ - tamtam.okinawa@gmail.com
72
+ executables:
73
+ - growi-image_converter
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/growi-image_converter
86
+ - growi-image_converter.gemspec
87
+ - lib/growi/image_converter.rb
88
+ - lib/growi/image_converter/attached_image_file.rb
89
+ - lib/growi/image_converter/body.rb
90
+ - lib/growi/image_converter/esa.rb
91
+ - lib/growi/image_converter/markdown_image.rb
92
+ - lib/growi/image_converter/page.rb
93
+ - lib/growi/image_converter/version.rb
94
+ homepage: https://github.com/aqutam/growi-image-converter-esa
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: GROWI image converter
117
+ test_files: []