undrive_google 1.0.0 → 1.0.1

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: 6309df259e4c0ec3a6853a2e2362a01a1ae21c92f9cf02254727318c084b3f47
4
- data.tar.gz: ff5130695837c720b1ead438f3cfbcebc903ea8da2f78e3e9007295055984bdb
3
+ metadata.gz: 34e0ef0495f11487f7c44caea53d6995a5b7a1a7166a0348c3c35fa3d7612f24
4
+ data.tar.gz: 631433cd7bb255aba19f769c1298254795bffa9102a29215bcc0c67916cf11d0
5
5
  SHA512:
6
- metadata.gz: df150a3aa25129d41bf43889b635627c03a33ce68884653de461e039595a0c7fa7592d0f96edd63287e4a5a834412c42b333d5e0ac1ec6dbb44858434493616a
7
- data.tar.gz: 1e850de4f83df8be4b09cea304fde2448af115106767afd4241b049f8cf9347dcd1dc1e6936d15dc83fa789573bd7fe8855923945967d2c78f4c89b4152a7313
6
+ metadata.gz: 31e36fd65a3c14b6cf39bd6e8eac32a056adae91402d3106b67d57feff3874c7b4e27418534ac3582f5cc1c7f8ae04a908594f9b2083ff332463690f19e305cc
7
+ data.tar.gz: bd3b2f07174026896b8ff3d70e3f0ec6eda20574017caf77e3098940a0d8fa6dcf2a85cf4e843dc0492d3756b9e76f14407f2c78ac831f98bfd350e700f073aa
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -10,5 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
  ### Fixed
11
11
  ### Removed
12
12
 
13
- ## 1.0.0 - 2022-10-24
13
+ ## 1.0.1 - 2022-10-26
14
+ ### Added
15
+ - Support for lang
16
+ - Support for title
17
+ - Support for unzipping zip
18
+ - Support for deleting zip
19
+ - Support for Renaming HTML
20
+
21
+ ## 1.0.0 - 2022-10-25
14
22
  - Initial release
