htot_conv 0.3.1 → 0.3.2

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.
@@ -1,70 +1,70 @@
1
- # frozen_string_literal: true
2
- require 'htot_conv/parser/base'
3
-
4
- module HTOTConv
5
- module Parser
6
- class Opml < Base
7
- def self.option_help
8
- {
9
- :key_header => {
10
- :default => [],
11
- :pat => Array,
12
- :desc => "key header",
13
- },
14
- }
15
- end
16
-
17
- def parse(input)
18
- outline = HTOTConv::Outline.new
19
- outline.key_header = @option[:key_header]
20
- outline.value_header = []
21
-
22
- parser = Nokogiri::XML::SAX::Parser.new(ListDoc.new(outline))
23
- parser.parse(input)
24
-
25
- outline
26
- end
27
-
28
- class ListDoc < Nokogiri::XML::SAX::Document
29
- def initialize(outline)
30
- @outline = outline
31
- @breadcrumb = []
32
- end
33
-
34
- def start_element(name, attrs=[])
35
- if (name == "outline")
36
- @breadcrumb << name
37
- generate_outline_item(attrs)
38
- end
39
- end
40
-
41
- def end_element(name)
42
- @breadcrumb.pop if (name == "outline")
43
- end
44
-
45
- private
46
- def generate_outline_item(attrs)
47
- text = ""
48
- level = @breadcrumb.length
49
- values = []
50
- attrs.each do |pair|
51
- attr_name, attr_val = pair
52
- if attr_name == "text"
53
- text = attr_val
54
- else
55
- unless @outline.value_header.include?(attr_name)
56
- @outline.value_header << attr_name
57
- values[@outline.value_header.length - 1] = attr_val
58
- else
59
- values[@outline.value_header.index(attr_name)] = attr_val
60
- end
61
- end
62
- end
63
-
64
- @outline.add_item(text, level, values)
65
- end
66
- end
67
-
68
- end
69
- end
70
- end
1
+ # frozen_string_literal: true
2
+ require 'htot_conv/parser/base'
3
+
4
+ module HTOTConv
5
+ module Parser
6
+ class Opml < Base
7
+ def self.option_help
8
+ {
9
+ :key_header => {
10
+ :default => [],
11
+ :pat => Array,
12
+ :desc => "key header",
13
+ },
14
+ }
15
+ end
16
+
17
+ def parse(input)
18
+ outline = HTOTConv::Outline.new
19
+ outline.key_header = @option[:key_header]
20
+ outline.value_header = []
21
+
22
+ parser = Nokogiri::XML::SAX::Parser.new(ListDoc.new(outline))
23
+ parser.parse(input)
24
+
25
+ outline
26
+ end
27
+
28
+ class ListDoc < Nokogiri::XML::SAX::Document
29
+ def initialize(outline)
30
+ @outline = outline
31
+ @breadcrumb = []
32
+ end
33
+
34
+ def start_element(name, attrs=[])
35
+ if (name == "outline")
36
+ @breadcrumb << name
37
+ generate_outline_item(attrs)
38
+ end
39
+ end
40
+
41
+ def end_element(name)
42
+ @breadcrumb.pop if (name == "outline")
43
+ end
44
+
45
+ private
46
+ def generate_outline_item(attrs)
47
+ text = ""
48
+ level = @breadcrumb.length
49
+ values = []
50
+ attrs.each do |pair|
51
+ attr_name, attr_val = pair
52
+ if attr_name == "text"
53
+ text = attr_val
54
+ else
55
+ unless @outline.value_header.include?(attr_name)
56
+ @outline.value_header << attr_name
57
+ values[@outline.value_header.length - 1] = attr_val
58
+ else
59
+ values[@outline.value_header.index(attr_name)] = attr_val
60
+ end
61
+ end
62
+ end
63
+
64
+ @outline.add_item(text, level, values)
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -1,70 +1,70 @@
1
- # frozen_string_literal: true
2
- require 'htot_conv/parser/base'
3
-
4
- module HTOTConv
5
- module Parser
6
- class SimpleText < Base
7
- def self.option_help
8
- {
9
- :indent => {
10
- :default => "\t",
11
- :pat => String,
12
- :desc => "indent character (default: TAB)",
13
- },
14
- :delimiter => {
15
- :default => nil,
16
- :pat => String,
17
- :desc => "separator character of additional data",
18
- },
19
- :preserve_empty_line => {
20
- :default => false,
21
- :pat => FalseClass,
22
- :desc => "preserve empty line as a level-1 item (default: no)",
23
- },
24
- :key_header => {
25
- :default => [],
26
- :pat => Array,
27
- :desc => "key header",
28
- },
29
- :value_header => {
30
- :default => [],
31
- :pat => Array,
32
- :desc => "value header",
33
- },
34
- }
35
- end
36
-
37
- def parse(input)
38
- indent_regexp = Regexp.new("^(?<indents>(#{Regexp.escape(@option[:indent])})*)")
39
- delimiter_regexp = (@option[:delimiter].kind_of?(String))? Regexp.new(Regexp.escape(@option[:delimiter])) : @option[:delimiter]
40
- outline = HTOTConv::Outline.new
41
- outline.key_header = @option[:key_header]
42
- outline.value_header = @option[:value_header]
43
-
44
- input.each_line do |line|
45
- next if ((line.chomp == "") && (!@option[:preserve_empty_line]))
46
-
47
- level = 1
48
- value = []
49
- if (@option[:indent] || '').length > 0
50
- indents = indent_regexp.match(line)[:indents]
51
- level = 1 + indents.length / @option[:indent].length
52
- line = line.sub(indent_regexp, "")
53
- end
54
-
55
- line = line.strip
56
- if delimiter_regexp
57
- key = line.split(delimiter_regexp)[0]
58
- value = line.split(delimiter_regexp)[1..-1] || []
59
- else
60
- key = line
61
- end
62
-
63
- outline.add_item(key, level, value)
64
- end
65
-
66
- outline
67
- end
68
- end
69
- end
70
- end
1
+ # frozen_string_literal: true
2
+ require 'htot_conv/parser/base'
3
+
4
+ module HTOTConv
5
+ module Parser
6
+ class SimpleText < Base
7
+ def self.option_help
8
+ {
9
+ :indent => {
10
+ :default => "\t",
11
+ :pat => String,
12
+ :desc => "indent character (default: TAB)",
13
+ },
14
+ :delimiter => {
15
+ :default => nil,
16
+ :pat => String,
17
+ :desc => "separator character of additional data",
18
+ },
19
+ :preserve_empty_line => {
20
+ :default => false,
21
+ :pat => FalseClass,
22
+ :desc => "preserve empty line as a level-1 item (default: no)",
23
+ },
24
+ :key_header => {
25
+ :default => [],
26
+ :pat => Array,
27
+ :desc => "key header",
28
+ },
29
+ :value_header => {
30
+ :default => [],
31
+ :pat => Array,
32
+ :desc => "value header",
33
+ },
34
+ }
35
+ end
36
+
37
+ def parse(input)
38
+ indent_regexp = Regexp.new("^(?<indents>(#{Regexp.escape(@option[:indent])})*)")
39
+ delimiter_regexp = (@option[:delimiter].kind_of?(String))? Regexp.new(Regexp.escape(@option[:delimiter])) : @option[:delimiter]
40
+ outline = HTOTConv::Outline.new
41
+ outline.key_header = @option[:key_header]
42
+ outline.value_header = @option[:value_header]
43
+
44
+ input.each_line do |line|
45
+ next if ((line.chomp == "") && (!@option[:preserve_empty_line]))
46
+
47
+ level = 1
48
+ value = []
49
+ if (@option[:indent] || '').length > 0
50
+ indents = indent_regexp.match(line)[:indents]
51
+ level = 1 + indents.length / @option[:indent].length
52
+ line = line.sub(indent_regexp, "")
53
+ end
54
+
55
+ line = line.strip
56
+ if delimiter_regexp
57
+ key = line.split(delimiter_regexp)[0]
58
+ value = line.split(delimiter_regexp)[1..-1] || []
59
+ else
60
+ key = line
61
+ end
62
+
63
+ outline.add_item(key, level, value)
64
+ end
65
+
66
+ outline
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,27 +1,27 @@
1
- # frozen_string_literal: true
2
- require 'htot_conv/parser/base.rb'
3
- require 'htot_conv/parser/simple_text.rb'
4
- require 'htot_conv/parser/dir_tree.rb'
5
- require 'htot_conv/parser/html_list.rb'
6
- require 'htot_conv/parser/opml.rb'
7
-
8
- module HTOTConv
9
- module Parser
10
- def create(type, *args)
11
- klass = HTOTConv::Parser.const_get(Rinne.camelize(type.to_s))
12
- klass.new(*args)
13
- end
14
- module_function :create
15
-
16
- def types
17
- HTOTConv::Parser.constants.reject { |klass|
18
- klass =~ /Base$/
19
- }.select { |klass|
20
- HTOTConv::Parser.const_get(klass).kind_of?(Class)
21
- }.map { |klass|
22
- Rinne.to_snake(klass.to_s).to_sym
23
- }
24
- end
25
- module_function :types
26
- end
27
- end
1
+ # frozen_string_literal: true
2
+ require 'htot_conv/parser/base.rb'
3
+ require 'htot_conv/parser/simple_text.rb'
4
+ require 'htot_conv/parser/dir_tree.rb'
5
+ require 'htot_conv/parser/html_list.rb'
6
+ require 'htot_conv/parser/opml.rb'
7
+
8
+ module HTOTConv
9
+ module Parser
10
+ def create(type, *args)
11
+ klass = HTOTConv::Parser.const_get(Rinne.camelize(type.to_s))
12
+ klass.new(*args)
13
+ end
14
+ module_function :create
15
+
16
+ def types
17
+ HTOTConv::Parser.constants.reject { |klass|
18
+ klass =~ /Base$/
19
+ }.select { |klass|
20
+ HTOTConv::Parser.const_get(klass).kind_of?(Class)
21
+ }.map { |klass|
22
+ Rinne.to_snake(klass.to_s).to_sym
23
+ }
24
+ end
25
+ module_function :types
26
+ end
27
+ end
@@ -1,13 +1,13 @@
1
- # frozen_string_literal: true
2
-
3
- module HTOTConv
4
- module Util
5
- def pad_array(array, length, pad=nil)
6
- raise ArgumentError, "array is not an array" unless array.kind_of?(Array)
7
- raise ArgumentError, "array length #{array.length} is larger than #{length}" if array.length > length
8
-
9
- array.concat(Array.new(length - array.length, pad))
10
- end
11
- module_function :pad_array
12
- end
13
- end
1
+ # frozen_string_literal: true
2
+
3
+ module HTOTConv
4
+ module Util
5
+ def pad_array(array, length, pad=nil)
6
+ raise ArgumentError, "array is not an array" unless array.kind_of?(Array)
7
+ raise ArgumentError, "array length #{array.length} is larger than #{length}" if array.length > length
8
+
9
+ array.concat(Array.new(length - array.length, pad))
10
+ end
11
+ module_function :pad_array
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
2
- module HTOTConv
3
- VERSION = "0.3.1"
4
- end
1
+ # frozen_string_literal: true
2
+ module HTOTConv
3
+ VERSION = "0.3.2"
4
+ end
data/lib/htot_conv.rb CHANGED
@@ -1,20 +1,20 @@
1
- # frozen_string_literal: true
2
- require 'htot_conv/version'
3
-
4
- require 'htot_conv/util'
5
- require 'htot_conv/outline'
6
- require 'htot_conv/generator'
7
- require 'htot_conv/parser'
8
- # require 'htot_conv/cli'
9
-
10
- module HTOTConv
11
-
12
- def convert(input, input_type, output, output_type, input_option={}, output_option={})
13
- parser = HTOTConv::Parser.create(input_type, input_option)
14
- outline = parser.parse(input)
15
- generator = HTOTConv::Generator.create(output_type, outline, output_option)
16
- generator.output(output)
17
- end
18
- module_function :convert
19
-
20
- end
1
+ # frozen_string_literal: true
2
+ require 'htot_conv/version'
3
+
4
+ require 'htot_conv/util'
5
+ require 'htot_conv/outline'
6
+ require 'htot_conv/generator'
7
+ require 'htot_conv/parser'
8
+ # require 'htot_conv/cli'
9
+
10
+ module HTOTConv
11
+
12
+ def convert(input, input_type, output, output_type, input_option={}, output_option={})
13
+ parser = HTOTConv::Parser.create(input_type, input_option)
14
+ outline = parser.parse(input)
15
+ generator = HTOTConv::Generator.create(output_type, outline, output_option)
16
+ generator.output(output)
17
+ end
18
+ module_function :convert
19
+
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htot_conv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@cat_in_136"
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-03 00:00:00.000000000 Z
11
+ date: 2017-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: axlsx
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  requirements: []
153
153
  rubyforge_project:
154
- rubygems_version: 2.5.2.1
154
+ rubygems_version: 2.5.2
155
155
  signing_key:
156
156
  specification_version: 4
157
157
  summary: Hierarchical-Tree Outline Text Converter