Module: Natour

Defined in:
lib/natour/image.rb,
lib/natour/config.rb,
lib/natour/report.rb,
lib/natour/species.rb,
lib/natour/station.rb,
lib/natour/version.rb,
lib/natour/fit_file.rb,
lib/natour/gpx_file.rb,
lib/natour/gps_track.rb,
lib/natour/asciinurse.rb,
lib/natour/species_list.rb,
lib/natour/map_geo_admin.rb,
lib/natour/convert_report.rb,
lib/natour/create_reports.rb,
lib/natour/gps_track_point.rb,
lib/natour/public_transport.rb,
lib/natour/utils/date_utils.rb,
lib/natour/utils/errno_utils.rb,
lib/natour/utils/stdout_utils.rb,
lib/natour/utils/boolean_utils.rb,
lib/natour/utils/botanical_name_utils.rb

Defined Under Namespace

Modules: Asciinurse, BooleanUtils, BotanicalNameUtils, DateUtils, ErrnoUtils, StdoutUtils Classes: Config, FITFile, GPSTrack, GPSTrackPoint, GPXFile, Image, MapGeoAdmin, PublicTransport, Report, Species, SpeciesList, Station

Constant Summary collapse

VERSION =
'0.12.0'

Class Method Summary collapse

Class Method Details

.convert_report(filename, out_dir: nil, out_file: nil, overwrite: false, backend: :pdf, draft: false, draft_backend: nil, image_maxdim: 16000) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/natour/convert_report.rb', line 11

def convert_report(filename, out_dir: nil, out_file: nil, overwrite: false,
                   backend: :pdf, draft: false, draft_backend: nil, image_maxdim: 16000)
  backend = if draft
              draft_backend || backend
            else
              backend
            end

  doc = Asciidoctor.load_file(
    filename,
    backend: backend,
    safe: :unsafe,
    standalone: true,
    attributes: {
      'pdf-theme' => 'natour',
      'pdf-themesdir' => "#{__dir__}/data/themes",
      'pdf-fontsdir' => "#{__dir__}/data/fonts"
    }
  )

  dir = Pathname(filename).dirname
  out_dir = Pathname(out_dir || dir)
  out_file = Pathname(
    out_file || "#{doc.attr('docname')}#{doc.attr('outfilesuffix')}"
  )
  filename = out_dir.join(out_file)

  if draft
    doc.find_by(context: :image).each do |node|
      target = node.attr('target')
      image = Image.load_file(dir.join(target).to_s)
      node.title = "#{node.title} [#{[target, image.date_time].compact.join('|')}]"
    end
  end

  %w[
    revdate
    docdate
    doctime
    docdatetime
    localdate
    localtime
    localdatetime
  ].each do |attr_name|
    attr_value = doc.attr(attr_name)
    next unless attr_value

    date_time = Time.parse(attr_value)
    if attr_name.end_with?('datetime')
      doc.set_attr(attr_name, date_time.strftime('%d.%m.%Y %H:%M:%S'))
    elsif attr_name.end_with?('date')
      doc.set_attr(attr_name, date_time.strftime('%d.%m.%Y'))
    elsif attr_name.end_with?('time')
      doc.set_attr(attr_name, date_time.strftime('%H:%M:%S'))
    end
  end

  if backend == :pdf
    Dir.mktmpdir do |tmp_dir|
      tmp_dir = Pathname(tmp_dir)

      title_logo_image = doc.attr('title-logo-image')
      if title_logo_image
        target = title_logo_image[/^image:{1,2}(.*?)\[(.*?)\]$/, 1]
        image = Image.load_file(dir.join(target).to_s).autorotate.shrink_to(image_maxdim)
        new_target = tmp_dir.join("title_logo_image_#{Pathname(target).basename}").to_s
        image.save_as(new_target)
        doc.set_attr('title-logo-image', title_logo_image.gsub(target, new_target))
      end

      doc.find_by(context: :image).each.with_index do |node, index|
        target = node.attr('target')
        image = Image.load_file(dir.join(target).to_s).autorotate.shrink_to(image_maxdim)
        new_target = tmp_dir.join("image#{index}_#{Pathname(target).basename}").to_s
        image.save_as(new_target)
        node.set_attr('target', new_target)
      end

      FileUtils.mkdir_p(out_dir)
      mode = File::WRONLY | File::CREAT | File::TRUNC | File::BINARY
      mode |= File::EXCL unless overwrite
      File.open(filename, mode) do |file|
        doc.write(doc.convert, file)
      end
    end
  else
    FileUtils.mkdir_p(out_dir)
    mode = File::WRONLY | File::CREAT | File::TRUNC
    mode |= File::EXCL unless overwrite
    File.open(filename, mode) do |file|
      doc.write(doc.convert, file)
    end
  end

  filename.to_s
end

.create_reports(dir, out_dir: nil, out_file: nil, overwrite: false, track_formats: %i[gpx fit],, create_map: true, map_layers: [], adoc_author: nil, short_species_names: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/natour/create_reports.rb', line 6

def create_reports(dir, out_dir: nil, out_file: nil, overwrite: false,
                   track_formats: %i[gpx fit], create_map: true, map_layers: [],
                   adoc_author: nil, short_species_names: false)
  out_dir = Pathname(out_dir || dir)
  out_file = Pathname(out_file || "#{Pathname(dir).realpath.basename}.adoc")
  reports = Report.load_directory(
    dir, track_formats: track_formats, create_map: create_map, overwrite_map: overwrite, map_layers: map_layers
  )
  reports.map.with_index(1) do |report, index|
    filename = if index < 2
                 out_dir.join(out_file)
               else
                 out_dir.join("#{out_file.basename('.*')} (#{index})#{out_file.extname}")
               end
    report.save_adoc(filename, overwrite: overwrite, author: adoc_author, short_species_names: short_species_names)
    filename.to_s
  end
end