Class: Suma::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/processor.rb

Class Method Summary collapse

Class Method Details

.run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_site") ⇒ Object



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
107
108
109
110
111
112
113
114
115
# File 'lib/suma/processor.rb', line 16

def run(metanorma_yaml_path:, schemas_all_path:, compile:, output_directory: "_site")
  Utils.log "Current directory: #{Dir.getwd}"

  # This reads the metanorma.yml file
  site_config = Suma::SiteConfig::Config.from_file(metanorma_yaml_path)

  # TODO: only reading the first file, which is a collection.yml, which is a hack...
  collection_config_path = site_config.metanorma.source.files.first
  collection_config = Suma::CollectionConfig.from_file(collection_config_path)
  collection_config.path = collection_config_path
  collection_config.manifest.expand_schemas_only("schema_docs")

  exported_schema_config = collection_config.manifest.export_schema_config(schemas_all_path)
  exported_schema_config.path = schemas_all_path

  Utils.log "Writing #{schemas_all_path}..."
  exported_schema_config.to_file
  Utils.log "Done."

  # now get rid of the source documents for schema sources

  col = Suma::SchemaCollection.new(
    config_yaml: schemas_all_path,
    manifest: collection_config.manifest,
    output_path_docs: "schema_docs",
    output_path_schemas: "plain_schemas",
  )

  if compile
    Utils.log "Compiling schema collection..."
    col.compile
  else
    Utils.log "No compile option set. Skipping schema compilation."
  end

  new_collection_config_path = "collection-output.yaml"
  collection_config.manifest.remove_schemas_only_sources
  collection_config.to_file(new_collection_config_path)

  # TODO: Do we still need this?
  # Define Proc to resolve fileref
  my_fileref_proc = proc do |ref_folder, fileref|
    # move schemas to modified_schemas
    if File.extname(fileref) == ".exp"
      fileref.gsub!(
        "../../schemas",
        "modified_schemas"
      )
    end
    File.join(ref_folder, fileref)
  end

  # TODO: Do we still need this?
  # Define Proc to resolve identifier
  my_identifier_proc = proc do |identifier|
    case identifier
    when %r{^documents/}
      identifier.gsub("documents/", "")
    else
      identifier
    end
  end

  # TODO: Do we still need this?
  # Define Proc to handle the compilation of express schemas
  express_schemas_renderer = proc do |collection_model|
  end

  # TODO: Do we still need this?
  Metanorma::Collection.tap do |mn|
    mn.set_identifier_resolver(&my_identifier_proc)
    mn.set_fileref_resolver(&my_fileref_proc)
    mn.set_pre_parse_model(&express_schemas_renderer)
  end

  if compile
    Utils.log "Compiling complete collection..."

    # TODO: Why will defining a collection immediately compile??
    metanorma_collection = Metanorma::Collection.parse(new_collection_config_path)

    # TODO: Somehow this is no longer used
    collection_opts = {
      format: [:html],
      output_folder: output_directory,
      compile: {
        install_fonts: false,
      },
      coverpage: "cover.html",
    }
    metanorma_collection.render(collection_opts)

    # remove xml files
    Dir.glob(File.join(Dir.getwd, output_directory, "*.xml")).each do |file|
      File.delete(file)
    end
  else
    Utils.log "No compile option set. Skipping collection compilation."
  end
end