Class: GeraBlog::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/gerablog/render.rb

Overview

Get all markdown files and return posts in html

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Render

Returns a new instance of Render.



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

def initialize(config)
  @config = config
end

Instance Attribute Details

Returns the value of attribute footer.



7
8
9
# File 'lib/gerablog/render.rb', line 7

def footer
  @footer
end

Instance Method Details

#file_to_post(filename, category) ⇒ Object



60
61
62
63
64
65
# File 'lib/gerablog/render.rb', line 60

def file_to_post(filename, category)
  md_content = File.read(filename)
  newfile = File.basename(filename).sub(/md$/, 'html')

  post_element(md_content, newfile, category)
end

#make_full_filename(filename, category) ⇒ Object



38
39
40
# File 'lib/gerablog/render.rb', line 38

def make_full_filename(filename, category)
  File.join(@config['dir']['output'], 'texts', category, filename)
end

#make_full_url(filename, category) ⇒ Object



42
43
44
# File 'lib/gerablog/render.rb', line 42

def make_full_url(filename, category)
  File.join(@config['blog']['url'], 'texts', category, filename)
end

#post_element(md_content, newfile, category) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gerablog/render.rb', line 46

def post_element(md_content, newfile, category)
  lines = md_content.split("\n")

  {
    title: lines[0][2..-1],
    category: category,
    description: lines[2][3..-1],
    date: Date.parse(newfile.match('\A(....-..-..)')[1]).rfc822,
    filename: make_full_filename(newfile, category),
    url: make_full_url(newfile, category),
    markdown: md_content
  }
end

#renderObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/gerablog/render.rb', line 13

def render
  c_dir = Dir[File.join(@config['dir']['texts'], '*')]
  c_names = c_dir.map { |d| File.basename d }
  categories_dir = Hash[c_names.zip c_dir]
  parser = Tenjin::Engine.new
  context = { categories: c_names, config: @config }
  categories = parser.render @config['template']['categories'], context

  [rendered_posts(categories_dir, categories), categories]
end

#render_post(md_render, filename, category, categories) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/gerablog/render.rb', line 67

def render_post(md_render, filename, category, categories)
  post = file_to_post(filename, category)
  post[:content] = md_render.to_html(
    post,
    post[:markdown],
    categories
  )

  post
end

#rendered_posts(categories_dir, categories) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gerablog/render.rb', line 24

def rendered_posts(categories_dir, categories)
  posts = []

  categories_dir.each do |category, dir|
    md_render = GeraBlog::RedcarpetDriver.new(category, @config)

    Dir["#{dir}/*.md"].sort.reverse.each do |file|
      posts.push render_post(md_render, file, category, categories)
    end
  end

  posts
end