Class: Obsidian::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/obsidian/parser.rb,
lib/obsidian/parser/version.rb

Constant Summary collapse

VERSION =
"0.7.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vault_directory) ⇒ Parser

Returns a new instance of Parser.



22
23
24
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
# File 'lib/obsidian/parser.rb', line 22

def initialize(vault_directory)
  @index = Obsidian::Page.create_root
  markdown_parser = MarkdownParser.new

  vault_directory.glob("**/*.md").each do |path|
    dirname, basename = path.relative_path_from(vault_directory).split

    next if basename == "."

    # Remove the path component "." from the start of the dirname
    parent_slug = dirname.to_s.gsub(/\A\.\/?/, "")

    if basename.to_s == "index.md"
      slug = parent_slug.to_s.gsub(/\.md\z/, "")
    else
      title = basename.to_s.gsub(/\.md\z/, "")
      slug = Obsidian.build_slug(title, parent_slug)
    end

    @index.add_page(
      slug,
      last_modified: path.mtime,
      content: MarkdownDocument.new(path, @index, markdown_parser: markdown_parser)
    )
  end

  # TODO: capture links between notes
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



20
21
22
# File 'lib/obsidian/parser.rb', line 20

def index
  @index
end

Instance Method Details

#pagesObject



51
52
53
54
55
# File 'lib/obsidian/parser.rb', line 51

def pages
  result = []
  index.walk_tree { |page| result << page }
  result
end