Class: Rollin::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/rollin/article.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ Article

Returns a new instance of Article.



4
5
6
7
8
9
10
11
12
# File 'lib/rollin/article.rb', line 4

def initialize(source_file)
  @source_file = source_file
  filename = File.basename(@source_file)
  @id = filename[0, filename.length - 3]
  @title_from_filename = filename[11, filename.length - 11 - 3].gsub('_', ' ')
  @year = filename[0, 4].to_i
  @month = filename[5, 2].to_i
  @day = filename[8, 2].to_i
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



2
3
4
# File 'lib/rollin/article.rb', line 2

def day
  @day
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/rollin/article.rb', line 2

def id
  @id
end

#monthObject (readonly)

Returns the value of attribute month.



2
3
4
# File 'lib/rollin/article.rb', line 2

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



2
3
4
# File 'lib/rollin/article.rb', line 2

def year
  @year
end

Instance Method Details

#bodyObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/rollin/article.rb', line 66

def body
  html_renderer_options = { hard_wrap: false }
  parser_options = { autolink: true,
                     space_after_headers: true,
                     fenced_code_blocks: true,
                     disable_indented_code_blocks: true }
  render = Redcarpet::Render::HTML.new(html_renderer_options)
  redcarpet = Redcarpet::Markdown.new(render, parser_options)
  redcarpet.render(read_from_file.content)
end

#dateObject



62
63
64
# File 'lib/rollin/article.rb', line 62

def date
  Date.new(@year, @month, @day)
end

#matches?(search) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/rollin/article.rb', line 22

def matches?(search)
  return @id == search if search.is_a? String

  search = search.inject({}) { |hash, (k,v)| hash[k.to_s] = v; hash }

  return false unless published? || show_unpublished?(search)

  if search.has_key?('year')
    return false if search.delete('year') != @year
    if search.has_key?('month')
      return false if search.delete('month') != @month
      if search.has_key?('day')
        return false if search.delete('day') != @day
      end
    end
  end

  if search.keys.empty?
    return true
  else
    search.each do |key, value|
      return true if metatags[key] != nil && (metatags[key] == value || metatags[key].include?(value))
    end
    return false
  end
end

#metatagsObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rollin/article.rb', line 49

def metatags
  file = read_from_file
  return {} unless file.has_front_matter?
  begin
    return YAML.safe_load(file.yaml_front_matter)
  rescue SyntaxError => e
    puts "YAML Exception reading #{File.join(@source_file)}: #{e.message}"
  rescue Exception => e
    puts "Error reading file #{File.join(@source_file)}: #{e.message}"
  end
  return {}
end

#published?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rollin/article.rb', line 14

def published?
  !metatags.has_key?('published') || metatags['published'] == 'true'
end

#titleObject



18
19
20
# File 'lib/rollin/article.rb', line 18

def title
  metatags['title'] || @title_from_filename
end