Module: Natour::Asciinurse

Included in:
Report
Defined in:
lib/natour/asciinurse.rb

Instance Method Summary collapse

Instance Method Details

#save_adoc(filename, overwrite: false, author: nil, short_species_names: false) ⇒ Object



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

def save_adoc(filename, overwrite: false, author: nil, short_species_names: false)
  dir = Pathname(filename).dirname
  FileUtils.mkdir_p(dir)
  mode = File::WRONLY | File::CREAT | File::TRUNC
  mode |= File::EXCL unless overwrite
  File.open(filename, mode) do |file|
    file.write(to_adoc(
      doc_root: Pathname(path).realpath
                              .relative_path_from(dir.realpath)
                              .to_s.force_encoding('utf-8'),
      author: author,
      short_species_names: short_species_names
    ))
  end
end

#to_adoc(doc_root: '.', author: nil, short_species_names: false) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/natour/asciinurse.rb', line 23

def to_adoc(doc_root: '.', author: nil, short_species_names: false)
  distance = ->(gps_track) { "#{gps_track.distance / 1000} km" if gps_track&.distance }
  ascent = ->(gps_track) { "#{gps_track.ascent} m" if gps_track&.ascent }
  descent = ->(gps_track) { "#{gps_track.descent} m" if gps_track&.descent }

  title_image = images.find(&:landscape?)

  doc = []
  doc << "= #{title}"
  doc << author if author
  doc << ':revdate: {docdate}'
  doc << ':figure-caption!:'
  doc << ':table-caption!:'
  doc << ':pdf-page-mode: none'
  doc << ':title-page:'
  if title_image
    doc << ":title-image: #{Pathname(doc_root).join(title_image.path)}"
    doc << ':title-logo-image: image::{title-image}[top=0%]'
    doc << ''
    doc << 'ifndef::backend-pdf[]'
    doc << 'image::{title-image}[]'
    doc << 'endif::[]'
  end
  doc << ''
  doc << '<<<'
  doc << ''
  doc << '== Allgemein'
  doc << ''
  doc << '[cols="h,3"]'
  doc << '|==='
  doc << "|Datum      |#{gps_track&.date&.strftime('%d.%m.%Y')}"
  doc << "|Startzeit  |#{gps_track&.start_point&.time&.strftime('%H:%M Uhr')}"
  doc << "|Dauer      |#{gps_track&.duration&.strftime('%th:%M h')}"
  doc << "|Strecke    |#{distance.call(gps_track)}"
  doc << "|Aufstieg   |#{ascent.call(gps_track)}"
  doc << "|Abstieg    |#{descent.call(gps_track)}"
  doc << "|Ausgangsort|#{starting_point}"
  doc << "|Ankunftsort|#{arrival_point}"
  doc << '|Teilnehmer |'
  doc << '|==='
  doc << ''
  if map_image
    doc << "image::#{Pathname(doc_root).join(map_image.path)}[]"
    doc << ''
  end
  doc << '<<<'
  doc << ''
  doc << '== Beschreibung'
  doc << ''
  doc << ''
  doc << ''
  unless images.empty?
    doc << '<<<'
    doc << ''
    doc << '== Bilder'
    doc << ''
    images.each do |image|
      width = if image.landscape?
                '80%'
              else
                '40%'
              end
      doc << '.Abbildung {counter:image}'
      doc << "image::#{Pathname(doc_root).join(image.path)}[width=#{width}]"
      doc << ''
    end
  end
  unless species_lists.empty?
    doc << '<<<'
    doc << ''
    doc << '== Artenlisten'
    doc << ''
    index = 1
    groups = species_lists.group_by(&:group)
    [
      OpenStruct.new(
        group: :plants,
        title: 'Pflanzenarten',
        columns: [
          OpenStruct.new(
            header: 'Wissenschaftlicher Name',
            accessor: ->(species) { short_species_names ? BotanicalNameUtils.parse(species.name) : species.name }
          ),
          OpenStruct.new(header: 'Deutscher Name', accessor: ->(species) { species.name_de })
        ]
      ),
      OpenStruct.new(
        group: :birds,
        title: 'Vogelarten',
        columns: [
          OpenStruct.new(header: 'Deutscher Name', accessor: ->(species) { species.name_de }),
          OpenStruct.new(header: 'Wissenschaftlicher Name', accessor: ->(species) { species.name })
        ]
      )
    ].each do |info|
      group = groups.fetch(info.group, [])
      next if group.empty?

      doc << "=== #{info.title}"
      doc << ''
      group.each do |species_list|
        caption = '.Tabelle {counter:species_lists}'
        caption << ": #{species_list.description}" if species_list.description
        columns = info.columns.select do |column|
          species_list.count.zero? || species_list.any? { |species| !column.accessor.call(species).nil? }
        end
        cols = [1] + [5 * info.columns.size / columns.size] * columns.size
        doc << caption
        doc << "[cols=\"#{cols.join(',')}\",options=\"header,breakable\"]"
        doc << '|==='
        doc << "|Nr.|#{columns.map(&:header).join('|')}"
        species_list.each do |species|
          doc <<
            "|{counter:species_list#{index}}|#{columns.map { |column| column.accessor.call(species) }
                                                      .join('|')}"
        end
        doc << '|==='
        doc << ''
        index += 1
      end
    end
  end
  doc.join("\n")
end