data/README.md CHANGED
@@ -14,15 +14,30 @@ If yes, DO NOT use this gem. Instead follow [these instructions](https://support
14
14
  OTOH, if you want to "own", host, track, etc your 'own' files
15
15
  (e.g. your resume), DO use this gem.
16
16
 
17
- ## NOTE: Export epub
17
+ ## NOTE: Export epub & Unzip html
18
18
 
19
19
  There is a bug (missing feature) in `google_drive` gem preventing export of epub. You can use this patched branch:
20
20
 
21
+ Create a Gemfile:
21
22
  ```ruby
23
+ source "https://rubygems.org"
24
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
22
25
  gem "google_drive", github: "pboling/google-drive-ruby", branch: "pboling-epub-mimetype"
23
26
  ```
24
27
 
25
- And you can upvote this PR [#427](https://github.com/gimite/google-drive-ruby/pull/427).
28
+ Please upvote this PR [#427](https://github.com/gimite/google-drive-ruby/pull/427)!
29
+
30
+ Separately, the `rubyzip` maintainers are now working on v3, and this gem utilizes that syntax. But it hasn't been released yet.
31
+ Therefore, if you need to unzip to HTML add another line to the `Gemfile` from above:
32
+ ```ruby
33
+ gem "rubyzip", github: "rubyzip/rubyzip", branch: "master"
34
+ ```
35
+
36
+ When liberating your files, ensure the script will use the Gemfile:
37
+ ```shell
38
+ BUNDLE_GEMFILE=path/to/Gemfile bundle update
39
+ BUNDLE_GEMFILE=path/to/Gemfile undrive_google -c path/to/config
40
+ ```
26
41
 
27
42
  ## Story Time
28
43
 
@@ -35,7 +50,7 @@ Tell me if you've heard this one already.
35
50
  1. Give 🐭 your 🍪 for "safe-keeping"
36
51
  2. Recognizing this SPoF, you ask 🐭 to give back a 🍪 copy
37
52
  3. "I'll run it through my 🍪 🖨", says 🐭
38
- 4. 🖨 replicates various 🍪 extensions: `pdf`, `odt`, `docx`, `txt`, `rtf` and `epub`
53
+ 4. 🖨 replicates various 🍪 extensions: `pdf`, `odt`, `docx`, `txt`, `rtf`, `zip`, and `epub`
39
54
  5. Rename 🍪 for web (e.g. replace ` ` with `_`)
40
55
  6. Extract replicated `.zip` format to `.html`
41
56
  7. Rename extracted HTML file for self-hosting
@@ -47,12 +62,9 @@ Tell me if you've heard this one already.
47
62
  13. Bake a new 🍪
48
63
  14. GOTO 1
49
64
 
50
- This gem solves the classic 🐭-🍪 problem by automating steps 3-8.
65
+ This gem solves the classic 🐭-🍪 problem by automating steps 3-9.
51
66
  Will save at least 15 minutes each loop.
52
67
 
53
- TODO:
54
- Configuration supports step 9, but it hasn't been implemented yet.
55
-
56
68
  Note that it doesn't have to be a resume.
57
69
  There are likely other use cases that apply.
58
70
 
@@ -131,7 +143,8 @@ keep_zip: true
131
143
 
132
144
  # Rename downloaded files following a pattern?
133
145
  # Only applies to files not explicitly specified with rename-<type>
134
- # Value will be splatted to gsub
146
+ # Will never apply to the html file unzipped from the .zip
147
+ # Used as: file_name.gsub(rename_pattern[0], rename_pattern[1])
135
148
  rename_pattern:
136
149
  - "_"
137
150
  - " "
@@ -141,13 +154,10 @@ dir: '' # defaults to current working directory
141
154
 
142
155
  verbose: true # or false
143
156
 
144
- #
145
- # TODO: implementation for title and lang
146
- #
147
- # [String] HTML title element inner text.
157
+ # [String] Set inner text of missing title element, if unzipping `.zip` to `.html`.
148
158
  title: '' # When empty, defaults to the title of the file.
149
159
 
150
- # [String] If unzipping `.zip`, adds lang attribute to html tag,
160
+ # [String] Adds lang attribute to html tag, If unzipping `.zip` to `.html`,
151
161
  # value is like `fr`, `es`, etc.
152
162
  lang: 'en'
153
163
  ```
data/exe/undrive_google CHANGED
@@ -1,12 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ DEBUG = ENV["UNDRIVE_DEBUG"] == "true"
5
+
4
6
  # Std-lib
5
7
  require "optparse"
6
8
 
7
9
  # Gems
8
10
  require "bundler/setup"
9
- require "byebug"
11
+ require "byebug" if DEBUG
10
12
 
11
13
  # This library
12
14
  require "undrive_google"
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Actions
5
+ # Liberate a file, and transform it
6
+ class Liberate
7
+ extend Forwardable
8
+
9
+ attr_accessor :extension, :tr, :dir, :file, :file_path
10
+
11
+ def_delegator :@tr, :download
12
+ def_delegator :@tr, :unzip
13
+ def_delegator :@tr, :rename_html
14
+ def_delegator :@tr, :fix_html
15
+ def_delegator :@tr, :delete_zip
16
+ def_delegator :@tr, :liberate!
17
+
18
+ def initialize(extension)
19
+ @extension = extension.to_s
20
+ @file = Session.instance.file
21
+ @dir = Options.instance.dir
22
+ @file_path = "#{@dir}/#{name}"
23
+ @tr = Transformation.new(download: Transformations::Download.new(@file_path, @extension))
24
+ return unless zip?
25
+
26
+ if Options.instance.unzip
27
+ @tr.unzip = Transformations::Unzip.new(@file_path)
28
+ @tr.fix_html = Transformations::FixHtml.new(@tr.unzip)
29
+ @tr.rename_html = Transformations::RenameHtml.new(@tr.unzip) if Options.instance.rename[:html]
30
+ end
31
+
32
+ @tr.delete_zip = Transformations::DeleteZip.new(@file_path) unless Options.instance.keep_zip
33
+ end
34
+
35
+ private
36
+
37
+ def name
38
+ "#{rename}.#{extension}"
39
+ end
40
+
41
+ def rename
42
+ exact_name = Options.instance.rename[extension]
43
+ return exact_name if exact_name
44
+
45
+ Options.instance.rename_proc.call(file.title)
46
+ end
47
+
48
+ # The zip is an HTML file packaged in a .zip archive
49
+ def zip?
50
+ extension == "zip"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -10,9 +10,9 @@ module UndriveGoogle
10
10
  def liberate!
11
11
  @exports = []
12
12
  extensions.each do |format|
13
- download = Actions::Download.new(format)
14
- exports << download
15
- download.download!
13
+ liberate = Actions::Liberate.new(format)
14
+ exports << liberate.file_path
15
+ liberate.liberate!
16
16
  end
17
17
 
18
18
  nil
@@ -25,8 +25,14 @@ module UndriveGoogle
25
25
  end
26
26
 
27
27
  def liberate!
28
- session.file
28
+ Options.instance.title = session.file.title if missing_title?
29
29
  CaptiveFile.instance.liberate!
30
30
  end
31
+
32
+ private
33
+
34
+ def missing_title?
35
+ Options.instance.title.nil? || Options.instance.title.empty?
36
+ end
31
37
  end
32
38
  end
@@ -10,18 +10,7 @@ module UndriveGoogle
10
10
  :dir, :verbose
11
11
 
12
12
  def initialize
13
- self.config_yaml = CONFIG_YAML_PATH
14
- self.key_file = KEY_FILE_PATH
15
- self.file_id = nil
16
- self.file_by = :key
17
- self.extensions = FILE_TYPES # On the command line, "all" expands to FILE_TYPES
18
- self.unzip, self.keep_zip = true
19
- self.rename = {}
20
- self.rename_proc = RENAME_PROC
21
- self.title, self.dir = nil
22
- self.lang = "en"
23
- self.verbose = false
24
- load_yaml
13
+ load_yaml(true)
25
14
  end
26
15
 
27
16
  def define_options(parser)
@@ -125,13 +114,13 @@ module UndriveGoogle
125
114
  end
126
115
 
127
116
  def lang_option(parser)
128
- parser.on("-l", "--lang LANG", String, '[NOOP] TODO: Add lang="LANG" attribute to HTML') do |lang|
117
+ parser.on("-l", "--lang LANG", String, 'Add lang="LANG" attribute to <html> tag of unzipped HTML') do |lang|
129
118
  self.lang = lang
130
119
  end
131
120
  end
132
121
 
133
122
  def title_option(parser)
134
- parser.on("-t", "--title TITLE", String, "[NOOP] TODO: Add <title>TITLE</title> element to HTML") do |title|
123
+ parser.on("-t", "--title TITLE", String, "Add <title>TITLE</title> element to unzipped HTML") do |title|
135
124
  self.title = title
136
125
  end
137
126
  end
@@ -143,8 +132,22 @@ module UndriveGoogle
143
132
  end
144
133
  end
145
134
 
146
- def load_yaml
147
- return unless config_yaml
135
+ def load_yaml(reset = false)
136
+ if reset
137
+ self.config_yaml = CONFIG_YAML_PATH
138
+ self.key_file = KEY_FILE_PATH
139
+ self.file_id = nil
140
+ self.file_by = :key
141
+ self.extensions = FILE_TYPES # On the command line, "all" expands to FILE_TYPES
142
+ self.unzip = true
143
+ self.keep_zip = true
144
+ self.rename = {}
145
+ self.rename_proc = RENAME_PROC
146
+ self.title = nil
147
+ self.dir = nil
148
+ self.lang = "en"
149
+ self.verbose = false
150
+ end
148
151
 
149
152
  cf = ConfigFile.new(config_yaml)
150
153
  return unless cf.any?
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ class Transformation < Struct.new(:download,
5
+ :unzip,
6
+ :fix_html,
7
+ :rename_html,
8
+ :delete_zip,
9
+ keyword_init: true)
10
+ def liberate!
11
+ each(&:process)
12
+ end
13
+
14
+ def each
15
+ yield download unless download.nil?
16
+ yield unzip unless unzip.nil?
17
+ yield fix_html unless fix_html.nil?
18
+ # Rename after fix to prevent confusion about the current path of the file
19
+ yield rename_html unless rename_html.nil?
20
+ yield delete_zip unless delete_zip.nil?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Transformations
5
+ # Delete the downloaded zip file.
6
+ class DeleteZip
7
+ attr_accessor :file_path
8
+
9
+ def initialize(file_path)
10
+ @file_path = file_path
11
+ end
12
+
13
+ # @return nil
14
+ def process
15
+ puts "Deleting #{file_path}" if Options.instance.verbose
16
+ File.delete(file_path)
17
+
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Transformations
5
+ # Download a particular version of the file.
6
+ class Download
7
+ attr_accessor :file, :extension, :file_path
8
+
9
+ def initialize(file_path, extension)
10
+ @extension = extension
11
+ @file = Session.instance.file
12
+ @file_path = file_path
13
+ end
14
+
15
+ # @return nil
16
+ def process
17
+ puts "Downloading #{file.title} as: #{extension} to: #{file_path}" if Options.instance.verbose
18
+ file.export_as_file(file_path, extension)
19
+
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Transformations
5
+ # In Google Doc generated HTML, optionally replace:
6
+ #
7
+ # <html><head>
8
+ #
9
+ # with
10
+ #
11
+ # <html lang="en"><head><title>Peter Boling's Bounce to the Ounce</title>
12
+ #
13
+ class FixHtml
14
+ extend Forwardable
15
+
16
+ attr_accessor :unzip, :lang, :title
17
+
18
+ def_delegator :@unzip, :html_path
19
+
20
+ def initialize(unzip)
21
+ @unzip = unzip
22
+ @lang = Options.instance.lang
23
+ @title = Options.instance.title
24
+ end
25
+
26
+ # @return nil
27
+ def process
28
+ return unless lang || title
29
+
30
+ puts "Checking html in #{html_path}" if Options.instance.verbose
31
+ file = File.open(html_path)
32
+ html = file.read
33
+ return unless html
34
+
35
+ puts "Fixing html in #{html_path}" if Options.instance.verbose
36
+ html.sub!("<html>", "<html lang=\"#{lang}\">") if lang
37
+ html.sub!("<head>", "<head><title>#{title}</title>") if title
38
+ File.write(html_path, html)
39
+
40
+ nil
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Transformations
5
+ # Rename the unzipped HTML file.
6
+ class RenameHtml
7
+ extend Forwardable
8
+
9
+ attr_accessor :unzip
10
+
11
+ def_delegator :@unzip, :html_path
12
+
13
+ def initialize(unzip)
14
+ @unzip = unzip
15
+ end
16
+
17
+ # @return nil
18
+ def process
19
+ puts "Renaming html file at #{html_path} to #{rename_path}" if Options.instance.verbose
20
+ File.rename(html_path, rename_path)
21
+
22
+ nil
23
+ end
24
+
25
+ private
26
+
27
+ def rename_path
28
+ "#{File.dirname(html_path)}/#{Options.instance.rename[:html]}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UndriveGoogle
4
+ module Transformations
5
+ # Download a particular version of the file.
6
+ class Unzip
7
+ attr_accessor :file_path, :html_path
8
+
9
+ def initialize(file_path)
10
+ @file_path = file_path
11
+ end
12
+
13
+ # @return nil
14
+ def process
15
+ return unless Options.instance.unzip
16
+
17
+ puts "Unzipping #{file_path}" if Options.instance.verbose
18
+ extract_zip(file_path, destination)
19
+ puts "Unzipped #{file_path} to #{html_path}" if Options.instance.verbose
20
+
21
+ nil
22
+ end
23
+
24
+ private
25
+
26
+ def destination
27
+ File.dirname(file_path)
28
+ end
29
+
30
+ def extract_zip(file, destination)
31
+ FileUtils.mkdir_p(destination)
32
+
33
+ Zip::File.open(file) do |zip_file|
34
+ zip_file.each do |f|
35
+ @html_path = File.join(destination, f.name)
36
+ zip_file.extract(f, @html_path) unless File.exist?(@html_path)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module UndriveGoogle
4
4
  module Version
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.1"
6
6
  end
7
7
  end
@@ -2,21 +2,35 @@
2
2
 
3
3
  # Std-lib
4
4
  require "psych" # The Ruby YAML library
5
+ require "forwardable" # The Ruby Delegation library
5
6
  require "singleton" # The Ruby Singleton library
6
7
 
7
8
  # third party gems
9
+ begin
10
+ require "zip"
11
+ rescue LoadError
12
+ warn "[WARN] Failed to load gem rubyzip, so unzip (and thus html) options will not work."
13
+ end
8
14
  require "version_gem"
9
15
  require "google_drive"
16
+ gd_fixed = GoogleDrive::Util::EXT_TO_CONTENT_TYPE.key?('.epub')
17
+ warn "[WARN] Your version of google_drive does not support .epub. If you need .epub support see: https://sr.ht/~galtzo/undrive_google/#note-export-epub" unless gd_fixed
10
18
 
11
19
  require_relative "undrive_google/version"
12
20
 
13
- require_relative "undrive_google/actions/download"
14
21
  require_relative "undrive_google/helpers/parse"
22
+ require_relative "undrive_google/actions/liberate"
23
+ require_relative "undrive_google/transformations/delete_zip"
24
+ require_relative "undrive_google/transformations/download"
25
+ require_relative "undrive_google/transformations/fix_html"
26
+ require_relative "undrive_google/transformations/rename_html"
27
+ require_relative "undrive_google/transformations/unzip"
15
28
  require_relative "undrive_google/captive_file"
16
29
  require_relative "undrive_google/cli"
17
30
  require_relative "undrive_google/config_file"
18
31
  require_relative "undrive_google/options"
19
32
  require_relative "undrive_google/session"
33
+ require_relative "undrive_google/transformation"
20
34
 
21
35
  module UndriveGoogle
22
36
  class Error < StandardError; end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undrive_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
@@ -36,7 +36,7 @@ cert_chain:
36
36
  gwGrEXGQGDZ0NIgBcmvMOqlXjkGQwQvugKycJ024z89+fz2332vdZIKTrSxJrXGk
37
37
  4/bR9A==
38
38
  -----END CERTIFICATE-----
39
- date: 2022-10-25 00:00:00.000000000 Z
39
+ date: 2022-10-26 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: google_drive
@@ -116,13 +116,19 @@ files:
116
116
  - SECURITY.md
117
117
  - exe/undrive_google
118
118
  - lib/undrive_google.rb
119
- - lib/undrive_google/actions/download.rb
119
+ - lib/undrive_google/actions/liberate.rb
120
120
  - lib/undrive_google/captive_file.rb
121
121
  - lib/undrive_google/cli.rb
122
122
  - lib/undrive_google/config_file.rb
123
123
  - lib/undrive_google/helpers/parse.rb
124
124
  - lib/undrive_google/options.rb
125
125
  - lib/undrive_google/session.rb
126
+ - lib/undrive_google/transformation.rb
127
+ - lib/undrive_google/transformations/delete_zip.rb
128
+ - lib/undrive_google/transformations/download.rb
129
+ - lib/undrive_google/transformations/fix_html.rb
130
+ - lib/undrive_google/transformations/rename_html.rb
131
+ - lib/undrive_google/transformations/unzip.rb
126
132
  - lib/undrive_google/version.rb
127
133
  homepage: https://sr.ht/~galtzo/undrive_google/
128
134
  licenses:
@@ -132,7 +138,7 @@ metadata:
132
138
  source_code_uri: https://git.sr.ht/~galtzo/undrive_google
133
139
  changelog_uri: https://git.sr.ht/~galtzo/undrive_google
134
140
  bug_tracker_uri: https://todo.sr.ht/~galtzo/undrive_google
135
- documentation_uri: https://www.rubydoc.info/gems/undrive_google/1.0.0
141
+ documentation_uri: https://www.rubydoc.info/gems/undrive_google/1.0.1
136
142
  wiki_uri: https://man.sr.ht/~galtzo/undrive_google/
137
143
  funding_uri: https://liberapay.com/pboling
138
144
  mailing_list_uri: https://lists.sr.ht/~galtzo/undrive_google-devel
metadata.gz.sig CHANGED
Binary file
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UndriveGoogle
4
- module Actions
5
- # Download a particular version of the file.
6
- class Download
7
- attr_accessor :file, :extension, :dir
8
-
9
- def initialize(extension)
10
- @file = Session.instance.file
11
- @extension = extension.to_s
12
- @dir = Options.instance.dir
13
- end
14
-
15
- def download!
16
- puts "Downloading #{file.title} as: #{extension} to: #{dir}/#{name}" if Options.instance.verbose
17
- file.export_as_file("#{dir}/#{name}", extension)
18
- end
19
-
20
- private
21
-
22
- def name
23
- "#{rename}.#{extension}"
24
- end
25
-
26
- def rename
27
- exact_name = Options.instance.rename[extension]
28
- return exact_name if exact_name
29
-
30
- Options.instance.rename_proc.call(file.title)
31
- end
32
- end
33
- end
34
- end