Class: Dubhe::Sources::Docs

Inherits:
Object
  • Object
show all
Includes:
Dubhe::Source
Defined in:
lib/dubhe/sources/docs.rb

Constant Summary collapse

EXTENSIONS =
%w[.html .htm .md .markdown].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, store: Store.new, policy: Policy.new(profile: :docs)) ⇒ Docs

Returns a new instance of Docs.



18
19
20
21
22
# File 'lib/dubhe/sources/docs.rb', line 18

def initialize(root:, store: Store.new, policy: Policy.new(profile: :docs))
  @root = File.expand_path(root.to_s)
  @store = store
  @policy = policy
end

Instance Attribute Details

#root ⇒ Object (readonly)

Returns the value of attribute root.



16
17
18
# File 'lib/dubhe/sources/docs.rb', line 16

def root
  @root
end

Instance Method Details

#entries(filter: nil) ⇒ Object



39
40
41
42
43
# File 'lib/dubhe/sources/docs.rb', line 39

def entries(filter: nil)
  result = @store.entries(source_id: id)
  result = refresh if result.empty?
  filter ? result.select { |entry| filter.call(entry) } : result
end

#fetch_body(entry) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/dubhe/sources/docs.rb', line 45

def fetch_body(entry)
  path = resolve(entry.url.to_s.sub("file://", ""))
  if markdown?(path)
    Body.new(kind: :html, content: markdown_to_html(File.read(path)), base_url: "file://#{path}", attachments: [], headers: {})
  else
    result = @policy.apply(File.binread(path), base_url: nil)
    Body.new(kind: :html, content: result.html, base_url: "file://#{path}", attachments: [], headers: {"blocked_count" => result.blocked_count})
  end
end

#icon ⇒ Object



26
# File 'lib/dubhe/sources/docs.rb', line 26

def icon = "folder"

#id ⇒ Object



24
# File 'lib/dubhe/sources/docs.rb', line 24

def id = "docs:#{Digest::SHA256.hexdigest(@root)}"

#refresh ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/dubhe/sources/docs.rb', line 28

def refresh
  entries = files.map do |path|
    relative = Pathname.new(path).relative_path_from(Pathname.new(@root)).to_s
    Entry.new(id: "#{id}:#{relative}", source_id: id, title: title_for(path), author: nil,
      published_at: nil, updated_at: File.mtime(path), url: "file://#{path}", summary: nil,
      tags: [], state: :unread, parent_id: parent_id(relative))
  end
  entries.each { |entry| @store.save_entry(entry); @store.save_body(entry.id, fetch_body(entry)) }
  entries
end

#resolve(path) ⇒ Object



55
56
57
58
# File 'lib/dubhe/sources/docs.rb', line 55

def resolve(path)
  candidate = path.to_s.start_with?("file://") ? path.to_s.sub("file://", "") : path
  @policy.resolve_local(@root, candidate)
end

#supports?(capability) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dubhe/sources/docs.rb', line 60

def supports?(capability)
  %i[refresh search tree unread].include?(capability)
end

#title ⇒ Object



25
# File 'lib/dubhe/sources/docs.rb', line 25

def title = File.basename(@root)