Class: Ace::Support::Nav::Atoms::ExtensionInferrer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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”]

Parameters:

  • protocol_extensions (Array<String>)

    Full protocol extensions

Returns:

  • (Array<String>)

    Shorthand extensions



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

Parameters:

  • pattern (String)

    The pattern to check

  • extensions (Array<String>)

    List of extensions to check against

Returns:

  • (Boolean)

    True if pattern ends with any of the extensions



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

Parameters:

  • pattern (String)

    The search pattern (e.g., “markdown-style”)

  • protocol_extensions (Array<String>) (defaults to: [])

    Protocol-specific extensions (e.g., [“.g.md”, “.guide.md”])

  • enabled (Boolean) (defaults to: true)

    Whether extension inference is enabled

  • fallback_order (Array<String>) (defaults to: DEFAULT_FALLBACK_ORDER)

    Order of extension types to try

Returns:

  • (Array<String>)

    List of candidate patterns to try



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

Parameters:

  • pattern (String)

    The pattern to process

  • extensions (Array<String>)

    List of extensions to strip

Returns:

  • (String)

    Pattern without 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_extensionsObject



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

Returns:

  • (Boolean)


119
120
121
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 119

def has_extension?(...)
  self.class.has_extension?(...)
end

#infer_extensionsObject

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_extensionObject



127
128
129
# File 'lib/ace/support/nav/atoms/extension_inferrer.rb', line 127

def strip_extension(...)
  self.class.strip_extension(...)
end