Class: JekyllPagesApi::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_pages_api/page.rb

Overview

wrapper for a Jekyll::Page

Constant Summary collapse

HTML_EXTS =
%w(.html .md .markdown .textile).to_set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, site) ⇒ Page

Jekyll::StaticFile doesn't expose a site accessor, so we require an explicit site argument here.



11
12
13
14
# File 'lib/jekyll_pages_api/page.rb', line 11

def initialize(page, site)
  @page = page
  @site = site
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



7
8
9
# File 'lib/jekyll_pages_api/page.rb', line 7

def page
  @page
end

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'lib/jekyll_pages_api/page.rb', line 7

def site
  @site
end

Instance Method Details

#base_urlObject



31
32
33
# File 'lib/jekyll_pages_api/page.rb', line 31

def base_url
  self.site.baseurl
end

#body_textObject



45
46
47
48
49
# File 'lib/jekyll_pages_api/page.rb', line 45

def body_text
  output = self.page.content if self.page.respond_to?(:content)
  output ||= File.read(self.page.path)
  self.filterer.text_only(output)
end

#filtererObject



21
22
23
# File 'lib/jekyll_pages_api/page.rb', line 21

def filterer
  @filterer ||= Filters.new
end

#html?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/jekyll_pages_api/page.rb', line 16

def html?
  path = self.page.path
  path.end_with?('/') || HTML_EXTS.include?(File.extname(path))
end

#rel_pathObject



35
36
37
38
39
# File 'lib/jekyll_pages_api/page.rb', line 35

def rel_path
  path = self.page.url if self.page.respond_to?(:url)
  path ||= self.page.relative_path if self.page.respond_to?(:relative_path)
  path
end

#skip_index?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/jekyll_pages_api/page.rb', line 55

def skip_index?
  (self.page.data['skip_index'] if self.page.respond_to?(:data)) || false
end

#tagsObject



51
52
53
# File 'lib/jekyll_pages_api/page.rb', line 51

def tags
  (self.page.data['tags'] if self.page.respond_to?(:data)) || []
end

#titleObject



25
26
27
28
29
# File 'lib/jekyll_pages_api/page.rb', line 25

def title
  title = self.page.data['title'] if self.page.respond_to?(:data)
  title ||= self.page.title if self.page.respond_to?(:title)
  self.filterer.decode_html(title || '')
end

#to_jsonObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll_pages_api/page.rb', line 59

def to_json
  optional = {}
  optional['skip_index'] = true if self.skip_index?
  optional.merge({
    title: self.title,
    url: self.url,
    tags: self.tags,
    body: self.body_text
  })
end

#urlObject



41
42
43
# File 'lib/jekyll_pages_api/page.rb', line 41

def url
  [self.base_url, rel_path].join
end