nbf_tools 0.1.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 023b1e16f83687108652237e0e32b8a7e1ad942c4d0fbedf06f9a3845353d809
4
- data.tar.gz: 9c35361b95ea7e3e6857cac1fdd6288abb3579e4e1bb98f1f8eff84447735228
3
+ metadata.gz: de5e17d1820e018c6dc5e9af02592540a633f591d394f379e3728c1a8895195d
4
+ data.tar.gz: 0f41f05769c1e740f56e44ca316604b8aaf485852659c9b661cbb8f86013ee6a
5
5
  SHA512:
6
- metadata.gz: 0f8bbe44b621662f646b2ec9befb410ec0b4e9f3e2826295688929ba80734ab11ec965c9566752ffdee4e75bd67472c7e7276d72c6a842f53eba19f8885e9eb3
7
- data.tar.gz: ed8924a1e235f9ea288bf31b2f5def66637f4f603b1533e9f89dd31946bec52a649ce3efd34f25bd10e975a0cf9e1e0292a8f3038e83eb9e006b3b5f25204491
6
+ metadata.gz: 84ff37f2ae40c766b750c0e731bc29f03beb714ba761d0149d379ff2bd1d0798637a8f5888853ef32a5264ada0e83e1d8e329bce48f1fd9c23f6ce94c8127c62
7
+ data.tar.gz: 04b1eda848f324e4e138ef40d8eccd4f60f1556f122b573801a47ce6a2d19d302baa0407bca79b08d24e776748e34225bf88b87ec5089c0940f8342fafc1f0ad
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
- ## [Unreleased]
1
+ # [Changelog]
2
+
3
+ ## [0.3.0] - 2023-10-21
4
+
5
+ - feat: implement command line tools haha `nbf_tools`
6
+
7
+ ## [0.2.1] - 2023-10-11
8
+
9
+ - doc: quote CHANGELOG.md method name
10
+
11
+ ## [0.2.0] - 2023-10-11
12
+
13
+ - feat: Reomve configuration, add parse options
14
+ - feat: change method name, `merge_files_to_one_html` -> `merge_files_to_one_html`, `parse_files_to_one` -> `parse_files_to_one`
15
+
16
+ ## [0.1.1] - 2023-10-06
17
+
18
+ - fix: CONTENT_FORMAT `s` char
2
19
 
3
20
  ## [0.1.0] - 2022-12-17
4
21
 
5
- - Initial release
22
+ - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nbf_tools (0.1.1)
4
+ nbf_tools (0.3.0)
5
5
  nokogiri (~> 1.15)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # NbfTools
2
2
 
3
- NETSCAPE Bookmark file tools.
3
+ NETSCAPE Bookmark file tools, parse and merge multiple files.
4
+
5
+ The file was exported from Chrome, Edge, Firefox or Safari browser.
4
6
 
5
7
  ## Installation
6
8
 
@@ -14,28 +16,63 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
16
 
15
17
  ## Usage
16
18
 
17
- Parse NETSCAPE Bookmark file.
19
+ ### Command
18
20
 
19
- ```ruby
20
- NbfTools.parse bookmark_file_path
21
+ Help.
22
+
23
+ ```sh
24
+ usage: nbf_tools [options]
25
+ -f, --files NETSCAPE Bookmark files, delimit by space
26
+ -o, --out-format output parse result format (default: html), options: json, html
27
+ -t, --personal-toolbar-folder-text custom personal toolbar folder text
28
+
29
+ other options:
30
+ -h, --help print help
31
+ -v, --version print version
21
32
  ```
22
33
 
23
- Merge and parse NETSCAPE Bookmark files.
34
+ Example.
24
35
 
25
- ```ruby
26
- NbfTools.parse_files_in_one bookmark_file_path1, bookmark_file_path2
36
+ ```sh
37
+ # print merged json
38
+ nbf_tools -f bk2.html bk1.html -o json
39
+
40
+ # save merged new html file
41
+ nbf_tools -f bk2.html bk1.html -o json > bk.html
27
42
  ```
28
43
 
29
- Export merge NETSCAPE Bookmark files to one html string.
44
+ ### Code
45
+
46
+ Example.
30
47
 
