pirka 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 +7 -0
- data/.document +3 -0
- data/.gitignore +6 -0
- data/.yardopts +1 -0
- data/COPYING.txt +674 -0
- data/ChangeLog.md +4 -0
- data/Gemfile +14 -0
- data/README.md +201 -0
- data/Rakefile +27 -0
- data/app/detect.rb +166 -0
- data/app/highlight.rb +161 -0
- data/app/lib.rb +52 -0
- data/app/subcommand.rb +35 -0
- data/app/update.rb +67 -0
- data/app.rb +74 -0
- data/bin/pirka +17 -0
- data/lib/pirka/config.rb +110 -0
- data/lib/pirka/library.rb +152 -0
- data/lib/pirka/version.rb +22 -0
- data/lib/pirka.rb +1 -0
- data/pirka.gemspec +49 -0
- data/test/fixtures/YWJj.yaml +10 -0
- data/test/helper.rb +18 -0
- data/test/test_library.rb +152 -0
- data/test/test_pirka.rb +12 -0
- metadata +296 -0
data/app/update.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "English"
|
2
|
+
require "pathname"
|
3
|
+
require "uri"
|
4
|
+
require "pirka/library"
|
5
|
+
require_relative "subcommand"
|
6
|
+
|
7
|
+
module Pirka
|
8
|
+
class App
|
9
|
+
class Update
|
10
|
+
PROGRAM_NAME = "update"
|
11
|
+
DESCRIPTION = "Update library files by remote files"
|
12
|
+
|
13
|
+
include Subcommand
|
14
|
+
|
15
|
+
URI = ::URI.parse("https://gitlab.com/KitaitiMakoto/pirka-library.git")
|
16
|
+
|
17
|
+
def run(argv)
|
18
|
+
parse_options! argv
|
19
|
+
ensure_git_command
|
20
|
+
dir = determine_directory(URI)
|
21
|
+
if dir.directory?
|
22
|
+
update_repository(URI, dir)
|
23
|
+
else
|
24
|
+
clone_repository(URI, dir)
|
25
|
+
$stderr.puts "Library was cloned to:"
|
26
|
+
$stdout.puts dir
|
27
|
+
@config.additional_directories << dir
|
28
|
+
@config.library_repositories << URI
|
29
|
+
begin
|
30
|
+
@config.save
|
31
|
+
$stderr.puts "and added to config file #{@config.filepath.to_s.dump}"
|
32
|
+
rescue Errno::EACCESS => error
|
33
|
+
$stderr.puts "Couldn't save config file to #{@config.filepath.to_s.dump}"
|
34
|
+
$stderr.puts error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def ensure_git_command
|
40
|
+
raise "Cannot find `git` command" unless system("type", "git")
|
41
|
+
end
|
42
|
+
|
43
|
+
# @todo Make more generic
|
44
|
+
def determine_directory(uri)
|
45
|
+
(Pathname.new(Dir.home)/".local/share/pirka"/uri.host/uri.path[1..-1]).sub_ext("")
|
46
|
+
end
|
47
|
+
|
48
|
+
def clone_repository(uri, dir)
|
49
|
+
run_command "git clone #{uri} #{dir}"
|
50
|
+
end
|
51
|
+
|
52
|
+
# @todo Make more generic
|
53
|
+
def update_repository(uri, dir)
|
54
|
+
Dir.chdir dir do
|
55
|
+
run_command "git fetch origin master && git checkout origin/master"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def run_command(command)
|
60
|
+
output = `#{command}`
|
61
|
+
$stderr.puts "Executing \`#{command}\`"
|
62
|
+
raise "Failed to execute \`#{command}\`" unless $CHILD_STATUS.success?
|
63
|
+
output
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/app.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "pirka/version"
|
3
|
+
require "pirka/config"
|
4
|
+
|
5
|
+
gem "epub-parser", Pirka::EPUB_PARSER_VERSION
|
6
|
+
|
7
|
+
module Pirka
|
8
|
+
class App
|
9
|
+
DESCRIPTION = "Pirka highlights source code syntax in EPUB files"
|
10
|
+
APPS = {}
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@config = nil
|
14
|
+
@tmp_opts = {
|
15
|
+
"additional_directories" => []
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def run(argv)
|
20
|
+
parse_options! argv
|
21
|
+
app = APPS[argv.first] ? APPS[argv.shift] : Highlight
|
22
|
+
app.new(@config).run(argv)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse_options!(argv)
|
28
|
+
config_path = Config.filepath
|
29
|
+
|
30
|
+
parser = OptionParser.new {|opt|
|
31
|
+
opt.version = Pirka::VERSION
|
32
|
+
|
33
|
+
opt.banner = <<EOB
|
34
|
+
#{DESCRIPTION}
|
35
|
+
|
36
|
+
Usage: #{opt.program_name} [global options] [<command>] [options]
|
37
|
+
EOB
|
38
|
+
|
39
|
+
opt.separator ""
|
40
|
+
opt.separator "Global options:"
|
41
|
+
opt.on "-c", "--config=FILE", "Config file. Defaults to #{Config.filepath}", Pathname do |path|
|
42
|
+
config_path = path
|
43
|
+
end
|
44
|
+
opt.on "-s", "--data-home=DIRECTORY", "Directory to *SAVE* library data", Pathname do |path|
|
45
|
+
@tmp_opts["data_home"] = path
|
46
|
+
end
|
47
|
+
opt.on "-d", "--directory=DIRECTORY", "Directory to *SEARCH* library data.", "Specify multiple times to add multiple directories.", Pathname do |path|
|
48
|
+
@tmp_opts["additional_directories"] << path
|
49
|
+
end
|
50
|
+
|
51
|
+
opt.separator ""
|
52
|
+
opt.separator "Commands:"
|
53
|
+
width = APPS.keys.collect(&:length).max
|
54
|
+
APPS.each_pair do |command, app|
|
55
|
+
opt.separator opt.summary_indent + command.ljust(width) +
|
56
|
+
opt.summary_indent * 2 + app::DESCRIPTION
|
57
|
+
end
|
58
|
+
opt.separator "If command is ommitted, highlight is used with no option"
|
59
|
+
}
|
60
|
+
parser.order! argv
|
61
|
+
@config = config_path.file? ? Config.load_file(config_path) : Config.new
|
62
|
+
@config.data_home = @tmp_opts["data_home"] if @tmp_opts["data_home"]
|
63
|
+
@config.additional_directories = @tmp_opts["additional_directories"] unless @tmp_opts["additional_directories"].empty?
|
64
|
+
|
65
|
+
Library.data_home = @config.data_home
|
66
|
+
Library.additional_directories = @config.additional_directories
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
require_relative "app/highlight"
|
72
|
+
require_relative "app/detect"
|
73
|
+
require_relative "app/update"
|
74
|
+
require_relative "app/lib"
|
data/bin/pirka
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative "../app"
|
3
|
+
|
4
|
+
root = File.expand_path(File.join(File.dirname(__FILE__),'..'))
|
5
|
+
if File.directory?(File.join(root,'.git'))
|
6
|
+
Dir.chdir(root) do
|
7
|
+
begin
|
8
|
+
require 'bundler/setup'
|
9
|
+
rescue LoadError => e
|
10
|
+
warn e.message
|
11
|
+
warn "Run `gem install bundler` to install Bundler"
|
12
|
+
exit -1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Pirka::App.new.run(ARGV)
|
data/lib/pirka/config.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "yaml"
|
3
|
+
require "pirka/library"
|
4
|
+
|
5
|
+
module Pirka
|
6
|
+
class Config
|
7
|
+
FILE_NAME = "pirka.yaml"
|
8
|
+
XDG_CONFIG_HOME = Pathname.new(".config")
|
9
|
+
CONFIG_FILE= XDG_CONFIG_HOME/FILE_NAME
|
10
|
+
XDG_CONFIG_DIRS = [Pathname.new("/etc/xdg")]
|
11
|
+
CONFIG_DIRS = XDG_CONFIG_DIRS.collect {|dir| dir/FILE_NAME}
|
12
|
+
|
13
|
+
@config_home = nil
|
14
|
+
@additional_directories = []
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :config_home, :additional_directories
|
18
|
+
|
19
|
+
def directories(user = nil)
|
20
|
+
config_dirs = ENV["XDG_CONFIG_DIRS"] ?
|
21
|
+
ENX["XDG_CONFIG_DIR"].split(":").collect {|dir| Pathname.new(dir)} :
|
22
|
+
CONFIG_DIRS
|
23
|
+
config_home = ENV["XDG_CONFIG_HOME"] ?
|
24
|
+
Pathname.new(ENV["XDG_CONFIG_HOME"]) :
|
25
|
+
Pathname.new(Dir.home(user))/XDG_CONFIG_HOME
|
26
|
+
([@config_home, config_home] + @additional_directories + config_dirs).compact
|
27
|
+
end
|
28
|
+
|
29
|
+
def config_directory(user = nil)
|
30
|
+
directories.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_file(path)
|
34
|
+
load_hash(
|
35
|
+
YAML.load_file(path).each_with_object({}) {|(key, value), h|
|
36
|
+
h[key] = case key
|
37
|
+
when "data_home"
|
38
|
+
Pathname(value)
|
39
|
+
when "additional_directories"
|
40
|
+
value.collect {|val| Pathname(val)}
|
41
|
+
when "library_repositories"
|
42
|
+
value.collect {|val| URI(val)}
|
43
|
+
else
|
44
|
+
value
|
45
|
+
end
|
46
|
+
})
|
47
|
+
end
|
48
|
+
|
49
|
+
def load_hash(h)
|
50
|
+
config = new
|
51
|
+
%w[data_home additional_directories library_repositories].each do |attr|
|
52
|
+
config.__send__("#{attr}=", h[attr]) if h[attr]
|
53
|
+
end
|
54
|
+
config
|
55
|
+
end
|
56
|
+
|
57
|
+
def filepath
|
58
|
+
config_directory/FILE_NAME
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_accessor :data_home, :additional_directories, :library_repositories
|
63
|
+
|
64
|
+
# @todo Consider login user
|
65
|
+
def initialize
|
66
|
+
@data_home = nil
|
67
|
+
@additional_directories = []
|
68
|
+
@library_repositories = []
|
69
|
+
end
|
70
|
+
|
71
|
+
# @todo Consider login user
|
72
|
+
def path_from_repository(repository_uri)
|
73
|
+
Library.data_directory/dirname_from_repository(repository_uri)
|
74
|
+
end
|
75
|
+
|
76
|
+
# @todo Consider URIs other than Git HTTPS URI
|
77
|
+
def dirname_from_repository(repository_uri)
|
78
|
+
repository_uri.host/repository_uri.path[1..-1].sub_ext("")
|
79
|
+
end
|
80
|
+
|
81
|
+
def config_directory(user = nil)
|
82
|
+
self.class.config_directory(user)
|
83
|
+
end
|
84
|
+
|
85
|
+
def filepath
|
86
|
+
self.class.filepath
|
87
|
+
end
|
88
|
+
|
89
|
+
def save(path = nil)
|
90
|
+
path = Pathname(path || filepath)
|
91
|
+
path.dirname.mkpath unless path.dirname.directory?
|
92
|
+
path.write to_yaml
|
93
|
+
path
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_h
|
97
|
+
h = {}
|
98
|
+
h["data_home"] = @data_home.to_path if @data_home
|
99
|
+
%w[additional_directories library_repositories].each do |key|
|
100
|
+
value = __send__(key)
|
101
|
+
h[key] = value.collect(&:to_s) unless value.empty?
|
102
|
+
end
|
103
|
+
h
|
104
|
+
end
|
105
|
+
|
106
|
+
def to_yaml
|
107
|
+
to_h.to_yaml
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "base64"
|
3
|
+
require "yaml"
|
4
|
+
require "epub/parser/cfi"
|
5
|
+
|
6
|
+
module Pirka
|
7
|
+
# Environment variables affect this class:
|
8
|
+
# XDG_DATA_HOME: Directory to store library files and read. Defaults to $HOME/.local/share
|
9
|
+
# XDG_DATA_DIRS: Directories to search library files. Separated with a colon ':'. Defaults to /usr/local/share:/usr/share
|
10
|
+
#
|
11
|
+
# @see https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
12
|
+
class Library
|
13
|
+
DIR_NAME = "pirka/local"
|
14
|
+
EXT = ".yaml"
|
15
|
+
SUBDIR_LENGTH = 4
|
16
|
+
XDG_DATA_HOME = Pathname.new(".local/share")
|
17
|
+
DATA_HOME = XDG_DATA_HOME/DIR_NAME
|
18
|
+
XDG_DATA_DIRS = [Pathname.new("/usr/local/share"), Pathname.new("/usr/share")]
|
19
|
+
DATA_DIRS = XDG_DATA_DIRS.collect {|dir| dir/DIR_NAME}
|
20
|
+
|
21
|
+
@data_home = nil
|
22
|
+
@additional_directories = []
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_accessor :data_home, :additional_directories
|
26
|
+
|
27
|
+
# @return [Array<Pathname>]
|
28
|
+
def directories(user = nil)
|
29
|
+
data_dirs = ENV["XDG_DATA_DIRS"] ?
|
30
|
+
ENV["XDG_DATA_DIRS"].split(":").collect {|dir| Pathname.new(dir)/DIR_NAME} :
|
31
|
+
DATA_DIRS
|
32
|
+
data_home = ENV["XDG_DATA_HOME"] ?
|
33
|
+
Pathname.new(ENV["XDG_DATA_HOME"])/DIR_NAME :
|
34
|
+
Pathname.new(Dir.home(user))/DATA_HOME
|
35
|
+
([@data_home, data_home] + @additional_directories + data_dirs).compact
|
36
|
+
end
|
37
|
+
|
38
|
+
# @see https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
39
|
+
def data_directory(user = nil)
|
40
|
+
directories.first
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param [String] release_identifier
|
44
|
+
# @return [Library, nil]
|
45
|
+
def find_by_release_identifier(release_identifier)
|
46
|
+
lib_path = filename(release_identifier)
|
47
|
+
directories.each do |dir|
|
48
|
+
path = dir/lib_path
|
49
|
+
return load_file(path) if path.file?
|
50
|
+
end
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param [String] release_identifier Release Identifier
|
55
|
+
# @return [String] String that `Release Identifier` property in metadata is encoded based on RFC 4648 "Base 64 Encoding with URL and Filename Safe Alphabet"
|
56
|
+
# @see https://tools.ietf.org/html/rfc4648#page-7
|
57
|
+
# @todo Better name
|
58
|
+
def basename_without_ext(release_identifier)
|
59
|
+
Base64.urlsafe_encode64(release_identifier)
|
60
|
+
end
|
61
|
+
|
62
|
+
def filename(release_identifier)
|
63
|
+
name = basename_without_ext(release_identifier)
|
64
|
+
name.insert(SUBDIR_LENGTH, "/") if name.length > SUBDIR_LENGTH
|
65
|
+
name + EXT
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param [Pathname, String] path
|
69
|
+
# @return [Library]
|
70
|
+
def load_file(path)
|
71
|
+
load_hash(YAML.load_file(path.to_s))
|
72
|
+
end
|
73
|
+
|
74
|
+
# @param [Hash] h
|
75
|
+
# @return [Library]
|
76
|
+
def load_hash(h)
|
77
|
+
library = new
|
78
|
+
|
79
|
+
h.each_pair do |key, value|
|
80
|
+
if key == "codelist"
|
81
|
+
value.each_pair do |cfi, data|
|
82
|
+
library.codelist[EPUB::Parser::CFI.parse(cfi)] = data
|
83
|
+
end
|
84
|
+
else
|
85
|
+
library.metadata[key] = value
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
library
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_reader :metadata, :codelist
|
94
|
+
|
95
|
+
# @param [Pathname, String, nil] directory for library files. When `nil` passed, default directories are used
|
96
|
+
def initialize
|
97
|
+
@metadata = {}
|
98
|
+
@codelist = {}
|
99
|
+
end
|
100
|
+
|
101
|
+
def data_directory(user = nil)
|
102
|
+
self.class.data_directory(user)
|
103
|
+
end
|
104
|
+
|
105
|
+
def filename
|
106
|
+
raise "Release Identifier is not set" unless @metadata["Release Identifier"]
|
107
|
+
self.class.filename(@metadata["Release Identifier"])
|
108
|
+
end
|
109
|
+
|
110
|
+
# @param [Pathname, String, nil] path File path to save library data.
|
111
|
+
# When `nil` is passwd, default directory + filename determined by Release Identifier is used
|
112
|
+
# @return [Pathname] File path that library data was saved
|
113
|
+
def save(path = nil)
|
114
|
+
path = data_directory/filename unless path
|
115
|
+
path = Pathname(path)
|
116
|
+
path.dirname.mkpath unless path.dirname.directory?
|
117
|
+
path.write to_yaml
|
118
|
+
path
|
119
|
+
end
|
120
|
+
|
121
|
+
# Iterate over codelist in order of EPUB CFI
|
122
|
+
# @overload each
|
123
|
+
# @yieldparam [EPUB::CFI] cfi EPUB CFI indicating code element
|
124
|
+
# @yieldparam [String] language Language name
|
125
|
+
# @overload each
|
126
|
+
# @return [Enumerator] Enumerator which iterates over cfi and lang
|
127
|
+
def each
|
128
|
+
sorted_list = @codelist.each_pair.sort_by {|(cfi, lang)| cfi}
|
129
|
+
if block_given?
|
130
|
+
sorted_list.each do |(cfi, lang)|
|
131
|
+
yield cfi, lang
|
132
|
+
end
|
133
|
+
else
|
134
|
+
sorted_list.each
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# @return [Hash]
|
139
|
+
def to_h
|
140
|
+
metadata.merge({
|
141
|
+
"codelist" => each.with_object({}) {|(cfi, value), list|
|
142
|
+
list[cfi.to_fragment] = value
|
143
|
+
}
|
144
|
+
})
|
145
|
+
end
|
146
|
+
|
147
|
+
# @return [String]
|
148
|
+
def to_yaml
|
149
|
+
to_h.to_yaml
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2017 KITAITI Makoto (KitaitiMakoto at gmail.com)
|
3
|
+
#
|
4
|
+
# pirka is free software: you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# pirka is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with pirka. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
#
|
17
|
+
|
18
|
+
module Pirka
|
19
|
+
# pirka version
|
20
|
+
VERSION = "0.1.0"
|
21
|
+
EPUB_PARSER_VERSION = "0.3.1"
|
22
|
+
end
|
data/lib/pirka.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pirka/version'
|
data/pirka.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'pirka/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = "pirka"
|
9
|
+
gem.version = Pirka::VERSION
|
10
|
+
gem.summary = %q{Syntax highlighting tool for EPUB books}
|
11
|
+
gem.description = %q{Pirka highlights source code syntax in EPUB books}
|
12
|
+
gem.license = "GPL"
|
13
|
+
gem.authors = ["KITAITI Makoto"]
|
14
|
+
gem.email = "KitaitiMakoto@gmail.com"
|
15
|
+
gem.homepage = "https://gitlab.com/KitaitiMakoto/pirka"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
|
19
|
+
`git submodule --quiet foreach --recursive pwd`.split($/).each do |submodule|
|
20
|
+
submodule.sub!("#{Dir.pwd}/",'')
|
21
|
+
|
22
|
+
Dir.chdir(submodule) do
|
23
|
+
`git ls-files`.split($/).map do |subpath|
|
24
|
+
gem.files << File.join(submodule,subpath)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
29
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
30
|
+
gem.require_paths = ['lib']
|
31
|
+
|
32
|
+
gem.add_runtime_dependency 'epub-parser', Pirka::EPUB_PARSER_VERSION
|
33
|
+
gem.add_runtime_dependency 'epub-maker'
|
34
|
+
gem.add_runtime_dependency 'rouge'
|
35
|
+
gem.add_runtime_dependency 'rouge-lexers-fluentd'
|
36
|
+
gem.add_runtime_dependency 'optparse-pathname'
|
37
|
+
gem.add_runtime_dependency 'colored'
|
38
|
+
|
39
|
+
gem.add_development_dependency 'bundler'
|
40
|
+
gem.add_development_dependency 'rake'
|
41
|
+
gem.add_development_dependency 'test-unit'
|
42
|
+
gem.add_development_dependency 'test-unit-notify'
|
43
|
+
gem.add_development_dependency 'simplecov'
|
44
|
+
gem.add_development_dependency 'rubygems-tasks'
|
45
|
+
gem.add_development_dependency 'yard'
|
46
|
+
gem.add_development_dependency 'pry'
|
47
|
+
gem.add_development_dependency 'pry-doc'
|
48
|
+
gem.add_development_dependency 'zipruby'
|
49
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError => error
|
6
|
+
abort error.message
|
7
|
+
end
|
8
|
+
|
9
|
+
require "simplecov"
|
10
|
+
SimpleCov.start do
|
11
|
+
add_filter "/test"
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'test/unit'
|
15
|
+
require "test/unit/notify"
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require "helper"
|
2
|
+
require "yaml"
|
3
|
+
require "tmpdir"
|
4
|
+
require "pirka/library"
|
5
|
+
require "epub/parser/cfi"
|
6
|
+
|
7
|
+
class TestLibrary < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
Pirka::Library.data_home = nil
|
10
|
+
Pirka::Library.additional_directories.clear
|
11
|
+
@library = Pirka::Library.new
|
12
|
+
@library.metadata["Release Identifier"] = "abc"
|
13
|
+
@library.metadata["title"] = "abc"
|
14
|
+
@library.codelist[EPUB::CFI("/6/30!/4/2/58/2")] = {"language" => "nginx"}
|
15
|
+
@library.codelist[EPUB::CFI("/6/31!/4/2/56/2")] = {"language" => "nginx"}
|
16
|
+
@library.codelist[EPUB::CFI("/6/30!/4/2/56/2")] = {"language" => "nginx"}
|
17
|
+
@fixure_path = "test/fixtures/YWJj.yaml"
|
18
|
+
@yaml = <<EOY
|
19
|
+
---
|
20
|
+
Release Identifier: abc
|
21
|
+
title: abc
|
22
|
+
codelist:
|
23
|
+
epubcfi(/6/30!/4/2/56/2):
|
24
|
+
language: nginx
|
25
|
+
epubcfi(/6/30!/4/2/58/2):
|
26
|
+
language: nginx
|
27
|
+
epubcfi(/6/31!/4/2/56/2):
|
28
|
+
language: nginx
|
29
|
+
EOY
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_load_hash
|
33
|
+
actual = Pirka::Library.load_hash(YAML.load(@yaml))
|
34
|
+
assert_equal @library.metadata, actual.metadata
|
35
|
+
assert_equal @library.each.to_a, actual.each.to_a
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_load_file
|
39
|
+
actual = Pirka::Library.load_file(@fixure_path)
|
40
|
+
assert_equal @library.metadata, actual.metadata
|
41
|
+
assert_equal @library.each.to_a, actual.each.to_a
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_each_iterates_over_list_in_order_of_cfi
|
45
|
+
cfis = %w[/6/30!/4/2/56/2
|
46
|
+
/6/30!/4/2/58/2
|
47
|
+
/6/31!/4/2/56/2]
|
48
|
+
i = 0
|
49
|
+
@library.each do |(cfi, _)|
|
50
|
+
assert_equal cfi.to_s, cfis[i]
|
51
|
+
i += 1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_each_returns_enumerator
|
56
|
+
assert_equal %w[/6/30!/4/2/56/2
|
57
|
+
/6/30!/4/2/58/2
|
58
|
+
/6/31!/4/2/56/2],
|
59
|
+
@library.each.collect {|(cfi, _)| cfi.to_s}
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_to_yaml
|
63
|
+
expected = YAML.load(@yaml)
|
64
|
+
actual = YAML.load(@library.to_yaml)
|
65
|
+
assert_equal expected, actual
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_save_to_path_when_path_specified
|
69
|
+
Dir.mktmpdir "pirka" do |dir|
|
70
|
+
path = Pathname.new(dir)/"arbitral-filename.yaml"
|
71
|
+
@library.save(path)
|
72
|
+
data = YAML.load_file(path.to_path)
|
73
|
+
codelist = data.delete("codelist")
|
74
|
+
assert_equal @library.metadata, data
|
75
|
+
|
76
|
+
expected_codelist = @library.each.with_object({}) {|(cfi, value), list|
|
77
|
+
list[cfi.to_fragment] = value
|
78
|
+
}
|
79
|
+
assert_equal expected_codelist, codelist
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_save_to_default_directory_when_path_not_specified
|
84
|
+
Dir.mktmpdir "pirka" do |dir|
|
85
|
+
xdh = ENV["XDG_DATA_HOME"]
|
86
|
+
begin
|
87
|
+
ENV["XDG_DATA_HOME"] = dir
|
88
|
+
@library.save
|
89
|
+
ensure
|
90
|
+
ENV["XDG_DATA_HOME"] = xdh
|
91
|
+
end
|
92
|
+
path = Pathname.new(dir)/"pirka/local/YWJj.yaml"
|
93
|
+
assert_path_exist path
|
94
|
+
|
95
|
+
data = YAML.load_file(path)
|
96
|
+
codelist = data.delete("codelist")
|
97
|
+
assert_equal @library.metadata, data
|
98
|
+
|
99
|
+
expected_codelist = @library.each.with_object({}) {|(cfi, value), list|
|
100
|
+
list[cfi.to_fragment] = value
|
101
|
+
}
|
102
|
+
assert_equal expected_codelist, codelist
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_save_to_specified_directory_when_data_home_is_specified
|
107
|
+
Dir.mktmpdir "pirka" do |dir|
|
108
|
+
Pirka::Library.data_home = Pathname.new(dir)
|
109
|
+
library = Pirka::Library.new
|
110
|
+
library.metadata["Release Identifier"] = "abc"
|
111
|
+
library.metadata["title"] = "abc"
|
112
|
+
library.codelist[EPUB::CFI("/6/30!/4/2/58/2")] = {"language" => "nginx"}
|
113
|
+
library.codelist[EPUB::CFI("/6/31!/4/2/56/2")] = {"language" => "nginx"}
|
114
|
+
library.codelist[EPUB::CFI("/6/30!/4/2/56/2")] = {"language" => "nginx"}
|
115
|
+
|
116
|
+
library.save
|
117
|
+
|
118
|
+
path = Pathname.new("#{dir}/YWJj.yaml")
|
119
|
+
assert_path_exist path.to_path
|
120
|
+
assert_equal @yaml, path.read
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_save_makes_subdirectory_when_basename_is_longer_than_4_characters
|
125
|
+
Dir.mktmpdir "pirka" do |dir|
|
126
|
+
Pirka::Library.data_home = Pathname.new(dir)
|
127
|
+
library = Pirka::Library.new
|
128
|
+
library.metadata["Release Identifier"] = "abcd"
|
129
|
+
|
130
|
+
library.save
|
131
|
+
path = Pathname.new(dir)/"YWJj/ZA==.yaml"
|
132
|
+
assert_path_exist path
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_find_by_release_identifier
|
137
|
+
Dir.mktmpdir "pirka" do |dir|
|
138
|
+
path = Pathname.new(dir)/"pirka/local/YWJj.yaml"
|
139
|
+
path.dirname.mkpath
|
140
|
+
path.write(@yaml)
|
141
|
+
xdh = ENV["XDG_DATA_HOME"]
|
142
|
+
begin
|
143
|
+
ENV["XDG_DATA_HOME"] = dir
|
144
|
+
library = Pirka::Library.find_by_release_identifier("abc")
|
145
|
+
ensure
|
146
|
+
ENV["XDG_DATA_HOME"] = xdh
|
147
|
+
end
|
148
|
+
assert_equal @library.metadata, library.metadata
|
149
|
+
assert_equal @library.each.to_a, library.each.to_a
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|