Class: Suma::SchemaAttachment

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

Direct Known Subclasses

SchemaDocument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, output_path:) ⇒ SchemaAttachment

Returns a new instance of SchemaAttachment.



11
12
13
14
15
# File 'lib/suma/schema_attachment.rb', line 11

def initialize(schema:, output_path:)
  @schema = schema
  @id = schema.id
  @output_path = output_path
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/suma/schema_attachment.rb', line 9

def config
  @config
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/suma/schema_attachment.rb', line 9

def id
  @id
end

#output_pathObject

Returns the value of attribute output_path.



9
10
11
# File 'lib/suma/schema_attachment.rb', line 9

def output_path
  @output_path
end

#schemaObject

Returns the value of attribute schema.



9
10
11
# File 'lib/suma/schema_attachment.rb', line 9

def schema
  @schema
end

Instance Method Details

#clean_artifactsObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/suma/schema_attachment.rb', line 118

def clean_artifacts
  [
    filename_config,
    filename_adoc,
    filename_adoc("presentation.xml"),
    filename_adoc("adoc.lutaml.log.txt"),
    filename_adoc("err.html"),
  ].each do |filename|
    FileUtils.rm_rf(filename)
  end
end

#compileObject

Compile Metanorma adoc per EXPRESS schema



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/suma/schema_attachment.rb', line 95

def compile
  # TODO: Clean artifacts after compiling
  # I am commenting out because I'm playing with the schemas-only status
  # return self if File.exist?(output_xml_path)

  save_config
  save_adoc

  relative_path = Pathname.new(filename_adoc).relative_path_from(Dir.pwd)
  Utils.log "Compiling schema (id: #{id}, type: #{self.class}) => #{relative_path}"
  Metanorma::Compile.new.compile(filename_adoc, agree_to_terms: true, install_fonts: false)
  Utils.log "Compiling schema (id: #{id}, type: #{self.class}) => #{relative_path}... done!"

  # clean_artifacts

  # filename_adoc('xml')
  self
end

#filename_adoc(ext = "adoc") ⇒ Object



46
47
48
# File 'lib/suma/schema_attachment.rb', line 46

def filename_adoc(ext = "adoc")
  File.join(@output_path, "doc_#{@schema.id}.#{ext}")
end

#filename_configObject



67
68
69
# File 'lib/suma/schema_attachment.rb', line 67

def filename_config
  File.join(@output_path, "schema_#{@schema.id}.yaml")
end

#output_extensionsObject



17
18
19
# File 'lib/suma/schema_attachment.rb', line 17

def output_extensions
  "xml,html"
end

#output_folderObject



130
# File 'lib/suma/schema_attachment.rb', line 130

def output_folder; end

#output_xml_pathObject



114
115
116
# File 'lib/suma/schema_attachment.rb', line 114

def output_xml_path
  filename_adoc("xml")
end

#save_adocObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/suma/schema_attachment.rb', line 50

def save_adoc
  relative_path = Pathname.new(filename_adoc).relative_path_from(Dir.pwd)
  Utils.log "Save EXPRESS adoc: #{relative_path}"

  # return if File.exist?(filename_adoc)
  FileUtils.mkdir_p(File.dirname(filename_adoc))

  relative_path = Pathname.new(filename_config)
    .relative_path_from(Pathname.new(File.dirname(filename_adoc)))

  # Utils.log "relative_path #{relative_path}"

  File.open(filename_adoc, "w") do |file|
    file.write(to_adoc(relative_path))
  end
end

#save_configObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/suma/schema_attachment.rb', line 83

def save_config
  relative_path = Pathname.new(filename_config).relative_path_from(Dir.pwd)
  Utils.log "Save schema config: #{relative_path}"

  # Still overwrite even if the file exists
  # return if File.exist?(filename_config)
  FileUtils.mkdir_p(File.dirname(filename_config))

  to_config.save_to_path(filename_config)
end

#to_adoc(path_to_schema_yaml) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/suma/schema_attachment.rb', line 21

def to_adoc(path_to_schema_yaml)
  <<~HEREDOC
    = #{@id}
    :lutaml-express-index: schemas; #{path_to_schema_yaml};
    :bare: true
    :mn-document-class: iso
    :mn-output-extensions: #{output_extensions}

    [lutaml_express_liquid,schemas,context]
    ----
    {% for schema in context.schemas %}

    [%unnumbered]
    == #{@id}

    [source%unnumbered]
    --
    {{ schema.formatted }}
    --
    {% endfor %}
    ----

  HEREDOC
end

#to_config(path: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/suma/schema_attachment.rb', line 71

def to_config(path: nil)
  # return @config unless @config
  @config = SchemaConfig::Config.new
  @config.schemas << SchemaConfig::Schema.new(
    id: @schema.id,
    path: @schema.path,
  )
  path and @config.path = path

  @config
end