31
48
  ```ruby
32
- NbfTools.parse_files_in_one_html bookmark_file_path1, bookmark_file_path2
49
+ require "nbf_tools"
50
+
51
+ # parse file
52
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
53
+ NbfTools.parse "bk2.html"
54
+
55
+ # merge and parse multiple files
56
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
57
+ NbfTools.parse_files_to_one "bk1.html", "bk2.html", personal_toolbar_folder_text: "bk"
58
+
59
+ # merge multiple files into one html string
60
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
61
+ NbfTools.merge_files_to_one_html "bk1.html", "bk2.html"
62
+
63
+ # parse and merge operate with options
64
+ # options: `{files: [], personal_toolbar_folder_text: [], out_format: []}`
65
+ # `:files` must present
66
+ # `:personal_toolbar_folder_text` is optional
67
+ # if `:out_format` is `"json"` return json format else retrun return html format
68
+ NbfTools.merge_files_to_one_html(files: %w[bk1.html bk2.html], personal_toolbar_folder_text: [], out_format: %w[json])
33
69
  ```
34
70
 
35
-
36
71
  ## Development
37
72
 
38
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+ After checking out the repo, run `bin/setup` to install dependencies.
74
+ Then, run `rake test` to run the tests.
75
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
76
 
40
77
  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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
78
 
data/exe/nbf_tools ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $stdout.sync = true
5
+
6
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
7
+
8
+ require "nbf_tools"
9
+
10
+ puts NbfTools.operate_with_options(NbfTools::Options.new(ARGV).parse)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "type"
4
+
3
5
  module NbfTools
4
6
  class HTML
5
7
  CONTENT_FORMAT = "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<!-- This is an automatically generated file.\n It will be read and overwritten.\n DO NOT EDIT! -->\n<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n<TITLE>Bookmarks</TITLE>\n<H1>Bookmarks</H1>\n<DL><p>\n%{content}\n</DL><p>"
@@ -22,9 +24,9 @@ module NbfTools
22
24
  return if data.nil?
23
25
  data.map do |e|
24
26
  case e["type"]
25
- when NbfTools.config.link_type_text
27
+ when NbfTools::Type::LINK
26
28
  link_html(e)
27
- when NbfTools.config.folder_type_text
29
+ when NbfTools::Type::FOLDER
28
30
  folder_html(e)
29
31
  end
30
32
  end.join("\n")
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "type"
4
+
3
5
  module NbfTools
4
6
  class Merge
5
7
  def initialize(data)
@@ -35,9 +37,9 @@ module NbfTools
35
37
  def flat_folder_and_link_map(data, link_map, folder_map)
36
38
  data.map do |e|
37
39
  case e["type"]
38
- when NbfTools.config.link_type_text
40
+ when NbfTools::Type::LINK
39
41
  choose_latest_element(link_map, "href", e)
40
- when NbfTools.config.folder_type_text
42
+ when NbfTools::Type::FOLDER
41
43
  choose_latest_element(folder_map, "path", e)
42
44
  flat_folder_and_link_map(e["items"], link_map, folder_map)
43
45
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NbfTools
4
+ class Options
5
+ COMMAND_HELP_TEXT = <<~TEXT.chomp
6
+ usage: nbf_tools [options]
7
+ -f, --files NETSCAPE Bookmark files, delimit by space
8
+ -o, --out-format output parse result format (default: html), options: json, html
9
+ -t, --personal-toolbar-folder-text custom personal toolbar folder text
10
+
11
+ other options:
12
+ -h, --help print help
13
+ -v, --version print version
14
+ TEXT
15
+
16
+ def initialize(argv)
17
+ @argv = argv
18
+ end
19
+
20
+ def parse
21
+ opts = {unknown_option: [], files: [], personal_toolbar_folder_text: [], out_format: []}
22
+ option_key = :unknown_option
23
+
24
+ @argv.each do |e|
25
+ case e
26
+ when "-h", "--help"
27
+ puts COMMAND_HELP_TEXT
28
+ exit
29
+ when "-v", "--version"
30
+ puts NbfTools::VERSION
31
+ exit
32
+ when "-f", "--files"
33
+ option_key = :files
34
+ when "-o", "--out_format"
35
+ option_key = :out_format
36
+ when "-t", "--personal_toolbar_folder_text"
37
+ option_key = :personal_toolbar_folder_text
38
+ when /\A-.+\z/
39
+ option_key = :unknown_option
40
+ warn "unknown option #{e}"
41
+ else
42
+ opts[option_key] << e
43
+ end
44
+ end
45
+
46
+ opts
47
+ end
48
+ end
49
+ end
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "nokogiri"
4
+ require_relative "type"
5
+
4
6
  module NbfTools
