Class: Ace::Support::Nav::Atoms::ExtensionInferrer
- Inherits:
-
Object
- Object
- Ace::Support::Nav::Atoms::ExtensionInferrer
- Defined in:
- lib/ace/support/nav/atoms/extension_inferrer.rb
Overview
Infers file extensions for protocol-based resource resolution Implements DWIM (Do What I Mean) extension inference
Constant Summary collapse
- DEFAULT_FALLBACK_ORDER =
Default extension inference order when not configured
%w[ protocol_shorthand protocol_full generic_markdown bare ].freeze
Class Method Summary collapse
-
.extract_shorthand_extensions(protocol_extensions) ⇒ Array<String>
Extract shorthand extensions from full protocol extensions e.g., from [“.g.md”, “.guide.md”] extract [“.g”].
-
.has_extension?(pattern, extensions) ⇒ Boolean
Check if a pattern already includes an extension from the list.
-
.infer_extensions(pattern, protocol_extensions: [], enabled: true, fallback_order: DEFAULT_FALLBACK_ORDER) ⇒ Array<String>
Generate candidate extensions for a pattern based on protocol config.
-
.strip_extension(pattern, extensions) ⇒ String
Get the base pattern without any extension.
Instance Method Summary collapse
- #extract_shorthand_extensions ⇒ Object
- #has_extension? ⇒ Boolean
-
#infer_extensions ⇒ Object
Instance method wrappers for backwards compatibility (deprecated).
- #strip_extension ⇒ Object
Class Method Details
.extract_shorthand_extensions(protocol_extensions) ⇒ Array<String>
Extract shorthand extensions from full protocol extensions e.g., from [“.g.md”, “.guide.md”] extract [“.g”]
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 80 def extract_shorthand_extensions(protocol_extensions) return [] if protocol_extensions.nil? || protocol_extensions.empty? shorthand_extensions = [] protocol_extensions.each do |ext| # Extract the part before the first dot after the initial dot # e.g., ".g.md" -> ".g", ".guide.md" -> ".guide" parts = ext.split(".") if parts.length >= 2 shorthand = ".#{parts[1]}" shorthand_extensions << shorthand unless shorthand_extensions.include?(shorthand) end end shorthand_extensions end |
.has_extension?(pattern, extensions) ⇒ Boolean
Check if a pattern already includes an extension from the list
69 70 71 72 73 74 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 69 def has_extension?(pattern, extensions) return false if pattern.nil? || pattern.empty? return false if extensions.nil? || extensions.empty? extensions.any? { |ext| pattern.end_with?(ext) } end |
.infer_extensions(pattern, protocol_extensions: [], enabled: true, fallback_order: DEFAULT_FALLBACK_ORDER) ⇒ Array<String>
Generate candidate extensions for a pattern based on protocol config
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 25 def infer_extensions(pattern, protocol_extensions: [], enabled: true, fallback_order: DEFAULT_FALLBACK_ORDER) return [pattern] unless enabled return [pattern] if pattern.empty? # Guard against nil fallback_order fallback_order ||= DEFAULT_FALLBACK_ORDER # Extract shorthand extensions from protocol extensions # e.g., from [".g.md", ".guide.md"] extract [".g"] shorthand_extensions = extract_shorthand_extensions(protocol_extensions) candidates = [] fallback_order.each do |fallback_type| case fallback_type when "protocol_shorthand" # Try with shorthand extensions first (e.g., ".g") shorthand_extensions.each do |ext| candidate = "#{pattern}#{ext}" candidates << candidate unless candidates.include?(candidate) end when "protocol_full" # Try with full protocol extensions (e.g., ".g.md", ".guide.md") protocol_extensions.each do |ext| candidate = "#{pattern}#{ext}" candidates << candidate unless candidates.include?(candidate) end when "generic_markdown" # Try with generic markdown extension (e.g., ".md") candidate = "#{pattern}.md" candidates << candidate unless candidates.include?(candidate) when "bare" # Try with no extension candidates << pattern unless candidates.include?(pattern) end end candidates end |
.strip_extension(pattern, extensions) ⇒ String
Get the base pattern without any extension
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 102 def strip_extension(pattern, extensions) return pattern if extensions.nil? || extensions.empty? result = pattern.dup extensions.each do |ext| result = result.sub(/#{Regexp.escape(ext)}\z/, "") if result.end_with?(ext) end result end |
Instance Method Details
#extract_shorthand_extensions ⇒ Object
123 124 125 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 123 def extract_shorthand_extensions(...) self.class.extract_shorthand_extensions(...) end |
#has_extension? ⇒ Boolean
119 120 121 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 119 def has_extension?(...) self.class.has_extension?(...) end |
#infer_extensions ⇒ Object
Instance method wrappers for backwards compatibility (deprecated)
115 116 117 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 115 def infer_extensions(...) self.class.infer_extensions(...) end |
#strip_extension ⇒ Object
127 128 129 |
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 127 def strip_extension(...) self.class.strip_extension(...) end |