Class: Architext::NativeMarkdownSource

Inherits:
Object
  • Object
show all
Defined in:
lib/architext/sources.rb

Defined Under Namespace

Classes: SearchResult, SearchToken

Constant Summary collapse

MARKDOWN_EXTENSIONS =
%w[.md .markdown].freeze
EXCLUDED_DIRECTORIES =
%w[.git .bundle node_modules vendor].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd) ⇒ NativeMarkdownSource

Returns a new instance of NativeMarkdownSource.



34
35
36
37
38
# File 'lib/architext/sources.rb', line 34

def initialize(root: Dir.pwd)
  @root = File.realpath(File.expand_path(root.to_s))
rescue Errno::ENOENT, Errno::ENOTDIR
  raise SourceNotFound, "Markdown root not found: #{root}"
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



32
33
34
# File 'lib/architext/sources.rb', line 32

def root
  @root
end

Instance Method Details

#diagnosticsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/architext/sources.rb', line 63

def diagnostics
  SourceDiagnostics.new(
    source: 'native',
    root: @root,
    vault: nil,
    vault_source: nil,
    status: 'ok',
    warning: nil,
    markdown_count: markdown_files.length,
    executable: nil,
    version: nil,
    resolved_vault_summary: nil
  )
rescue SourceError => e
  SourceDiagnostics.new(
    source: 'native',
    root: @root,
    vault: nil,
    vault_source: nil,
    status: 'error',
    warning: e.message,
    markdown_count: nil,
    executable: nil,
    version: nil,
    resolved_vault_summary: nil
  )
end

#read(path) ⇒ Object

Raises:



55
56
57
58
59
60
61
# File 'lib/architext/sources.rb', line 55

def read(path)
  absolute = resolve_relative(path)
  raise SourceNotFound, "Markdown file not found: #{path}" unless File.file?(absolute)
  raise SourceError, "Not a markdown file: #{path}" unless markdown_file?(absolute)

  read_absolute(absolute)
end

#search(query) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/architext/sources.rb', line 40

def search(query)
  tokens = parse_query(query)
  results = markdown_files.filter_map do |path|
    relative = relative_path(path)
    content = read_absolute(path)
    next unless tokens.empty? || tokens.all? { |token| token_matches?(token, relative, content) }

    SearchResult.new(path: relative, score: relevance_score(tokens, relative, content))
  end

  results.uniq(&:path)
         .sort_by { |result| [-result.score, result.path.downcase] }
         .map(&:path)
end