5
7
  class Parse
6
- def initialize file
7
- @document = Nokogiri::HTML(File.open(file))
8
+ def initialize options = {}
9
+ @options = options
8
10
  end
9
11
 
10
- def parse
12
+ def parse file
13
+ @document = Nokogiri::HTML(File.open(file))
11
14
  parse_node_children(@document.xpath("/html/body/dl").first, Pathname.new("/"))
12
15
  end
13
16
 
@@ -17,18 +20,26 @@ module NbfTools
17
20
  when "dt"
18
21
  items.push(*parse_node_children(c, path))
19
22
  when "a"
20
- bookmark = c.to_h.merge("text" => c.text, "type" => NbfTools.config.link_type_text, "path" => path.to_s)
23
+ bookmark = c.to_h.merge("text" => c.text, "type" => NbfTools::Type::LINK, "path" => path.to_s)
21
24
  items.push bookmark
22
25
  when "h3"
23
- folder_name = c.text
24
- folder_name = NbfTools.config.personal_toolbar_folder_text if c["personal_toolbar_folder"] == "true"
25
- folder = c.to_h.merge("text" => folder_name, "type" => NbfTools.config.folder_type_text, "path" => (path + folder_name).to_s, "items" => [])
26
+ text = folder_text(c)
27
+ folder = c.to_h.merge("text" => text, "type" => NbfTools::Type::FOLDER, "path" => (path + text).to_s, "items" => [])
26
28
  items.push folder
27
29
  when "dl"
28
- folder = items.select { |e| e["type"] == "folder" }.last
30
+ folder = items.reverse.find { |e| e["type"] == "folder" }
29
31
  folder.nil? ? items.push(*parse_node_children(c), path) : folder["items"] = parse_node_children(c, path + folder["text"])
30
32
  end
31
33
  end
32
34
  end
35
+
36
+ def folder_text item
37
+ return item.text unless item["personal_toolbar_folder"] == "true"
38
+ if @options[:personal_toolbar_folder_text].to_s.empty?
39
+ @options[:personal_toolbar_folder_text] = item.text
40
+ else
41
+ @options[:personal_toolbar_folder_text]
42
+ end
43
+ end
33
44
  end
34
45
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NbfTools
4
+ module Type
5
+ LINK = "link"
6
+ FOLDER = "folder"
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NbfTools
4
- VERSION = "0.1.1"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/nbf_tools.rb CHANGED
@@ -1,45 +1,54 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "nbf_tools/configuration"
4
3
  require_relative "nbf_tools/html"
5
4
  require_relative "nbf_tools/merge"
6
5
  require_relative "nbf_tools/parse"
6
+ require_relative "nbf_tools/options"
7
7
  require_relative "nbf_tools/version"
8
8
 
9
+ require "json"
10
+
9
11
  module NbfTools
10
12
  class Error < StandardError; end
11
- # Your code goes here...
12
13
 
13
14
  class << self
14
- def config
15
- return @config if defined?(@config)
16
-
17
- @config = Configuration.new
18
- @config.personal_toolbar_folder_text = "personal toolbar"
19
- @config.link_type_text = "link"
20
- @config.folder_type_text = "folder"
21
-
22
- @config
23
- end
24
15
 
25
- def configure(&block)
26
- config.instance_exec(&block)
16
+ # parse file
17
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
18
+ def parse(file, **options)
19
+ Parse.new(**options).parse(file)
27
20
  end
28
21
 
29
- def parse(file)
30
- Parse.new(file).parse
31
- end
32
-
33
- def parse_files_in_one(*files)
34
- data = files.map { |file| parse(file) }
22
+ # merge and parse multiple files
23
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
24
+ def parse_files_to_one(*files, **options)
25
+ parse_tool = Parse.new(**options)
26
+ data = files.map { |file| parse_tool.parse(file) }
35
27
  return data[0] if files.size < 2
36
28
  Merge.new(data).to_a
37
29
  end
38
30
 
39
- def parse_files_in_one_html(*files)
31
+ # merge multiple files into one html string
32
+ # options: `{personal_toolbar_folder_text: "some string"}`, is optional
33
+ def merge_files_to_one_html(*files, **options)
40
34
  return File.read(files[0]) if files.size < 2
