htot_conv 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "htot_conv"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "htot_conv"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/htot_conv CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
- #
4
- require 'rubygems'
5
- require 'htot_conv'
6
- require 'htot_conv/cli'
7
-
8
- HTOTConv::CLI.main(ARGV)
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ require 'rubygems'
5
+ require 'htot_conv'
6
+ require 'htot_conv/cli'
7
+
8
+ HTOTConv::CLI.main(ARGV)
data/htot_conv.gemspec CHANGED
@@ -1,37 +1,37 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'htot_conv/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "htot_conv"
8
- spec.version = HTOTConv::VERSION
9
- spec.authors = ["@cat_in_136"]
10
- spec.email = ["cat.in.136+github@gmail.com"]
11
-
12
- spec.summary = %q{Hierarchical-Tree Outline Text Converter}
13
- spec.description = %q{Convert from a simple hierarchical-tree outline text into ugly xlsx file}
14
- spec.homepage = "https://github.com/cat-in-136/htot_conv"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- if spec.respond_to?(:metadata)
20
- #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
- else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
- end
24
-
25
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
29
-
30
- spec.add_dependency "axlsx", "~> 2.0.1"
31
- spec.add_dependency "rinne", "~> 0.0.3"
32
-
33
- spec.add_development_dependency "bundler", "~> 1.12"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "minitest", "~> 5.0"
36
- spec.add_development_dependency "fakefs", "~> 0.11.3"
37
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'htot_conv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "htot_conv"
8
+ spec.version = HTOTConv::VERSION
9
+ spec.authors = ["@cat_in_136"]
10
+ spec.email = ["cat.in.136+github@gmail.com"]
11
+
12
+ spec.summary = %q{Hierarchical-Tree Outline Text Converter}
13
+ spec.description = %q{Convert from a simple hierarchical-tree outline text into ugly xlsx file}
14
+ spec.homepage = "https://github.com/cat-in-136/htot_conv"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "axlsx", "~> 2.0.1"
31
+ spec.add_dependency "rinne", "~> 0.0.3"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.12"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "fakefs", "~> 0.11.3"
37
+ end
data/lib/htot_conv/cli.rb CHANGED
@@ -1,160 +1,174 @@
1
- # frozen_string_literal: true
2
- require 'optparse'
3
-
4
- require 'rinne'
5
-
6
- module HTOTConv
7
- module CLI
8
- class ScriptOptions
9
- def initialize
10
- @options = {
11
- :from_type => :simple_text,
12
- :to_type => :xlsx_type2,
13
- }
14
- @from_options = {}
15
- @to_options = {}
16
- end
17
- attr_accessor :options, :from_options, :to_options
18
-
19
- def define_options(opts, io_filter=false)
20
- opts.banner = %q{Hierarchical-Tree Outline Text Converter}
21
- opts.define_head %q{Usage: htot_conv [options] [input] [output]}
22
- opts.separator %q{}
23
- opts.separator %q{Options:}
24
-
25
- from_types = HTOTConv::Parser.types.map { |v| [v, v.to_s.tr("_", "-")] }.flatten
26
- to_types = HTOTConv::Generator.types.map { |v| [v, v.to_s.tr("_", "-")] }.flatten
27
-
28
- opts.on("-f", "--from-type=TYPE", from_types, "type of input (default: #{options[:from_type]})") do |v|
29
- options[:from_type] = v.to_s.tr("-", "_")
30
- end
31
- opts.on("-t", "--to-type=TYPE", to_types, "type of output (default: #{options[:to_type]})") do |v|
32
- options[:to_type] = v.to_s.tr("-", "_")
33
- end
34
- opts.on("-l", "--list-type", "list input/output type") do
35
- $stdout << "type of input:\n"
36
- $stdout << HTOTConv::Parser.types.join(" ") << "\n"
37
- $stdout << "\n"
38
- $stdout << "type of output:\n"
39
- $stdout << HTOTConv::Generator.types.join(" ") << "\n"
40
- $stdout << "\n"
41
- exit
42
- end
43
-
44
- opts.separator ""
45
- opts.on("-h", "-?", "--help", "Show this message") do
46
- puts opts
47
- exit
48
- end
49
- opts.on("--version", "Show version") do
50
- $stdout << "htot_conv #{HTOTConv::VERSION}\n"
51
- exit
52
- end
53
-
54
- opts.separator ""
55
- opts.separator "I/O Options:"
56
- if io_filter
57
- define_sub_options_of(opts, HTOTConv::Parser, options[:from_type], "from") do |key, v|
58
- @from_options[key] = v
59
- end
60
- define_sub_options_of(opts, HTOTConv::Generator, options[:to_type], "from") do |key, v|
61
- @to_options[key] = v
62
- end
63
- else
64
- define_sub_options(opts, HTOTConv::Parser, "from") do |key, v|
65
- @from_options[key] = v
66
- end
67
- define_sub_options(opts, HTOTConv::Generator, "to") do |key, v|
68
- @to_options[key] = v
69
- end
70
- end
71
- end
72
-
73
- private
74
- def define_sub_options_of(opts, klass, type, prefix) # :yields: key, v
75
- type_klass = klass.const_get(Rinne.camelize(type.to_s))
76
- type_klass.option_help.each do |key,v|
77
- long_option = "--#{prefix}-#{key.to_s.tr('_','-')}"
78
- opts.on("#{long_option}=VAL", v[:pat], "For #{type}, #{v[:desc]}") do |v|
79
- yield key, v
80
- end
81
- end
82
- end
83
-
84
- private
85
- def define_sub_options(opts, klass, prefix) # :yields: key, v
86
- options = {}
87
-
88
- klass.types.each do |type|
89
- type_klass = klass.const_get(Rinne.camelize(type.to_s))
90
- type_klass.option_help.each do |key,v|
91
- long_option = "--#{prefix}-#{key.to_s.tr('_','-')}"
92
-
93
- if options.include?(long_option)
94
- options[long_option][:desc] << "For #{type}, #{v[:desc]}"
95
- unless options[long_option][:pattern] == v[:pat]
96
- if (options[long_option][:pattern].kind_of?(Array) && v[:pat].kind_of?(Array))
97
- options[long_option][:pattern] = options[long_option][:pattern].concat(v[:pat]).uniq
98
- else
99
- raise "pattern registration mismatch around #{long_option}"
100
- end
101
- end
102
- else
103
- options[long_option] = {
104
- :key => key,
105
- :pattern => v[:pat],
106
- :desc => ["For #{type}, #{v[:desc]}"],
107
- }
108
- end
109
- end
110
- end
111
-
112
- options.each do |long_option, value|
113
- opts.on("#{long_option}=VAL", value[:pattern], *value[:desc]) do |v|
114
- yield value[:key], v
115
- end
116
- end
117
- end
118
- end
119
-
120
- def optparse(args)
121
- script_opts = ScriptOptions.new
122
- OptionParser.new do |opts|
123
- script_opts.define_options(opts)
124
-
125
- begin
126
- opts.parse!(args.dup)
127
- rescue OptionParser::ParseError => ex
128
- $stderr << ex.message << "\n"
129
- exit 1
130
- end
131
- end
132
-
133
- OptionParser.new do |opts|
134
- script_opts.define_options(opts, true)
135
-
136
- begin
137
- opts.parse!(args)
138
- rescue OptionParser::ParseError => ex
139
- $stderr << ex.message << "\n"
140
- exit 1
141
- end
142
- end
143
- script_opts
144
- end
145
- module_function :optparse
146
-
147
- def main(args=ARGV)
148
- script_opts = HTOTConv::CLI.optparse(args)
149
- options = script_opts.options
150
- from_options = script_opts.from_options
151
- to_options = script_opts.to_options
152
-
153
- inio = ((args.length > 0) && (args[0] != "-"))? File.open(args[0], "rb") : $stdin
154
- outio = ((args.length > 1) && (args[1] != "-"))? File.open(args[1], "wb") : $stdout
155
-
156
- HTOTConv.convert(inio, options[:from_type], outio, options[:to_type], from_options, to_options)
157
- end
158
- module_function :main
159
- end
160
- end
1
+ # frozen_string_literal: true
2
+ require 'optparse'
3
+
4
+ require 'rinne'
5
+
6
+ module HTOTConv
7
+ module CLI
8
+ class ScriptOptions
9
+ def initialize
10
+ @options = {
11
+ :from_type => :simple_text,
12
+ :to_type => :xlsx_type2,
13
+ }
14
+ @from_options = {}
15
+ @to_options = {}
16
+ end
17
+ attr_reader :options, :from_options, :to_options
18
+
19
+ def freeze
20
+ @options.freeze
21
+ @from_options.freeze
22
+ @to_options.freeze
23
+ super
24
+ end
25
+
26
+ def define_options(opts, io_filter=false)
27
+ opts.banner = %q{Hierarchical-Tree Outline Text Converter}
28
+ opts.define_head %q{Usage: htot_conv [options] [input] [output]}
29
+ opts.separator %q{}
30
+ opts.separator %q{Options:}
31
+
32
+ from_types = HTOTConv::Parser.types.map { |v| [v, v.to_s.tr("_", "-")] }.flatten
33
+ to_types = HTOTConv::Generator.types.map { |v| [v, v.to_s.tr("_", "-")] }.flatten
34
+
35
+ opts.on("-f", "--from-type=TYPE", from_types, "type of input (default: #{options[:from_type]})") do |v|
36
+ @options[:from_type] = v.to_s.tr("-", "_")
37
+ end
38
+ opts.on("-t", "--to-type=TYPE", to_types, "type of output (default: #{options[:to_type]})") do |v|
39
+ @options[:to_type] = v.to_s.tr("-", "_")
40
+ end
41
+ opts.on("-l", "--list-type", "list input/output type") do
42
+ $stdout << "type of input:\n"
43
+ $stdout << HTOTConv::Parser.types.join(" ") << "\n"
44
+ $stdout << "\n"
45
+ $stdout << "type of output:\n"
46
+ $stdout << HTOTConv::Generator.types.join(" ") << "\n"
47
+ $stdout << "\n"
48
+ exit
49
+ end
50
+
51
+ opts.separator ""
52
+ opts.on("-h", "-?", "--help", "Show this message") do
53
+ puts opts
54
+ exit
55
+ end
56
+ opts.on("--version", "Show version") do
57
+ $stdout << "htot_conv #{HTOTConv::VERSION}\n"
58
+ exit
59
+ end
60
+
61
+ opts.separator ""
62
+ opts.separator "I/O Options:"
63
+ if io_filter
64
+ define_sub_options_of(opts, HTOTConv::Parser, @options[:from_type], "from") do |key, v|
65
+ @from_options[key] = v
66
+ end
67
+ define_sub_options_of(opts, HTOTConv::Generator, @options[:to_type], "to") do |key, v|
68
+ @to_options[key] = v
69
+ end
70
+ else
71
+ define_sub_options(opts, HTOTConv::Parser, "from") do |key, v|
72
+ @from_options[key] = v
73
+ end
74
+ define_sub_options(opts, HTOTConv::Generator, "to") do |key, v|
75
+ @to_options[key] = v
76
+ end
77
+ end
78
+ end
79
+
80
+ private
81
+ def add_sub_options_of_a_type_to!(cli_options, opts, klass, type, prefix)
82
+ type_klass = klass.const_get(Rinne.camelize(type.to_s))
83
+ type_klass.option_help.each do |key,v|
84
+ long_option = "--#{prefix}-#{key.to_s.tr('_','-')}"
85
+
86
+ if cli_options.include?(long_option)
87
+ cli_options[long_option][:desc] << "For #{type}, #{v[:desc]}"
88
+ unless cli_options[long_option][:pattern] == v[:pat]
89
+ if (cli_options[long_option][:pattern].kind_of?(Array) && v[:pat].kind_of?(Array))
90
+ cli_options[long_option][:pattern] = cli_options[long_option][:pattern].concat(v[:pat]).uniq
91
+ else
92
+ raise "pattern registration mismatch around #{long_option}"
93
+ end
94
+ end
95
+ else
96
+ cli_options[long_option] = {
97
+ :key => key,
98
+ :pattern => v[:pat],
99
+ :desc => ["For #{type}, #{v[:desc]}"],
100
+ }
101
+ end
102
+ end
103
+ end
104
+
105
+ private
106
+ def define_sub_options_of(opts, klass, type, prefix) # :yields: key, v
107
+ cli_options = {}
108
+
109
+ add_sub_options_of_a_type_to!(cli_options, opts, klass, type, prefix)
110
+
111
+ cli_options.each do |long_option, value|
112
+ opts.on("#{long_option}=VAL", value[:pattern], *value[:desc]) do |v|
113
+ yield value[:key], v
114
+ end
115
+ end
116
+ end
117
+
118
+ private
119
+ def define_sub_options(opts, klass, prefix) # :yields: key, v
120
+ cli_options = {}
121
+
122
+ klass.types.each do |type|
123
+ add_sub_options_of_a_type_to!(cli_options, opts, klass, type, prefix)
124
+ end
125
+
126
+ cli_options.each do |long_option, value|
127
+ opts.on("#{long_option}=VAL", value[:pattern], *value[:desc]) do |v|
128
+ yield value[:key], v
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ def optparse!(args)
135
+ script_opts = ScriptOptions.new
136
+ OptionParser.new do |opts|
137
+ script_opts.define_options(opts)
138
+
139
+ begin
140
+ opts.parse!(args.dup)
141
+ rescue OptionParser::ParseError => ex
142
+ $stderr << ex.message << "\n"
143
+ exit 1
144
+ end
145
+ end
146
+
147
+ OptionParser.new do |opts|
148
+ script_opts.define_options(opts, true)
149
+
150
+ begin
151
+ opts.parse!(args)
152
+ rescue OptionParser::ParseError => ex
153
+ $stderr << ex.message << "\n"
154
+ exit 1
155
+ end
156
+ end
157
+ script_opts.freeze
158
+ end
159
+ module_function :optparse!
160
+
161
+ def main(args=ARGV)
162
+ script_opts = HTOTConv::CLI.optparse!(args)
163
+ options = script_opts.options
164
+ from_options = script_opts.from_options
165
+ to_options = script_opts.to_options
166
+
167
+ inio = ((args.length > 0) && (args[0] != "-"))? File.open(args[0], "rb") : $stdin
168
+ outio = ((args.length > 1) && (args[1] != "-"))? File.open(args[1], "wb") : $stdout
169
+
170
+ HTOTConv.convert(inio, options[:from_type], outio, options[:to_type], from_options, to_options)
171
+ end
172
+ module_function :main
173
+ end
174
+ end
@@ -1,35 +1,35 @@
1
- # frozen_string_literal: true
2
- module HTOTConv
3
- module Generator
4
- class Base
5
- def initialize(data, option={})
6
- data.validate
7
- @data = data
8
- @option = self.class.option_help.inject({}) { |h, pair| h[pair[0]] = pair[1][:default]; h}.merge(option)
9
- end
10
- def self.option_help
11
- {}
12
- end
13
-
14
- def output(outputfile)
15
- raise NotImplementedError.new("#{self.class.name}.#{__method__} is an abstract method.")
16
- end
17
- end
18
-
19
- class XlsxBase < Base
20
- def output_to_worksheet(ws)
21
- raise NotImplementedError.new("#{self.class.name}.#{__method__} is an abstract method.")
22
- end
23
-
24
- def output(outputfile)
25
- p = Axlsx::Package.new
26
- p.workbook do |wb|
27
- wb.add_worksheet do |ws|
28
- output_to_worksheet(ws)
29
- end
30
- end
31
- p.serialize(outputfile)
32
- end
33
- end
34
- end
35
- end
1
+ # frozen_string_literal: true
2
+ module HTOTConv
3
+ module Generator
4
+ class Base
5
+ def initialize(data, option={})
6
+ data.validate
7
+ @data = data
8
+ @option = self.class.option_help.inject({}) { |h, pair| h[pair[0]] = pair[1][:default]; h}.merge(option)
9
+ end
10
+ def self.option_help
11
+ {}
12
+ end
13
+
14
+ def output(outputfile)
15
+ raise NotImplementedError.new("#{self.class.name}.#{__method__} is an abstract method.")
16
+ end
17
+ end
18
+
19
+ class XlsxBase < Base
20
+ def output_to_worksheet(ws)
21
+ raise NotImplementedError.new("#{self.class.name}.#{__method__} is an abstract method.")
22
+ end
23
+
24
+ def output(outputfile)
25
+ p = Axlsx::Package.new
26
+ p.workbook do |wb|
27
+ wb.add_worksheet do |ws|
28
+ output_to_worksheet(ws)
29
+ end
30
+ end
31
+ p.serialize(outputfile)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,24 +1,24 @@
1
- # frozen_string_literal: true
2
- require 'axlsx'
3
-
4
- require 'htot_conv/generator/base'
5
-
6
- module HTOTConv
7
- module Generator
8
- class XlsxType0 < XlsxBase
9
- def output_to_worksheet(ws)
10
- max_value_length = @data.max_value_length
11
-
12
- ws.add_row([@data.key_header[0], 'Outline Level'].concat(
13
- HTOTConv::Util.pad_array(@data.value_header, max_value_length)),
14
- :style => Axlsx::STYLE_THIN_BORDER)
15
-
16
- @data.item.each do |item|
17
- ws.add_row([item.key, item.level.to_i].concat(
18
- HTOTConv::Util.pad_array(item.value, max_value_length)),
19
- :style => Axlsx::STYLE_THIN_BORDER)
20
- end
21
- end
22
- end
23
- end
24
- end
1
+ # frozen_string_literal: true
2
+ require 'axlsx'
3
+
4
+ require 'htot_conv/generator/base'
5
+
6
+ module HTOTConv
7
+ module Generator
8
+ class XlsxType0 < XlsxBase
9
+ def output_to_worksheet(ws)
10
+ max_value_length = @data.max_value_length
11
+
12
+ ws.add_row([@data.key_header[0], 'Outline Level'].concat(
13
+ HTOTConv::Util.pad_array(@data.value_header, max_value_length)),
14
+ :style => Axlsx::STYLE_THIN_BORDER)
15
+
16
+ @data.item.each do |item|
17
+ ws.add_row([item.key, item.level.to_i].concat(
18
+ HTOTConv::Util.pad_array(item.value, max_value_length)),
19
+ :style => Axlsx::STYLE_THIN_BORDER)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end