images_to_scrapbox 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/images_to_scrapbox.gemspec +29 -0
- data/lib/cli.rb +60 -0
- data/lib/images_to_scrapbox/version.rb +3 -0
- data/lib/images_to_scrapbox.rb +167 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61745dce879d45720a668651340e24da58db388574ca7dd932c87fc495f5f076
|
4
|
+
data.tar.gz: df9fa7cffd452d56e4b0a2639241ce2f2a6db617ca5ae2ab88780f958bf73064
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d74b63258f355d88e81da2cbfd8ace22f795ad569520013d8b3dee17f7987fc9ecd4926654336a5e6465e8bde6e37ad091ec4495c90fa5c23bae79a7472efa3d
|
7
|
+
data.tar.gz: 8bca05eac5da7525fc7b421ef2562afbc752b4713accfbb7f20565a4a26e1a8a36c4cc187a840cef9b7762f04cc7e6c61e3be5339d5fdb287a58a1d659409687
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "images_to_scrapbox/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "images_to_scrapbox"
|
8
|
+
spec.version = ImagesToScrapbox::VERSION
|
9
|
+
spec.authors = ["Hiroharu Sugawara"]
|
10
|
+
spec.email = ["hsugawa@tmu.ac.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{convert images files to scrapbox-import-ready-json.}
|
13
|
+
spec.description = %q{convert images files to scrapbox-import-ready-json.}
|
14
|
+
spec.homepage = %q{https://github.com/hsugawa8651/images_to_scrapbox_gem}
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_dependency "thor"
|
28
|
+
spec.add_dependency "rest-client"
|
29
|
+
end
|
data/lib/cli.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "images_to_scrapbox"
|
2
|
+
require "thor"
|
3
|
+
|
4
|
+
module ImagesToScrapbox
|
5
|
+
|
6
|
+
class CLI < Thor
|
7
|
+
|
8
|
+
no_commands do
|
9
|
+
def helper_add_paths(*globs)
|
10
|
+
if globs.length > 0
|
11
|
+
globs.each do |glob|
|
12
|
+
ImagesToScrapbox::Converter.add(glob)
|
13
|
+
end
|
14
|
+
else
|
15
|
+
while glob=$stdin.gets
|
16
|
+
ImagesToScrapbox::Converter.add(glob.chomp)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class_option :help, :type => :boolean, :aliases => '-h', :desc => 'help message.'
|
23
|
+
default_task :help
|
24
|
+
|
25
|
+
class_option :unique, :type => :boolean, :aliases => '-u',
|
26
|
+
:default => true, :desc => 'unique files'
|
27
|
+
class_option :sort, :type => :string, :aliases => '-s',
|
28
|
+
:default => "none", :desc => 'sort files'
|
29
|
+
class_option :ascending, :type => :boolean, :aliases => '-a',
|
30
|
+
:default => true, :desc => "sort in ascending order, or descending"
|
31
|
+
|
32
|
+
method_option :image, type: :boolean, aliases: '-i',
|
33
|
+
:default => true, desc: 'register images'
|
34
|
+
method_option :larger, type: :boolean, aliases: '-l',
|
35
|
+
:default => false, desc: 'larger image'
|
36
|
+
method_option :timestamp, :type => :string, :aliases => '-t',
|
37
|
+
:default => "mtime", :desc => "file timestamp: atime, ctime, mtime"
|
38
|
+
method_option :toc, type: :boolean, aliases: '-t',
|
39
|
+
:default => true, desc: 'creates table-of-contents'
|
40
|
+
|
41
|
+
desc 'convert FILES [options]', 'Convert images files to scrapbox-json'
|
42
|
+
def convert(*globs)
|
43
|
+
helper_add_paths(globs)
|
44
|
+
ImagesToScrapbox::Converter.perform(TRUE, options)
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'list FILES [options]', 'List images files to be processed'
|
48
|
+
def list(*globs)
|
49
|
+
helper_add_paths(globs)
|
50
|
+
ImagesToScrapbox::Converter.perform(FALSE, options)
|
51
|
+
end
|
52
|
+
|
53
|
+
desc 'version', 'version'
|
54
|
+
def version
|
55
|
+
p ImagesToScrapbox::VERSION
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require "images_to_scrapbox/version"
|
2
|
+
require "rest-client"
|
3
|
+
# require "open-uri"
|
4
|
+
require "uri"
|
5
|
+
require "JSON"
|
6
|
+
require 'base64'
|
7
|
+
|
8
|
+
def register_image(image_fullpath)
|
9
|
+
gyazo_url="https://upload.gyazo.com/api/upload"
|
10
|
+
begin
|
11
|
+
r = RestClient.post gyazo_url, {
|
12
|
+
"access_token" => ENV["GYAZO_TOKEN"],
|
13
|
+
:imagedata => File.new(image_fullpath, 'rb')
|
14
|
+
}
|
15
|
+
return JSON.parse(r)
|
16
|
+
rescue => e
|
17
|
+
p e
|
18
|
+
puts e.backtrace
|
19
|
+
puts <<EOS
|
20
|
+
Failed to register image data.
|
21
|
+
- Be online.
|
22
|
+
- Set your ACCESS_TOKEN to the environment variable "GYAZO_TOKEN"
|
23
|
+
EOS
|
24
|
+
ensure
|
25
|
+
end
|
26
|
+
exit(1)
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
module ImagesToScrapbox
|
31
|
+
# Your code goes here...
|
32
|
+
class Converter
|
33
|
+
@@converters=[]
|
34
|
+
|
35
|
+
def Converter.add(globn)
|
36
|
+
Dir.glob(globn).each do |g|
|
37
|
+
next unless g =~ /\.(gif|jpe?g|png)$/i
|
38
|
+
@@converters.push Converter.new(g)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def Converter.perform(do_convert=TRUE, options)
|
43
|
+
|
44
|
+
if options[:unique]
|
45
|
+
@@converters.uniq!{ |e| e.image_name }
|
46
|
+
end
|
47
|
+
|
48
|
+
case options[:sort]
|
49
|
+
when "none"
|
50
|
+
when "name"
|
51
|
+
@@converters.sort_by!{ |e| e.image_name }
|
52
|
+
else
|
53
|
+
raise Thor::Error, "Unknown sort type: #{sort_type}"
|
54
|
+
end
|
55
|
+
|
56
|
+
unless options[:ascending]
|
57
|
+
@@converters.reverse!
|
58
|
+
end
|
59
|
+
|
60
|
+
unless do_convert
|
61
|
+
@@converters.each do |c|
|
62
|
+
puts c.image_name
|
63
|
+
end
|
64
|
+
return
|
65
|
+
end
|
66
|
+
|
67
|
+
@@converted_on="Converted on #{Time.now.to_s}"
|
68
|
+
|
69
|
+
pages=[]
|
70
|
+
@@converters.map do |converter|
|
71
|
+
pages.concat converter.start(options)
|
72
|
+
end
|
73
|
+
|
74
|
+
if options[:toc]
|
75
|
+
pages.concat Converter.toc_page
|
76
|
+
end
|
77
|
+
|
78
|
+
result={
|
79
|
+
"pages": pages
|
80
|
+
}
|
81
|
+
|
82
|
+
puts result.to_json
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
attr_reader :image_name, :image_path, :page_title
|
87
|
+
def initialize(path)
|
88
|
+
@image_name=path
|
89
|
+
@image_path=File.expand_path(path)
|
90
|
+
end
|
91
|
+
|
92
|
+
def Converter.toc_page()
|
93
|
+
tocpage=SbPage.new()
|
94
|
+
tocpage.set_page_title(@@converted_on)
|
95
|
+
@@converters.map do |converter|
|
96
|
+
tocpage.push_text(" ["+converter.page_title+"]")
|
97
|
+
end
|
98
|
+
tocpage.get_page
|
99
|
+
end
|
100
|
+
|
101
|
+
def start(options)
|
102
|
+
|
103
|
+
timestamp=""
|
104
|
+
case options[:timestamp]
|
105
|
+
when "atime"
|
106
|
+
timestamp=File.atime(@image_path).to_s
|
107
|
+
when "ctime"
|
108
|
+
timestamp=File.ctime(@image_path).to_s
|
109
|
+
when "mtime"
|
110
|
+
timestamp=File.mtime(@image_path).to_s
|
111
|
+
else
|
112
|
+
raise Thor::Error, "Unknown timestamp type: #{timestamp_kind}"
|
113
|
+
end
|
114
|
+
@page_title=@image_name + " " + timestamp
|
115
|
+
|
116
|
+
imagepage=SbPage.new()
|
117
|
+
imagepage.set_page_title(@page_title)
|
118
|
+
imagepage.push_text(@image_name)
|
119
|
+
imagepage.push_empty_text()
|
120
|
+
|
121
|
+
if options[:image]
|
122
|
+
r=register_image(@image_path)
|
123
|
+
url=r["url"]
|
124
|
+
imagepage.push_text( options[:larger] ? "[["+url+"]]" : "["+url+"]" )
|
125
|
+
end
|
126
|
+
|
127
|
+
imagepage.push_text("["+@@converted_on+"]")
|
128
|
+
imagepage.get_page
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class SbPage
|
133
|
+
def set_page_title(title)
|
134
|
+
@page_title=title
|
135
|
+
push_text(title)
|
136
|
+
push_empty_text
|
137
|
+
end
|
138
|
+
|
139
|
+
def get_page()
|
140
|
+
[
|
141
|
+
{
|
142
|
+
"title": @page_title,
|
143
|
+
"lines": json
|
144
|
+
}
|
145
|
+
]
|
146
|
+
end
|
147
|
+
|
148
|
+
def initialize()
|
149
|
+
@page_title=""
|
150
|
+
@sb_json=[]
|
151
|
+
end
|
152
|
+
|
153
|
+
def json()
|
154
|
+
@sb_json
|
155
|
+
end
|
156
|
+
|
157
|
+
def push_text(s)
|
158
|
+
s.split("\n").each do |s1|
|
159
|
+
@sb_json << s1
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def push_empty_text()
|
164
|
+
@sb_json << ""
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: images_to_scrapbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hiroharu Sugawara
|
@@ -97,6 +97,10 @@ files:
|
|
97
97
|
- Rakefile
|
98
98
|
- bin/console
|
99
99
|
- bin/setup
|
100
|
+
- images_to_scrapbox.gemspec
|
101
|
+
- lib/cli.rb
|
102
|
+
- lib/images_to_scrapbox.rb
|
103
|
+
- lib/images_to_scrapbox/version.rb
|
100
104
|
homepage: https://github.com/hsugawa8651/images_to_scrapbox_gem
|
101
105
|
licenses:
|
102
106
|
- MIT
|
@@ -117,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
121
|
version: '0'
|
118
122
|
requirements: []
|
119
123
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
124
|
+
rubygems_version: 2.7.3
|
121
125
|
signing_key:
|
122
126
|
specification_version: 4
|
123
127
|
summary: convert images files to scrapbox-import-ready-json.
|