Module: Coradoc::FormatCatalog

Defined in:
lib/coradoc/format_catalog.rb

Overview

Format registry, detection, and capability introspection. Single source of truth for “what formats exist and what can they do?”, extracted from the top-level Coradoc façade. Public API on Coradoc delegates here.

Class Method Summary collapse

Class Method Details

.binary_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/coradoc/format_catalog.rb', line 37

def binary_format?(format)
  opts = registry.options_for(format)
  opts&.fetch(:binary, false) == true
end

.capabilitiesObject



67
68
69
70
71
72
73
74
# File 'lib/coradoc/format_catalog.rb', line 67

def capabilities
  registered_formats.each_with_object({}) do |name, caps|
    caps[name] = {
      parse: parse_format?(name),
      serialize: serialize_format?(name)
    }
  end
end

.detect_format(filename) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/coradoc/format_catalog.rb', line 28

def detect_format(filename)
  ext = File.extname(filename).downcase
  registry.each_key do |name|
    opts = registry.options_for(name)
    return name if opts[:extensions]&.include?(ext)
  end
  nil
end

.get_format(format_name) ⇒ Object



20
21
22
# File 'lib/coradoc/format_catalog.rb', line 20

def get_format(format_name)
  registry.get(format_name)
end

.normalize_format(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/coradoc/format_catalog.rb', line 42

def normalize_format(name)
  return nil unless name

  key = name.to_s.downcase
  registry.each_key do |fmt_name|
    opts = registry.options_for(fmt_name)
    return fmt_name if opts[:aliases]&.include?(key)
  end
  key.to_sym
end

.parse_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/coradoc/format_catalog.rb', line 60

def parse_format?(format)
  mod = get_format(format)
  return false unless mod

  mod.public_methods.include?(:parse_to_core) || mod.public_methods.include?(:parse)
end

.register_format(format_name, format_module, **options) ⇒ Object



14
15
16
17
18
# File 'lib/coradoc/format_catalog.rb', line 14

def register_format(format_name, format_module, **options)
  format_module.extend(FormatModule::Interface) unless format_module.is_a?(FormatModule::Interface)
  registry.register(format_name, format_module, options)
  FormatModule.validate!(format_module, format_name)
end

.registered_formatsObject



24
25
26
# File 'lib/coradoc/format_catalog.rb', line 24

def registered_formats
  registry.list
end

.registryObject



10
11
12
# File 'lib/coradoc/format_catalog.rb', line 10

def registry
  @registry ||= Registry.new
end

.resolve_output_format(output_file, default: :html) ⇒ Object



76
77
78
79
80
# File 'lib/coradoc/format_catalog.rb', line 76

def resolve_output_format(output_file, default: :html)
  return default unless output_file

  detect_format(output_file) || default
end

.serialize_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/coradoc/format_catalog.rb', line 53

def serialize_format?(format)
  mod = get_format(format)
  return false unless mod

  mod.serialize?
end