Skip to content

Commit

Permalink
Merge pull request #82 from metanorma/features/formats
Browse files Browse the repository at this point in the history
set formats for rendering from config: https://github.com/metanorma/m…
  • Loading branch information
opoudjis authored Aug 22, 2024
2 parents 83c1819 + c12c198 commit dab0e02
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 27 deletions.
65 changes: 50 additions & 15 deletions lib/metanorma/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Generic # rubocop:disable Style/MutableConstant
DOCUMENT_NAMESPACE = "https://www.metanorma.org/ns/generic"
YAML_CONFIG_FILE = "metanorma.yml"

class DummyProcessor < ::Metanorma::Processor
def initialize; end # rubocop:disable Lint/MissingSuper
end

class Configuration
CONFIG_ATTRS = %i[
organization_name_short
Expand Down Expand Up @@ -62,6 +66,7 @@ class Configuration
word_footnotefontsize
xml_root_tag
pdf_stylesheet
formats
].freeze

def filepath_attrs
Expand All @@ -86,11 +91,31 @@ def initialize(*args)
# 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)
set_default_values_from_yaml_file(@yaml)
end

# may be invoked multiple times, needs to not overwrite
def postprocess_defaults
default_org
default_formats
default_titles
end

def default_org
self.organization_name_short ||= ORGANIZATION_NAME_SHORT
self.organization_name_long ||= ORGANIZATION_NAME_LONG
self.document_namespace ||= DOCUMENT_NAMESPACE
default_titles
end

# convert array to hash; if already is hash (no override in customize),
# don't reconvert
def default_formats
formats.is_a?(Hash) and return
self.formats ||= %w(html doc)
self.formats = self.formats.each_with_object({}) do |k, m|
m[k.to_sym] = k
end
self.formats.merge! DummyProcessor.new.output_formats
end

def default_titles
Expand All @@ -107,18 +132,25 @@ def default_titles
end

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)
if File.file?(config_file)
root_path, default_config_options =
set_default_values_from_yaml_file_prep(config_file)
CONFIG_ATTRS.each do |attr|
set_default_value_from_yaml_file(attr, root_path,
default_config_options)
end
end
postprocess_defaults
end

def set_default_value_from_yaml_file(attr, root_path, default_options)
value = default_options[attr.to_s]
value && filepath_attrs.include?(attr) and
value = absolute_path(value, root_path)
instance_variable_set("@#{attr}", value)
end

def set_default_values_from_yaml_file_prep(config_file)
# root_path = File.dirname(self.class::_file || __FILE__)
root_path = File.dirname(config_file)
default_config_options =
YAML.safe_load(File.read(config_file, encoding: "UTF-8"))
Expand All @@ -135,11 +167,14 @@ def blank?(val)
end

def absolute_path(value, root_path)
if value.is_a? Hash then absolute_path1(value, root_path)
elsif value.is_a? Array then absolute_path1_array(value, root_path)
elsif value.is_a?(String) && !value.empty? &&
!Pathname.new(value).absolute?
Pathname.new(File.join(root_path, value)).cleanpath.to_s
case value
when Hash then absolute_path1(value, root_path)
when Array then absolute_path1_array(value, root_path)
when String
if !value.empty? && !Pathname.new(value).absolute?
Pathname.new(File.join(root_path, value)).cleanpath.to_s
else value
end
else value
end
end
Expand Down
13 changes: 12 additions & 1 deletion lib/metanorma/generic/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "metanorma/standoc/converter"
require "fileutils"
require_relative "front"
require "metanorma"
require "pathname"

module Metanorma
module Generic
Expand Down Expand Up @@ -58,6 +60,10 @@ def doctype(node)
def read_config_file(path_to_config_file)
Metanorma::Generic.configuration
.set_default_values_from_yaml_file(path_to_config_file)
# reregister Processor to Metanorma with updated values
if defined? Metanorma::Registry
Metanorma::Registry.instance.register(Metanorma::Generic::Processor)
end
end

def sectiontype_streamline(ret)
Expand All @@ -75,7 +81,12 @@ def sectiontype_streamline(ret)
end

def document(node)
read_config_file(node.attr("customize")) if node.attr("customize")
if node.attr("customize")
p = node.attr("customize")
(Pathname.new p).absolute? or
p = File.expand_path(File.join(Metanorma::Utils::localdir(node), p))
read_config_file(p)
end
super
end

Expand Down
5 changes: 1 addition & 4 deletions lib/metanorma/generic/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ def initialize # rubocop:disable Lint/MissingSuper
end

def output_formats
super.merge(
html: "html",
doc: "doc",
)
configuration.formats
end

def fonts_manifest
Expand Down
8 changes: 6 additions & 2 deletions metanorma.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ logo_paths:
- /metanorma-mine/lib/isodoc/mine/html/logo1.jpg
- /metanorma-mine/lib/isodoc/mine/html/logo2.jpg
validate_rng_file: /metanorma-mine/lib/metanorma/mine/mine.rng
formats:
- html
- pdf
- doc
htmlcoverpage: /metanorma-mine/lib/isodoc/mine/html/html_mine_titlepage.html
htmlintropage: /metanorma-mine/lib/isodoc/mine/html/html_mine_intro.html
htmlstylesheet: /metanorma-mine/lib/isodoc/mine/html/htmlstyle.scss
Expand Down Expand Up @@ -74,5 +78,5 @@ normref_titles:
bibliography_titles:
- Bibliography
fonts_manifest:
- Font1
- Font2
Font1:
Font2:
47 changes: 42 additions & 5 deletions spec/metanorma/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,49 @@
expect(processor).not_to be nil
end

it "registers output formats against metanorma" do
output = <<~OUTPUT
[[:doc, "doc"], [:html, "html"], [:presentation, "presentation.xml"], [:rxl, "rxl"], [:xml, "xml"]]
OUTPUT
context "configure processor" do
before do
FileUtils.rm_f(Metanorma::Generic::YAML_CONFIG_FILE)
end

after do
FileUtils.rm_f(Metanorma::Generic::YAML_CONFIG_FILE)
end

it "registers output formats against metanorma" do
output = <<~OUTPUT
[[:doc, "doc"], [:html, "html"], [:presentation, "presentation.xml"], [:rxl, "rxl"], [:xml, "xml"]]
OUTPUT

Metanorma::Generic.configuration = nil
Metanorma::Generic.configure {}
expect(Metanorma::Generic::Processor.new.output_formats.sort.to_s)
.to be_equivalent_to output
end

it "sets output formats by configuration" do
output = <<~OUTPUT
[[:html, "html"], [:pdf, "pdf"], [:presentation, "presentation.xml"], [:rxl, "rxl"], [:xml, "xml"]]
OUTPUT

yaml_content = { "formats" => %w(html pdf) }

FileUtils.rm_f(Metanorma::Generic::YAML_CONFIG_FILE)
File.new(Metanorma::Generic::YAML_CONFIG_FILE, "w+").tap do |file|
file.puts(yaml_content.to_yaml)
end.close

Metanorma::Generic.configuration = nil
Metanorma::Generic.configure {}
expect(Metanorma::Generic::Processor.new.output_formats.sort.to_s)
.to be_equivalent_to output.to_s

Metanorma::Generic.configure do |config|
config.formats = Metanorma::Generic::Configuration.new.formats
end

expect(processor.output_formats.sort.to_s).to be_equivalent_to output
FileUtils.rm_f(Metanorma::Generic::YAML_CONFIG_FILE)
end
end

it "registers version against metanorma" do
Expand Down

0 comments on commit dab0e02

Please sign in to comment.