Module: Metanorma::Standoc::Attachment

Included in:
Bibitem, Cleanup
Defined in:
lib/metanorma/cleanup/attachment.rb

Instance Method Summary collapse

Instance Method Details

#attachment_cleanup(xmldoc) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/metanorma/cleanup/attachment.rb', line 4

def attachment_cleanup(xmldoc)
  xmldoc.xpath("//bibitem[uri/@type = 'attachment']").each do |b|
    b["hidden"] = "true"
    b.at("./docidentifier[@type = 'title']")&.remove
    u = b.at("./uri[@type = 'attachment']")
    c = b.at("./uri[@type = 'citation']") ||
      u.after("<uri type='citation'/>")
    uri = attachment_uri(u.text, b)
    u.children = uri
    c.children = uri
  end
end

#attachment_location(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/metanorma/cleanup/attachment.rb', line 35

def attachment_location(path)
  f = path
  @datauriattachment and
    f = File.join(@attachmentsdir, File.basename(path))
  Pathname.new(File.expand_path(f))
    .relative_path_from(Pathname.new(File.expand_path(@output_dir))).to_s
end

#attachment_uri(path, bib) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/metanorma/cleanup/attachment.rb', line 17

def attachment_uri(path, bib)
  init_attachments
  path = File.join(@localdir, path)
  valid_attachment?(path, bib) or return ""
  @datauriattachment or return attachment_location(path)
  save_attachment(path, bib)
end

#datauri_attachment(path, doc) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/metanorma/cleanup/attachment.rb', line 43

def datauri_attachment(path, doc)
  @datauriattachment or return
  m = add_misc_container(doc)
  f = attachment_location(path)
  e = (m << "<attachment name='#{f}'/>").last_element_child
  Vectory::Utils::datauri(path, @output_dir).scan(/.{1,60}/)
    .each { |dd| e << "#{dd}\n" }
  f
end

#init_attachmentsObject



60
61
62
63
64
65
66
67
# File 'lib/metanorma/cleanup/attachment.rb', line 60

def init_attachments
  @datauriattachment or return
  @attachmentsdir and return
  @attachmentsfld = "_#{@filename}_attachments"
  @attachmentsdir = File.join(@output_dir, @attachmentsfld)
  FileUtils.rm_rf(@attachmentsdir)
  FileUtils.mkdir_p(@attachmentsdir)
end

#save_attachment(path, bib) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/metanorma/cleanup/attachment.rb', line 25

def save_attachment(path, bib)
  init_attachments
  f = File.basename(path)
  File.exist?(File.join(@attachmentsdir, f)) and
    f += "_#{UUIDTools::UUID.random_create}"
  out_fld = File.join(@attachmentsdir, f)
  FileUtils.cp(path, out_fld)
  datauri_attachment(out_fld, bib.document)
end

#valid_attachment?(path, bib) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/metanorma/cleanup/attachment.rb', line 53

def valid_attachment?(path, bib)
  File.exist?(path) and return true
  p = Pathname.new(path).cleanpath
  @log.add("STANDOC_9", bib, params: [p])
  false
end