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
- .binary_format?(format) ⇒ Boolean
- .capabilities ⇒ Object
- .detect_format(filename) ⇒ Object
- .get_format(format_name) ⇒ Object
-
.lazy_load_format(format_name) ⇒ Object
If a format gem (e.g. coradoc-adoc) is in the bundle but has not been required yet, attempt to require it on first lookup.
- .normalize_format(name) ⇒ Object
- .parse_format?(format) ⇒ Boolean
- .register_format(format_name, format_module, **options) ⇒ Object
- .registered_formats ⇒ Object
- .registry ⇒ Object
- .resolve_output_format(output_file, default: :html) ⇒ Object
- .serialize_format?(format) ⇒ Boolean
Class Method Details
.binary_format?(format) ⇒ Boolean
56 57 58 59 |
# File 'lib/coradoc/format_catalog.rb', line 56 def binary_format?(format) opts = registry.(format) opts&.fetch(:binary, false) == true end |
.capabilities ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/coradoc/format_catalog.rb', line 86 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
47 48 49 50 51 52 53 54 |
# File 'lib/coradoc/format_catalog.rb', line 47 def detect_format(filename) ext = File.extname(filename).downcase registry.each_key do |name| opts = registry.(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) || lazy_load_format(format_name) end |
.lazy_load_format(format_name) ⇒ Object
If a format gem (e.g. coradoc-adoc) is in the bundle but has not
been required yet, attempt to require it on first lookup. This
removes the friction where Coradoc.parse(text, format: :asciidoc)
blows up just because the user only wrote require "coradoc" and
never explicitly required coradoc/asciidoc. The require is for
an external gem, not internal library code, so it stays within
the autoload-over-require_relative rule.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/coradoc/format_catalog.rb', line 31 def lazy_load_format(format_name) return nil if format_name.nil? || format_name.to_s.empty? begin require "coradoc/#{format_name}" rescue LoadError return nil end registry.get(format_name) end |
.normalize_format(name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/coradoc/format_catalog.rb', line 61 def normalize_format(name) return nil unless name key = name.to_s.downcase registry.each_key do |fmt_name| opts = registry.(fmt_name) return fmt_name if opts[:aliases]&.include?(key) end key.to_sym end |
.parse_format?(format) ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/coradoc/format_catalog.rb', line 79 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, **) format_module.extend(FormatModule::Interface) unless format_module.is_a?(FormatModule::Interface) registry.register(format_name, format_module, ) FormatModule.validate!(format_module, format_name) end |
.registered_formats ⇒ Object
43 44 45 |
# File 'lib/coradoc/format_catalog.rb', line 43 def registered_formats registry.list end |
.registry ⇒ Object
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
95 96 97 98 99 |
# File 'lib/coradoc/format_catalog.rb', line 95 def resolve_output_format(output_file, default: :html) return default unless output_file detect_format(output_file) || default end |
.serialize_format?(format) ⇒ Boolean
72 73 74 75 76 77 |
# File 'lib/coradoc/format_catalog.rb', line 72 def serialize_format?(format) mod = get_format(format) return false unless mod mod.serialize? end |