class Metanorma::Cli::SiteGenerator

Attributes

asset_directory[R]
asset_folder[R]
collection_name[R]
manifest_file[R]
site_path[R]
source[R]
stylesheet[R]
template_dir[R]

Public Class Methods

generate(source, options = {}, compile_options = {}) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 22
def self.generate(source, options = {}, compile_options = {})
  new(source, options, compile_options).generate
end
new(source, options = {}, compile_options = {}) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 8
def initialize(source, options = {}, compile_options = {})
  @source = find_realpath(source)
  @site_path = options.fetch(:output_dir, "site").to_s
  @asset_folder = options.fetch(:asset_folder, "documents").to_s
  @collection_name = options.fetch(:collection_name, "documents.xml")

  @manifest_file = find_realpath(options.fetch(:config, default_config))
  @template_dir = options.fetch(:template_dir, template_data("path"))
  @stylesheet = options.fetch(:stylesheet, template_data("stylesheet"))

  @compile_options = compile_options
  ensure_site_asset_directory!
end

Public Instance Methods

generate() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 26
def generate
  site_directory = asset_directory.join("..")
  select_source_files.each { |source| compile(source) }

  Dir.chdir(site_directory) do
    build_collection_file(collection_name)
    convert_to_html_page(collection_name, "index.html")
  end
end

Private Instance Methods

build_asset_output_directory(source) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 150
def build_asset_output_directory(source)
  sub_directory = Pathname.new(source.gsub(@source.to_s, "")).dirname.to_s
  sub_directory.gsub!("/sources", "")
  sub_directory.slice!(0)

  output_directory = asset_directory.join(sub_directory)
  create_directory_if_not_present!(output_directory)

  output_directory
end
build_collection_file(collection_name) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 62
def build_collection_file(collection_name)
  collection_path = [site_path, collection_name].join("/")
  UI.info("Building collection file: #{collection_path} ...")

  Relaton::Cli::RelatonFile.concatenate(
    asset_folder,
    collection_name,
    title: manifest[:collection_name],
    organization: manifest[:collection_organization],
  )
end
compile(source) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 74
def compile(source)
  UI.info("Compiling #{source} ...")

  options = @compile_options.merge(
    format: :asciidoc, output_dir: build_asset_output_directory(source)
  )

  Metanorma::Cli::Compiler.compile(source.to_s, options)
end
config_from_manifest() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 102
def config_from_manifest
  if manifest_file
    manifest_config(YAML.safe_load(File.read(manifest_file.to_s)))
  end
end
convert_to_html_page(collection, page_name) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 84
def convert_to_html_page(collection, page_name)
  UI.info("Generating html site in #{site_path} ...")

  Relaton::Cli::XMLConvertor.to_html(collection, stylesheet, template_dir)
  File.rename(Pathname.new(collection).sub_ext(".html").to_s, page_name)
end
create_directory_if_not_present!(directory) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 146
def create_directory_if_not_present!(directory)
  FileUtils.mkdir_p(directory) unless directory.exist?
end
default_config() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 47
def default_config
  default_file = Pathname.new(Dir.pwd).join("metanorma.yml")
  default_file if File.exist?(default_file)
end
ensure_site_asset_directory!() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 139
def ensure_site_asset_directory!
  asset_path = [site_path, asset_folder].join("/")
  @asset_directory = Pathname.new(Dir.pwd).join(asset_path)

  create_directory_if_not_present!(@asset_directory)
end
extract_config_data(node, key) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 128
def extract_config_data(node, key)
  node ? node[key] : nil
end
find_realpath(source_path) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 41
def find_realpath(source_path)
  Pathname.new(source_path.to_s).realpath if source_path
rescue Errno::ENOENT
  source_path
end
manifest() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 96
def manifest
  @manifest ||= config_from_manifest || {
    files: [], collection_name: "", collection_organization: ""
  }
end
manifest_config(manifest) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 108
def manifest_config(manifest)
  {
    files: extract_config_data(
      manifest["metanorma"]["source"], "files"
    ) || [],

    collection_name: extract_config_data(
      manifest["metanorma"]["collection"], "name"
    ),

    collection_organization: extract_config_data(
      manifest["metanorma"]["collection"], "organization"
    ),

    template: extract_config_data(manifest["metanorma"], "template"),
  }
rescue NoMethodError
  raise Errors::InvalidManifestFileError.new("Invalid manifest file")
end
select_source_files() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 52
def select_source_files
  files = source_from_manifest

  if files.empty?
    files = Dir[File.join(source, "**", "*.adoc")]
  end

  files.flatten.uniq.reject { |file| File.directory?(file) }
end
source_from_manifest() click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 132
def source_from_manifest
  @source_from_manifest ||= manifest[:files].map do |source_file|
    file_path = source.join(source_file).to_s
    file_path.include?("*") ? Dir.glob(file_path) : file_path
  end.flatten
end
template_data(node) click to toggle source
# File lib/metanorma/cli/site_generator.rb, line 91
def template_data(node)
  template_node = manifest[:template]
  template_node&.fetch(node.to_s, nil)
end