dirtp 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8042deed082df4dd25c93248372820fe1d443a9e
4
+ data.tar.gz: 58452f5a327bc269bb2b58ae3388436b9744b894
5
+ SHA512:
6
+ metadata.gz: c0b91bccbb1e33ae68841a675071a3076e1691d0309348e3e35a2dc5f1e77898d4bb69b813fb7fe7f42abc1c38d957f889117e98f7948d4a74c7d66bef1bd7ad
7
+ data.tar.gz: d215b025584d7ec229d166ba2ca9e82a118a787d16775407172d921c38e3644dd54852646a4e5e5c59ce65aadee7a8c9d19b0be21e4135592fa3541767725956
@@ -0,0 +1,49 @@
1
+ .DS_Store
2
+ *.rbc
3
+ capybara-*.html
4
+ .rspec
5
+ /log/*
6
+ !/log/.keep
7
+ /tmp
8
+ /db/*.sqlite3
9
+ /db/*.sqlite3-journal
10
+ /public/system
11
+ /public/uploads/*
12
+ /coverage/
13
+ /spec/tmp
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
17
+
18
+ # TODO Comment out these rules if you are OK with secrets being uploaded to the repo
19
+ config/initializers/secret_token.rb
20
+ secrets.sh
21
+ docker-tutum-stack*.yml
22
+ docker-cloud-stack*.yml
23
+ secrets.yml
24
+ # Ignore linked files for rails (JLM 150513)
25
+ config/database.yml
26
+ config/secrets.yml
27
+ .env
28
+
29
+ ## Environment normalisation:
30
+ /.bundle
31
+ /vendor/bundle
32
+
33
+ # these should all be checked in to normalise the environment:
34
+ # Gemfile.lock, .ruby-version, .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
38
+
39
+ # if using bower-rails ignore default bower_components path bower.json files
40
+ /vendor/assets/bower_components
41
+ *.bowerrc
42
+ bower.json
43
+
44
+ # Ignore pow environment settings
45
+ .powenv
46
+
47
+ # Ignore working files of RubyMine
48
+ .idea
49
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'logger'
4
+ gem 'mechanize'
5
+ gem 'nokogiri'
6
+ gem 'rest-client'
7
+ gem 'slop'
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ domain_name (0.5.20170404)
5
+ unf (>= 0.0.5, < 1.0.0)
6
+ http-cookie (1.0.3)
7
+ domain_name (~> 0.5)
8
+ logger (1.2.8)
9
+ mechanize (2.7.5)
10
+ domain_name (~> 0.5, >= 0.5.1)
11
+ http-cookie (~> 1.0)
12
+ mime-types (>= 1.17.2)
13
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
14
+ net-http-persistent (~> 2.5, >= 2.5.2)
15
+ nokogiri (~> 1.6)
16
+ ntlm-http (~> 0.1, >= 0.1.1)
17
+ webrobots (>= 0.0.9, < 0.2)
18
+ mime-types (3.1)
19
+ mime-types-data (~> 3.2015)
20
+ mime-types-data (3.2016.0521)
21
+ mini_portile2 (2.3.0)
22
+ net-http-digest_auth (1.4.1)
23
+ net-http-persistent (2.9.4)
24
+ netrc (0.11.0)
25
+ nokogiri (1.8.1)
26
+ mini_portile2 (~> 2.3.0)
27
+ ntlm-http (0.1.1)
28
+ rest-client (2.0.2)
29
+ http-cookie (>= 1.0.2, < 2.0)
30
+ mime-types (>= 1.16, < 4.0)
31
+ netrc (~> 0.8)
32
+ slop (4.6.1)
33
+ unf (0.1.4)
34
+ unf_ext
35
+ unf_ext (0.0.7.4)
36
+ webrobots (0.1.2)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ logger
43
+ mechanize
44
+ nokogiri
45
+ rest-client
46
+ slop
47
+
48
+ BUNDLED WITH
49
+ 1.13.6
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,72 @@
1
+ # Dirtp: TablePress maker for Web directories
2
+
3
+
4
+ This Ruby script queries a website that offers directory listings, and converts them into TablePress tables
5
+ for inclusion into a Wordpress website. The first column is the date and the second column is a link to the
6
+ file on the remote server. When directories are encountered in the listing, these are recursively
7
+ parsed, but flattened and not listed, so that all files are listed in a single table. The directory structure
8
+ is only reflected by including the directory names in the links.
9
+ Optionally, each filename can be looked up in a separate web-based database to retrieve a description of each
10
+ file, which is then included in a third column in the generated table.
11
+
12
+ ## Usage
13
+
14
+ Check the `bin/dir-to-tablepress.rb` script for an example of using this gem.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'dirtp'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install dirtp
31
+
32
+ ## Command-Line Options
33
+ This gem comes with a commend-line utility which demonstrates its use and can be used
34
+ to generate Tablepress JSON tables.
35
+
36
+ Without any options, the script prints a usage message and exits. The mandatory first argument us the URL of the
37
+ directory to parse.
38
+ * `--lookup` (abbreviated `-l`): Look up each filename in a web-based database and include the resulting description
39
+ as a third column in the generated table. To use this option, some configuration options need to be defined
40
+ in the `secrets.yml` file: `db_uri` is the URL of the lookup API, to which a query in the form `?filename=fname`
41
+ will be appended. The output is expected in XML, as documented elsewhere. The only tag interpretted is the
42
+ `<description>` and this text is included in the third table column.
43
+
44
+ * `--update table-ref` (abbreviated `-u`): this unimplmented option will extend an existing table. Existing
45
+ entries will not be modified. Additional entries will be added. Entries for files which are no longer present
46
+ will be removed with a warning.
47
+ * `--outfile outputfilename` (abbreviated `-o`): write the table to `outfilename` instead of standard output.
48
+ * `--secrets secretfile.yml` (abbreviated `-s`): Use the given file for the configuration options rather than the
49
+ default `secrets.yml`.
50
+ * `--debug` (abbreviated `-d`): print additional debugging information to STDERR and use a localhost proxy on port
51
+ 8888 to allow web traffic to be inspected.
52
+
53
+ ### Configuration
54
+ The URL of the web-based filename lookup API, together with the usernames
55
+ and passwords for it and for the directory to be parsed, are stored in `secrets.yml` which is not included
56
+ in the sources. An example of that file file is available as `example-secrets.yml`.
57
+
58
+ ### Usage
59
+ The example script was developed to allow lists of files to be shown in a Wordpress website. The idea is to periodically
60
+ recreate or update the table either via `wp-cron` or by manipulating the Wordpress API. The output format is
61
+ compatible with TablePress' import format.
62
+
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jlm/dir-to-tablepress.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dirtp"
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(__FILE__)
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+ ####
3
+ # Copyright 2017 John Messenger
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ ####
17
+
18
+ require 'rubygems'
19
+ require 'date'
20
+ require 'logger'
21
+ require 'rest-client'
22
+ require 'slop'
23
+ require 'yaml'
24
+ require 'dirtp'
25
+
26
+ #
27
+ # Main program
28
+ #
29
+ begin
30
+ opts = Slop.parse do |o|
31
+ o.banner = 'usage: dir-to-tablepress.rb [options] URL-of-directory'
32
+ o.string '-s', '--secrets', 'secrets YAML file name', default: 'secrets.yml'
33
+ o.bool '-d', '--debug', 'debug mode'
34
+ o.bool '-u', '--update', 'update an existing table'
35
+ o.bool '-l', '--lookup', 'look up filenames in the web database'
36
+ o.string '-o', '--outfile', 'output filename'
37
+ o.int '-p', '--per-page', 'how many files to display per page', default: 15
38
+ o.on '--help' do
39
+ STDERR.puts o
40
+ exit
41
+ end
42
+ end
43
+
44
+ # The first argument to the script is the URL of the directory to convert.
45
+ if opts.args.count == 1
46
+ arch_url = opts.args[0]
47
+ else
48
+ STDERR.puts opts
49
+ exit
50
+ end
51
+
52
+ config = YAML.load(File.read(opts[:secrets]))
53
+ $stdout.reopen(opts[:outfile]) if opts[:outfile]
54
+
55
+ # Set up logging (which is not used)
56
+ $DEBUG = opts.debug?
57
+ $logger = Logger.new(STDERR)
58
+ $logger.level = Logger::INFO
59
+ $logger.level = Logger::DEBUG if $DEBUG
60
+
61
+ # When debugging, use a proxy on localhost to allow Charles Proxy to see the interactions
62
+ if $DEBUG
63
+ RestClient.proxy = "http://localhost:8888"
64
+ $logger.debug("Using HTTP proxy #{RestClient.proxy}")
65
+ end
66
+
67
+ arch_creds = [config['username'], config['password']]
68
+ db_creds = [config['db_user'], config['db_password']]
69
+
70
+ tptable = TPTable.new(arch_url, creds: arch_creds, db_url: config['db_uri'], db_creds: db_creds)
71
+
72
+ # Retrieve and parse the directory into an array of file details.
73
+ files = tptable.parse_dir(lookup: opts[:lookup])
74
+
75
+ if $DEBUG
76
+ files.each do |f|
77
+ puts "#{f[:date]} #{f[:name]} #{f[:href]} #{f[:desc]}"
78
+ end
79
+ end
80
+
81
+ puts tptable.to_json(per_page: opts[:per_page], lookup: opts[:lookup])
82
+
83
+ end
@@ -0,0 +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
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dirtp/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dirtp"
8
+ spec.version = Dirtp::VERSION
9
+ spec.authors = ["John Messenger"]
10
+ spec.email = ["j.l.messenger@ieee.org"]
11
+
12
+ spec.summary = %q{Parse an Apache-generated auto-index directory listing and make a TablePress table from it.}
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/jlm/dir-to-tablepress"
15
+ spec.license = "Apache-2.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8'
28
+ spec.add_runtime_dependency 'rest-client', '~> 2.0'
29
+ spec.add_runtime_dependency 'slop', '~> 4.6'
30
+ spec.add_runtime_dependency 'logger', '~> 1.2'
31
+ end
@@ -0,0 +1,5 @@
1
+ username: user
2
+ password: pass
3
+ db_uri: http://example.com/lookup.php
4
+ db_user: dbuser
5
+ db_password: dbpass
@@ -0,0 +1,6 @@
1
+ require "dirtp/version"
2
+
3
+ module Dirtp
4
+ end
5
+
6
+ require 'dirtp/tptable'
@@ -0,0 +1,138 @@
1
+ ####
2
+ # Copyright 2017 John Messenger
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ####
16
+
17
+ require 'rubygems'
18
+ require 'date'
19
+ require 'json'
20
+ require 'nokogiri'
21
+ require 'open-uri'
22
+ require 'rest-client'
23
+
24
+ class TPTable
25
+ attr_accessor :dir_url, :creds, :db_url, :db_creds
26
+
27
+ # @param [String] dir_url URL of the directory to scan
28
+ # @param [Array<String>] creds Username and password required to authenticate access to the directory
29
+ # @param [String] db_url URL of the database in which to look up the file's description
30
+ # @param [Array] db_creds Username and password required to authenticate access to the database
31
+ def initialize(dir_url, creds: [], db_url: '', db_creds: [])
32
+ @dir_url = dir_url
33
+ @creds = creds
34
+ @db_url = db_url
35
+ @db_creds = db_creds
36
+ @files = []
37
+ end
38
+
39
+ ####
40
+ # Look up a filename in the web-based filename database and return its description
41
+ ####
42
+ # @param [String] filename
43
+ def query_db(filename)
44
+ uri = URI.parse(@db_url)
45
+ uri = URI::HTTP.build(host: uri.host, path: uri.path, query: "filename=#{filename}")
46
+ page = open(uri, http_basic_authentication: @db_creds) { |f| f.read }
47
+ pagedoc = Nokogiri::XML(page)
48
+ pagedoc.css('description').text
49
+ end
50
+
51
+ ####
52
+ # Retrieve a directory listing from a web server and parse it to extract filenames and dates. Optionally query a
53
+ # web-based database to retrieve a textual description of the file.
54
+ ####
55
+ # @param [String] prefix A prefix to prepend to the filename in recursive invocations
56
+ # @param [Bool] lookup Should the filename be looked up in the web database?
57
+ def parse_dir(prefix: '', lookup: false)
58
+ page = open(@dir_url, http_basic_authentication: @creds) { |f| f.read }
59
+ pagedoc = Nokogiri::HTML(page)
60
+ found_parent = false
61
+ pagedoc.at('pre').children.each do |el|
62
+ unless found_parent
63
+ found_parent = /Parent Directory/.match(el.text) unless found_parent
64
+ next unless found_parent
65
+ end
66
+ next unless found_parent
67
+ next if /Parent Directory/.match(el.text)
68
+ puts el.text if $DEBUG
69
+ if el.name == 'a'
70
+ if /\/$/.match(el.text)
71
+ puts "Directory #{el['href']}" if $DEBUG
72
+ # XXX What's the best way to deal with recursive calls in a class? Could use recursive instansiation or
73
+ # make parse_dir a Class method instead of an Instance method.
74
+ dir = TPTable.new(@dir_url + el['href'], creds: @creds, db_url: @db_url, db_creds: @db_creds)
75
+ @files += dir.parse_dir(prefix: el.text, lookup: lookup)
76
+ else
77
+ puts "It's a file #{el['href']} with prefix #{prefix}" if $DEBUG
78
+ name = el.text
79
+ file = { name: prefix + name,
80
+ href: @dir_url + el['href'],
81
+ date: DateTime.parse(el.next) }
82
+ file[:desc] = query_db(name) if lookup
83
+ @files << file
84
+ end
85
+ end
86
+ end
87
+ return @files
88
+ end
89
+
90
+ ####
91
+ # Convert the file list to a TablePress specification in JSON. Optionally query a
92
+ # web-based database to retrieve a textual description of the file.
93
+ ####
94
+ # @param [Integer] per_page Tell TablePress how many entries to show per page
95
+ # @param [Bool] lookup Should the filename be looked up in the web database?
96
+ def to_json(per_page: 10, lookup: false)
97
+ # Build a TablePress table specification which will be output as a JSON object for import into Wordpress.
98
+ table = {
99
+ name: 'Archive',
100
+ description: "Archive of files for #{File.split(@dir_url)[-1]}, generated by dir-to-tablepress.rb at #{Time.now}",
101
+ options: {
102
+ table_head: true,
103
+ table_foot: false,
104
+ alternating_row_colors: true,
105
+ row_hover: true,
106
+ print_name: true,
107
+ print_name_position: 'above',
108
+ print_description: false,
109
+ print_description_position: 'below',
110
+ extra_css_classes: '',
111
+ use_datatables: true,
112
+ datatables_sort: true,
113
+ datatables_filter: true,
114
+ datatables_paginate: true,
115
+ datatables_lengthchange: true,
116
+ datatables_paginate_entries: per_page,
117
+ datatables_info: true,
118
+ datatables_scrollx: false,
119
+ datatables_custom_commands: '"order": [[ 0, \'desc\' ]], "columnDefs": [ { "type": "date", "targets": [ 0 ] } ]'
120
+ }
121
+ }
122
+
123
+ # Add table headers
124
+ table[:data] = lookup ? [ %w[Date Document Description] ] : [ %w[Date Document] ]
125
+
126
+ @files.each do |f|
127
+ builder = Nokogiri::XML::Builder.new do |xml|
128
+ xml.a(File.split(f[:name])[-1], href: f[:href])
129
+ end
130
+ link = builder.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
131
+ entry = [f[:date].to_date.to_s, link]
132
+ entry << f[:desc] if lookup
133
+ table[:data] << entry
134
+ end
135
+ JSON.pretty_generate(table)
136
+ end
137
+
138
+ end
@@ -0,0 +1,3 @@
1
+ module Dirtp
2
+ VERSION = "0.1.4"
3
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dirtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - John Messenger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: slop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: logger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ description:
98
+ email:
99
+ - j.l.messenger@ieee.org
100
+ executables:
101
+ - console
102
+ - dir-to-tablepress.rb
103
+ - setup
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/dir-to-tablepress.rb
115
+ - bin/setup
116
+ - dirtp.gemspec
117
+ - example-secrets.yml
118
+ - lib/dirtp.rb
119
+ - lib/dirtp/tptable.rb
120
+ - lib/dirtp/version.rb
121
+ homepage: https://github.com/jlm/dir-to-tablepress
122
+ licenses:
123
+ - Apache-2.0
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.5.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Parse an Apache-generated auto-index directory listing and make a TablePress
145
+ table from it.
146
+ test_files: []