natour 0.3.0 → 0.4.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: 653a7f10629f9d5cb63e991eba1e4693e88dcf3b26d0a92978f98601d8223507
4
- data.tar.gz: 8e8671274bab8ac94bec16bcd5d967cdd337ccfb81e63b406d3d066d0689951d
3
+ metadata.gz: 731b0453c6a936a6207ce9802c212ed6747631eaedac2c88becc01420774f066
4
+ data.tar.gz: b8558c8228ddc650b44c92632f55d44737cdb9f5ac2114e1b472b8cb860d12d2
5
5
  SHA512:
6
- metadata.gz: f8a3ec14f64440b08bc1e291ac20bd7cff86cef27509010c253779eab32ca26098667b8d5b13b216f8f486174b326f201941201da98a4b4ed8e22d3a43d2407f
7
- data.tar.gz: 3554afc251f92900f32fe59e925c690cc9f1de77f7e3b84c0a94c5885a2706543ddca2b0a6cd50c11a0ea13918923c3e1766ef7059d938ed89fae49d49135a8c
6
+ metadata.gz: a866a26f237240eeb30e6b4ca8aa5e82c1bfb7db472d18951a5fccb6e3c873f6b0683b9c04bfe60202d81442edb3205e7b8a7d44dd4eddad09329c1a4d181d9e
7
+ data.tar.gz: f181d95ffb4df6445c649a51b6d762bd8d801ccfcdc57c616e8dad7cb30f5b52afe5daf294c1ae76c4ecec9efa719f03dbceae84dd9db7a2fdbfbed870d97f09
data/CHANGELOG.adoc CHANGED
@@ -6,6 +6,17 @@ The format is based on https://keepachangelog.com/en/1.0.0/[Keep a Changelog^],
6
6
 
7
7
  == Unreleased
8
8
 
9
+ == 0.4.0 - 2021-05-02
10
+
11
+ === Added
12
+
13
+ - Show date/time of images in draft output
14
+ - Support preferred conversion backend for draft output
15
+
16
+ === Changed
17
+
18
+ - Remove redundant method `Image.portrait?`
19
+
9
20
  == 0.3.0 - 2021-03-07
10
21
 
11
22
  === Changed
data/bin/natour CHANGED
@@ -17,6 +17,7 @@ config = Natour::Config.load_file(
17
17
  'adoc-author' => nil,
18
18
  'backend' => 'pdf',
19
19
  'draft' => false,
20
+ 'draft-backend' => nil,
20
21
  'image-maxdim' => 1800
21
22
  }
22
23
  )
@@ -69,6 +70,9 @@ option_parser = OptionParser.new do |opts|
69
70
  opts.on('--[no-]draft', 'Show additional information (e.g. image paths)') do |value|
70
71
  config['draft'] = value
71
72
  end
73
+ opts.on('--draft-backend BACKEND', 'Preferred conversion backend for draft (pdf, html5 ...)') do |value|
74
+ config['draft-backend'] = value
75
+ end
72
76
  opts.on('--image-maxdim DIM', 'Shrink oversized images (PDF only)') do |value|
73
77
  config['image-maxdim'] = value.to_i
74
78
  end
@@ -106,6 +110,7 @@ else
106
110
  overwrite: config['overwrite'],
107
111
  backend: config['backend'],
108
112
  draft: config['draft'],
113
+ draft_backend: config['draft-backend'],
109
114
  image_maxdim: config['image-maxdim']
110
115
  )
111
116
  end
data/lib/natour.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'natour/helpers/date_parser'
2
- require 'natour/helpers/suppress_output'
1
+ require 'natour/utils/date_utils'
2
+ require 'natour/utils/stdout_utils'
3
3
  require 'natour/convert'
4
4
  require 'natour/create'
5
5
  require 'natour/asciinurse'
@@ -77,10 +77,10 @@ module Natour
77
77
  doc << '== Bilder'
