Skip to content

Commit

Permalink
set formats for rendering from config: #77
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Aug 22, 2024
1 parent 83c1819 commit 5a04b1d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
34 changes: 27 additions & 7 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 @@ -87,10 +92,23 @@ def initialize(*args)
@yaml = File.join(File.dirname(self.class::_file || __FILE__), "..",
"..", YAML_CONFIG_FILE)
set_default_values_from_yaml_file(@yaml) if File.file?(@yaml)
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

def default_formats
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 Down Expand Up @@ -118,7 +136,6 @@ def set_default_values_from_yaml_file(config_file)
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 +152,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
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 5a04b1d

Please sign in to comment.