Class: Metanorma::Generic::Configuration

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

Constant Summary collapse

CONFIG_ATTRS =
%i[
  organization_name_short
  organization_name_long
  bibliography_titles
  boilerplate
  committees
  document_namespace
  docid_template
  doctypes
  default_doctype
  i18nyaml
  logo_path
  logo_paths
  header
  htmlcoverpage
  htmlintropage
  htmlstylesheet
  html_bodyfont
  html_headerfont
  html_monospacefont
  html_normalfontsize
  html_monospacefontsize
  html_smallerfontsize
  html_footnotefontsize
  metadata_extensions
  metanorma_name
  normref_titles
  published_stages
  relations
  default_stage
  stage_abbreviations
  scripts
  scripts_pdf
  standardstylesheet
  symbols_titles
  termsdefs_titles
  validate_rng_file
  webfont
  wordcoverpage
  wordintropage
  wordstylesheet
  word_bodyfont
  word_headerfont
  word_monospacefont
  word_normalfontsize
  word_monospacefontsize
  word_smallerfontsize
  word_footnotefontsize
  xml_root_tag
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Configuration

Returns a new instance of Configuration.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/metanorma/generic.rb', line 82

def initialize(*args)
  super
  # Try to set config values from yaml file in current directory
  @yaml = File.join(File.dirname(self.class::_file || __FILE__), "..",
                    "..", YAML_CONFIG_FILE)
  set_default_values_from_yaml_file(@yaml) if File.file?(@yaml)
  self.organization_name_short ||= ORGANIZATION_NAME_SHORT
  self.organization_name_long ||= ORGANIZATION_NAME_LONG
  self.document_namespace ||= DOCUMENT_NAMESPACE
  default_titles
end

Class Attribute Details

._fileObject

Returns the value of attribute _file.



75
76
77
# File 'lib/metanorma/generic.rb', line 75

def _file
  @_file
end

Class Method Details

.inherited(klass) ⇒ Object

rubocop:disable Lint/MissingSuper



78
79
80
# File 'lib/metanorma/generic.rb', line 78

def self.inherited(klass) # rubocop:disable Lint/MissingSuper
  klass._file = caller_locations(1..1).first.absolute_path
end

Instance Method Details

#absolute_path(value, root_path) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/metanorma/generic.rb', line 134

def absolute_path(value, root_path)
  if value.is_a? Hash then absolute_path1(value, root_path)
  elsif value.is_a? Array
    value.reject { |a| blank?(a) }.each_with_object([]) do |v1, g|
      g << absolute_path(v1, root_path)
    end
  elsif value.is_a?(String) && !value.empty?
    File.join(root_path, "..", "..", value)
  else value
  end
end

#absolute_path1(hash, pref) ⇒ Object



146
147
148
149
150
# File 'lib/metanorma/generic.rb', line 146

def absolute_path1(hash, pref)
  hash.reject { |_k, v| blank?(v) }.transform_values do |v|
    absolute_path(v, pref)
  end
end

#blank?(val) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/metanorma/generic.rb', line 130

def blank?(val)
  val.nil? || (val.respond_to?(:empty?) && val.empty?)
end

#default_titlesObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/generic.rb', line 94

def default_titles
  self.termsdefs_titles ||=
    ["Terms and definitions", "Terms, definitions, symbols and abbreviated terms",
     "Terms, definitions, symbols and abbreviations", "Terms, definitions and symbols",
     "Terms, definitions and abbreviations", "Terms, definitions and abbreviated terms"]
  self.symbols_titles ||=
    ["Symbols and abbreviated terms", "Symbols", "Abbreviated terms",
     "Abbreviations"]
  self.normref_titles ||=
    ["Normative references"]
  self.bibliography_titles ||= ["Bibliography"]
end

#filepath_attrsObject



65
66
67
68
69
70
# File 'lib/metanorma/generic.rb', line 65

def filepath_attrs
  %i[i18nyaml boilerplate logo_path logo_paths header
     htmlcoverpage htmlintropage htmlstylesheet scripts scripts_pdf
     standardstylesheet validate_rng_file wordcoverpage wordintropage
     wordstylesheet]
end

#set_default_values_from_yaml_file(config_file) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/metanorma/generic.rb', line 107

def set_default_values_from_yaml_file(config_file)
  root_path, default_config_options =
    set_default_values_from_yaml_file_prep(config_file)
  CONFIG_ATTRS.each do |attr_name|
    value = default_config_options[attr_name.to_s]
    value && filepath_attrs.include?(attr_name) and
      value = absolute_path(value, root_path)
    instance_variable_set("@#{attr_name}", value)
  end
end

#set_default_values_from_yaml_file_prep(config_file) ⇒ Object



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

def set_default_values_from_yaml_file_prep(config_file)
  root_path = File.dirname(self.class::_file || __FILE__)
  default_config_options =
    YAML.safe_load(File.read(config_file, encoding: "UTF-8"))
  default_config_options["doctypes"].is_a? Array and
    default_config_options["doctypes"] =
      default_config_options["doctypes"].each_with_object({}) do |k, m|
        m[k] = nil
      end
  [root_path, default_config_options]
end