Class: Metanorma::Release::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/release/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, output_dir:) ⇒ Site

Returns a new instance of Site.



12
13
14
15
# File 'lib/metanorma/release/site.rb', line 12

def initialize(index:, output_dir:)
  @index = index
  @output_dir = output_dir
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/metanorma/release/site.rb', line 10

def index
  @index
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



10
11
12
# File 'lib/metanorma/release/site.rb', line 10

def output_dir
  @output_dir
end

Instance Method Details

#enrich!Object



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
# File 'lib/metanorma/release/site.rb', line 22

def enrich!
  return if index.empty?

  documents = index.publications.map do |pub|
    rxl_file = pub.files.find { |f| f.format == "rxl" }
    next pub.to_h unless rxl_file

    rxl_path = File.join(output_dir, rxl_file.path)
    next pub.to_h unless File.exist?(rxl_path)

    bib = Relaton::Bib::Item.from_xml(File.read(rxl_path))
    enriched = pub.to_h
    enriched["bibliographic"] = bib.to_h
    enriched
  rescue StandardError => e
    warn "  Skip #{pub.identifier}: #{e.message}"
    pub.to_h
  end

  dest = File.join(output_dir, "relaton")
  FileUtils.mkdir_p(dest)
  index_data = { "root" => { "title" => "Document Registry",
                             "items" => documents.compact } }
  File.write(File.join(dest, "index.json"),
             JSON.pretty_generate(index_data))
  File.write(File.join(dest, "index.yaml"), YAML.dump(index_data))
end

#package!(zip_path: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/metanorma/release/site.rb', line 50

def package!(zip_path: nil)
  require "zip"

  path = zip_path || "#{output_dir}.zip"
  Zip::File.open(path, Zip::File::CREATE) do |zipfile|
    Dir.glob("#{output_dir}/**/*").each do |file|
      next if File.directory?(file)

      entry_name = file.sub("#{File.dirname(output_dir)}/", "")
      zipfile.add(entry_name, file)
    end
  end
  path
end

#write!Object



17
18
19
20
# File 'lib/metanorma/release/site.rb', line 17

def write!
  FileUtils.mkdir_p(output_dir)
  index.write(File.join(output_dir, "index.json"))
end