Class: Rain::Pages

Inherits:
Object
  • Object
show all
Includes:
LowType
Defined in:
lib/pages/pages.rb

Defined Under Namespace

Classes: Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata:) ⇒ Pages

Returns a new instance of Pages.



13
14
15
16
17
18
19
# File 'lib/pages/pages.rb', line 13

def initialize(metadata:)
  @file_paths = .file_types.values_at('md', 'rd', 'markdown', 'raindown').flat_map { it }.compact
  @url_paths = {}
  @tags = {}

  process_files
end

Instance Attribute Details

#url_pathsObject (readonly)

Returns the value of attribute url_paths.



9
10
11
# File 'lib/pages/pages.rb', line 9

def url_paths
  @url_paths
end

Class Method Details

.pages_pathObject



134
135
136
# File 'lib/pages/pages.rb', line 134

def pages_path
  File.expand_path('app/pages', Dir.pwd)
end

.url_path(file_path:) ⇒ Object

Remove segments beginning with "_" and ending with "&", "-" or "/". Example: app/pages/docs/_basics/_1-getting-started.md => app/pages/docs/getting-started.md



123
124
125
126
127
128
129
130
131
132
# File 'lib/pages/pages.rb', line 123

def url_path(file_path:)
  url_path = file_path.split(Regexp.union(['/', '&'])).map do |segment|
    next segment.sub(/^_\d\W/, '') if segment.sub(/^_\d\W/, '') != segment
    next nil if segment.sub(/^_/, '') != segment

    segment
  end.compact.join('/')

  url_path.delete_prefix(pages_path).delete_suffix(File.extname(url_path))
end

Instance Method Details

#list(**typed_tags) ⇒ Object



31
32
33
34
35
# File 'lib/pages/pages.rb', line 31

def list(**typed_tags)
  file_paths = tagged(**typed_tags)
  sorted_paths = file_paths.sort_by { order(it) }
  sorted_paths.map { |file_path| present_file(file_path:) }
end

#page(path:) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/pages/pages.rb', line 21

def page(path:)
  path = '/home' if path == '/'
  file_path = @url_paths[path] || return

  , markdown = parse_file(file_path:)
  raindown = Raindown.render(markdown:)

  Page.new(, raindown)
end

#tagged(**typed_tags) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/pages/pages.rb', line 37

def tagged(**typed_tags)
  file_paths = []

  typed_tags.each do |type, tag|
    file_paths = [*file_paths, *@tags.dig(type, tag)]
  end

  file_paths
end