Class: OKF::Bundle::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/okf/bundle/reader.rb

Overview

Reads an OKF bundle directory into an in-memory OKF::Bundle. Together with Bundle::Writer this is the only component that touches the filesystem — the core (Bundle, Concept, Graph, Validator, Linter) then works purely in memory.

It parses eagerly: each concept file becomes an OKF::Concept, each index.md/log.md is kept as raw text (its structure is validated as text), and a concept file whose frontmatter does not parse is retained as an unparseable entry (carrying the ParseError message, so §9.1 can report it) rather than dropped or raised. Every read goes through Path.join_under! so a symlinked or crafted path cannot escape the bundle root.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Reader

Returns a new instance of Reader.



22
23
24
# File 'lib/okf/bundle/reader.rb', line 22

def initialize(dir)
  @root = File.expand_path(dir.to_s)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



20
21
22
# File 'lib/okf/bundle/reader.rb', line 20

def root
  @root
end

Class Method Details

.read(dir) ⇒ Object



16
17
18
# File 'lib/okf/bundle/reader.rb', line 16

def self.read(dir)
  new(dir).read
end

Instance Method Details

#readObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/okf/bundle/reader.rb', line 26

def read
  concepts = []
  reserved = []
  unparseable = []

  markdown_paths.each do |path|
    begin
      content = File.read(Path.join_under!(@root, path), encoding: "UTF-8")
      if Concept.reserved?(path)
        reserved << Entry.new(path: path, content: content)
      else
        frontmatter, body = Markdown::Frontmatter.parse(content)
        concepts << Concept.new(path: path, frontmatter: frontmatter, body: body)
      end
    rescue Markdown::Frontmatter::ParseError => e
      unparseable << Entry.new(path: path, content: content, error: e.message)
    end
  end

  Bundle.new(concepts: concepts, reserved: reserved, unparseable: unparseable, root: @root)
end