78
78
  doc << ''
79
79
  images.each do |image|
80
- width = if image.portrait?
81
- '40%'
82
- else
80
+ width = if image.landscape?
83
81
  '80%'
82
+ else
83
+ '40%'
84
84
  end
85
85
  doc << '.Abbildung {counter:image}'
86
86
  doc << "image::#{Pathname(doc_root).join(image.path)}[width=#{width}]"
@@ -8,7 +8,13 @@ module Natour
8
8
  module_function
9
9
 
10
10
  def convert(filename, out_dir: nil, out_file: nil, overwrite: false,
11
- backend: 'pdf', draft: false, image_maxdim: 16000)
11
+ backend: 'pdf', draft: false, draft_backend: nil, image_maxdim: 16000)
12
+ backend = if draft
13
+ draft_backend || backend
14
+ else
15
+ backend
16
+ end
17
+
12
18
  doc = Asciidoctor.load_file(
13
19
  filename,
14
20
  backend: backend,
@@ -29,7 +35,9 @@ module Natour
29
35
 
30
36
  if draft
31
37
  doc.find_by(context: :image).each do |node|
32
- node.title = "#{node.title} [#{node.attr('target')}]"
38
+ target = node.attr('target')
39
+ image = Image.load_file(dir.join(target).to_s)
40
+ node.title = "#{node.title} [#{[target, image.date_time].compact.join('|')}]"
33
41
  end
34
42
  end
35
43
 
@@ -40,25 +48,17 @@ module Natour
40
48
  title_logo_image = doc.attr('title-logo-image')
41
49
  if title_logo_image
42
50
  target = title_logo_image[/^image:{1,2}(.*?)\[(.*?)\]$/, 1]
43
- options = {}
44
- options[:autorotate] = true if target =~ /\.jpe?g$/i
45
- image = Vips::Image.new_from_file(dir.join(target).to_s, options)
46
- scale = image_maxdim / image.size.max.to_f
47
- image = image.resize(scale) if scale < 1.0
51
+ image = Image.load_file(dir.join(target).to_s).autorotate.shrink_to(image_maxdim)
48
52
  new_target = tmp_dir.join("title_logo_image_#{Pathname(target).basename}").to_s
49
- suppress_output { image.write_to_file(new_target) }
53
+ image.save_as(new_target)
50
54
  doc.set_attr('title-logo-image', title_logo_image.gsub(target, new_target))
51
55
  end
52
56
 
53
57
  doc.find_by(context: :image).each.with_index do |node, index|
54
58
  target = node.attr('target')
55
- options = {}
56
- options[:autorotate] = true if target =~ /\.jpe?g$/i
57
- image = Vips::Image.new_from_file(dir.join(target).to_s, options)
58
- scale = image_maxdim / image.size.max.to_f
59
- image = image.resize(scale) if scale < 1.0
59
+ image = Image.load_file(dir.join(target).to_s).autorotate.shrink_to(image_maxdim)
60
60
  new_target = tmp_dir.join("image#{index}_#{Pathname(target).basename}").to_s
61
- suppress_output { image.write_to_file(new_target) }
61
+ image.save_as(new_target)
62
62
  node.set_attr('target', new_target)
63
63
  end
64
64
 
data/lib/natour/image.rb CHANGED
@@ -1,29 +1,57 @@
1
1
  require 'vips'
2
2
  require 'timeliness'
3
+ require 'fileutils'
4
+ require 'pathname'
3
5
 
4
6
  module Natour
5
7
  class Image
6
8
  attr_reader :path
7
9
  attr_reader :date_time
8
10
 
9
- def initialize(path)
11
+ def initialize(path, image)
10
12
  @path = path
11
- image = Vips::Image.new_from_file(path)
12
- width, height = image.size
13
- @portrait = width < height
14
- get_field = ->(name) { image.get(name) if image.get_fields.include?(name) }
15
- orientation = get_field.call('exif-ifd0-Orientation')
16
- @portrait = orientation[/^(\d) \(/, 1].to_i.between?(5, 8) if orientation
17
- date_time = get_field.call('exif-ifd0-DateTime')
13
+ @image = image
14
+ orientation = get_field('exif-ifd0-Orientation')
15
+ @landscape = if orientation
16
+ orientation[/^(\d) \(/, 1].to_i.between?(1, 4)
17
+ else
18
+ image.width >= image.height
19
+ end
20
+ date_time = get_field('exif-ifd0-DateTime')
18
21
  @date_time = Timeliness.parse(date_time[/^(.*?) \(/, 1], format: 'yyyy:mm:dd hh:nn:ss') if date_time
19
22
  end
20
23
 
21
- def portrait?
22
- @portrait
24
+ def self.load_file(filename)
25
+ Image.new(filename, Vips::Image.new_from_file(filename))
23
26
  end
24
27
 
25
28
  def landscape?
26
- !portrait?
29
+ @landscape
30
+ end
31
+
32
+ def autorotate
33
+ Image.new(@path, @image.autorot)
34
+ end
35
+
36
+ def shrink_to(maxdim)
37
+ scale = maxdim / @image.size.max.to_f
38
+ image = if scale < 1.0
39
+ @image.resize(scale)
40
+ else
41
+ @image.copy
42
+ end
43
+ Image.new(@path, image)
44
+ end
45
+
46
+ def save_as(filename)
47
+ FileUtils.mkdir_p(Pathname(filename).dirname)
48
+ StdoutUtils.suppress_output { @image.write_to_file(filename) }
49
+ end
50
+
51
+ private
52
+
53
+ def get_field(name)
54
+ @image.get(name) if @image.get_fields.include?(name)
27
55
  end
28
56
  end
29
57
  end
data/lib/natour/report.rb CHANGED
@@ -35,7 +35,7 @@ module Natour
35
35
  title = Pathname.pwd.basename.to_s.encode('utf-8')
36
36
  .gsub(/^\d{4}-\d{2}-\d{2}( |_|-)?/, '')
37
37
  images = Pathname.glob('**/*.{jpg,jpeg}', File::FNM_CASEFOLD)
38
- .map { |filename| Image.new(filename.to_s) }
38
+ .map { |filename| Image.load_file(filename.to_s) }
39
39
  .sort_by { |image| [image.date_time ? 0 : 1, image.date_time, image.path] }
40
40
  species_lists =
41
41
  Pathname.glob('**/*.{csv,kml}', File::FNM_CASEFOLD)
@@ -58,7 +58,7 @@ module Natour
58
58
  gps_track.save_gpx(track, overwrite: true)
59
59
  filename = Pathname(gps_track.path).sub_ext('.jpg')
60
60
  map.save_image(filename, tracks: [track], layers: map_layers)
61
- Image.new(filename.to_s)
61
+ Image.load_file(filename.to_s)
62
62
  end
63
63
  end
64
64
  end
@@ -34,13 +34,13 @@ module Natour
34
34
  case header
35
35
  when /^Primary/
36
36
  CSV.open(filename, 'r:windows-1252:utf-8', headers: true, liberal_parsing: true) do |csv|
37
- date = DateParser.parse(Pathname(filename).basename).compact.first
37
+ date = DateUtils.parse(Pathname(filename).basename).compact.first
38
38
  items = csv.map { |row| Species.new(row[1], row[0]) }
39
39
  .sort_by(&:name_de).uniq
40
40
  [SpeciesList.new(filename, date, :kosmos_vogelfuehrer, :birds, nil, nil, items)]
41
41
  end
42
42
  when /^<\?xml.*?www\.ornitho\.ch/m
43
- date = DateParser.parse(Pathname(filename).basename).compact.first
43
+ date = DateUtils.parse(Pathname(filename).basename).compact.first
44
44
  doc = Nokogiri.XML(File.read(filename, mode: 'r:utf-8'))
45
45
  folder = doc.at('/xmlns:kml/xmlns:Document/xmlns:Folder/xmlns:Folder/xmlns:Folder')
46
46
  name = folder.at('./xmlns:name').text
@@ -57,7 +57,7 @@ module Natour
57
57
  .reject { |rows| rows.count == 1 }
58
58
  chunks.map do |rows|
59
59
  name, description = rows.shift
60
- date = DateParser.parse(name, Pathname(filename).basename).compact.first
60
+ date = DateUtils.parse(name, Pathname(filename).basename).compact.first
61
61
  items = rows.map { |row| Species.new(row[1][/^(([^ ]+ [^ ]+)(( aggr\.)|( subsp\. [^ ]+))?)/, 1], row[2]) }
62
62
  .sort_by(&:name).uniq
63
63
  SpeciesList.new(
@@ -73,7 +73,7 @@ module Natour
73
73
  end
74
74
  when /^obs_id/
75
75
  CSV.open(filename, 'r:bom|utf-16le:utf-8', col_sep: "\t", headers: true) do |csv|
76
- date = DateParser.parse(Pathname(filename).basename).compact.first
76
+ date = DateUtils.parse(Pathname(filename).basename).compact.first
77
77
  items = csv.select { |row| row[0] }
78
78
  .map { |row| Species.new(row[11][/^(([^ ]+ [^ ]+)(( aggr\.)|( subsp\. [^ ]+))?)/, 1], nil) }
79
79
  .sort_by(&:name).uniq
@@ -1,7 +1,7 @@
1
1
  require 'timeliness'
2
2
 
3
3
  module Natour
4
- module DateParser
4
+ module DateUtils
5
5
  module_function
6
6
 
7
7
  def parse(*args)
@@ -0,0 +1,16 @@
1
+ module Natour
2
+ module StdoutUtils
3
+ module_function
4
+
5
+ def suppress_output
6
+ orig_stdout = $stdout.clone
7
+ orig_stderr = $stderr.clone
8
+ $stdout.reopen(File.new(File::NULL, 'w'))
9
+ $stderr.reopen(File.new(File::NULL, 'w'))
10
+ yield
11
+ ensure
12
+ $stdout.reopen(orig_stdout)
13
+ $stderr.reopen(orig_stderr)
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Gysi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-07 00:00:00.000000000 Z
11
+ date: 2021-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -192,8 +192,6 @@ files:
192
192
  - lib/natour/gps_track.rb
193
193
  - lib/natour/gps_track_point.rb
194
194
  - lib/natour/gpx_file.rb
195
- - lib/natour/helpers/date_parser.rb
196
- - lib/natour/helpers/suppress_output.rb
197
195
  - lib/natour/image.rb
198
196
  - lib/natour/map_geo_admin.rb
199
197
  - lib/natour/public_transport.rb
@@ -201,6 +199,8 @@ files:
201
199
  - lib/natour/species.rb
202
200
  - lib/natour/species_list.rb
203
201
  - lib/natour/station.rb
202
+ - lib/natour/utils/date_utils.rb
203
+ - lib/natour/utils/stdout_utils.rb
204
204
  homepage: https://rubygems.org/gems/natour
205
205
  licenses:
206
206
  - MIT
@@ -1,14 +0,0 @@
1
- module Natour
2
- module_function
3
-
4
- def suppress_output
5
- orig_stdout = $stdout.clone
6
- orig_stderr = $stderr.clone
7
- $stdout.reopen(File.new(File::NULL, 'w'))
8
- $stderr.reopen(File.new(File::NULL, 'w'))
9
- yield
10
- ensure
11
- $stdout.reopen(orig_stdout)
12
- $stderr.reopen(orig_stderr)
13
- end
14
- end