41
- data = parse_files_in_one(*files)
35
+ data = parse_files_to_one(*files, **options)
42
36
  HTML.new(data).to_s
43
37
  end
38
+
39
+ # parse and merge operate with options
40
+ # options: `{files: [], personal_toolbar_folder_text: [], out_format: []}`
41
+ # `:files` must present
42
+ # `:personal_toolbar_folder_text` is optional
43
+ # if `:out_format` is `"json"` return json format else retrun return html format
44
+ def operate_with_options(opts)
45
+ opts[:out_format] ||= []
46
+ opts[:personal_toolbar_folder_text] = [opts[:personal_toolbar_folder_text]] if opts[:personal_toolbar_folder_text].is_a?(Array)
47
+ if opts[:out_format].join(" ") == "json"
48
+ parse_files_to_one(*opts[:files], personal_toolbar_folder_text: opts[:personal_toolbar_folder_text].join(" ")).to_json
49
+ else
50
+ merge_files_to_one_html(*opts[:files], personal_toolbar_folder_text: opts[:personal_toolbar_folder_text].join(" "))
51
+ end
52
+ end
44
53
  end
45
54
  end
data/nbf_tools.gemspec CHANGED
@@ -8,10 +8,11 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Sapeu"]
9
9
  spec.email = ["sapeu.lee@foxmail.com"]
10
10
 
11
- spec.summary = "NETSCAPE Bookmark file tools."
12
- spec.description = "NETSCAPE Bookmark file tools."
11
+ spec.summary = "NETSCAPE Bookmark file tools, parse and merge multiple files."
12
+ spec.description = "NETSCAPE Bookmark file tools, parse file and merge multiple files.\nThe file was exported from Chrome, Edge, Firefox or Safari browser."
13
13
  spec.homepage = "https://github.com/sapeu/nbf_tools"
14
14
  spec.required_ruby_version = ">= 2.7.0"
15
+ spec.license = "MIT"
15
16
 
16
17
  # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nbf_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sapeu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-06 00:00:00.000000000 Z
11
+ date: 2023-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -38,10 +38,13 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.31'
41
- description: NETSCAPE Bookmark file tools.
41
+ description: |-
42
+ NETSCAPE Bookmark file tools, parse file and merge multiple files.
43
+ The file was exported from Chrome, Edge, Firefox or Safari browser.
42
44
  email:
43
45
  - sapeu.lee@foxmail.com
44
- executables: []
46
+ executables:
47
+ - nbf_tools
45
48
  extensions: []
46
49
  extra_rdoc_files: []
47
50
  files:
@@ -51,16 +54,19 @@ files:
51
54
  - Gemfile.lock
52
55
  - README.md
53
56
  - Rakefile
57
+ - exe/nbf_tools
54
58
  - lib/nbf_tools.rb
55
- - lib/nbf_tools/configuration.rb
56
59
  - lib/nbf_tools/html.rb
57
60
  - lib/nbf_tools/merge.rb
61
+ - lib/nbf_tools/options.rb
58
62
  - lib/nbf_tools/parse.rb
63
+ - lib/nbf_tools/type.rb
59
64
  - lib/nbf_tools/version.rb
60
65
  - nbf_tools.gemspec
61
66
  - sig/nbf_tools.rbs
62
67
  homepage: https://github.com/sapeu/nbf_tools
63
- licenses: []
68
+ licenses:
69
+ - MIT
64
70
  metadata:
65
71
  homepage_uri: https://github.com/sapeu/nbf_tools
66
72
  source_code_uri: https://github.com/sapeu/nbf_tools
@@ -83,5 +89,5 @@ requirements: []
83
89
  rubygems_version: 3.4.10
84
90
  signing_key:
85
91
  specification_version: 4
86
- summary: NETSCAPE Bookmark file tools.
92
+ summary: NETSCAPE Bookmark file tools, parse and merge multiple files.
87
93
  test_files: []
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NbfTools
4
- class Configuration
5
- # personal toolbar folder text, default: ''
6
- attr_accessor :personal_toolbar_folder_text
7
- # link type text, default: 'link'
8
- attr_accessor :link_type_text
9
- # folder type text, default: 'folder'
10
- attr_accessor :folder_type_text
11
- end
12
- end