Class: GeraBlog::Blog

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

Overview

Blog

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = './') ⇒ Blog

Returns a new instance of Blog.



64
65
66
67
68
69
70
71
# File 'lib/gerablog/blog.rb', line 64

def initialize(root = './')
  @config = ParseConfig.new
  ini_blog
  ini_dir root
  ini_template

  reset_parser
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/gerablog/blog.rb', line 20

def config
  @config
end

Instance Method Details

#create_category_dir(category) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gerablog/blog.rb', line 84

def create_category_dir(category)
  category_dir = File.join config['dir']['output'], 'texts', category

  GeraBlog.create_dir category_dir

  GeraBlog.make_dest_dir(
    File.join(@config['dir']['texts'], category, 'images'),
    category_dir,
    remove: true
  )
end

#create_dirsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gerablog/blog.rb', line 109

def create_dirs
  GeraBlog.create_dir File.join(@config['dir']['output'], 'texts')

  GeraBlog.make_dest_dir(
    @config['dir']['assets'],
    @config['dir']['output'],
    remove: true
  )

  @posts.map { |p| p[:category] }
        .uniq
        .map { |category| create_category_dir category }
end

#full_template_dir(file) ⇒ Object



45
46
47
# File 'lib/gerablog/blog.rb', line 45

def full_template_dir(file)
  File.join @config['dir']['template'], file
end

#ini_blogObject



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

def ini_blog
  @config.add 'blog',
              'root' => '.',
              'url' => 'http://localhost:8080',
              'title' => 'GeraBlog',
              'name' => 'GeraBlog Static Blog Generator',
              'description' => 'GeraBlog - My own static site generator',
              'language' => 'pt-br',
              'itens_in_rss' => '50',
              'itens_in_index' => 10
end

#ini_dir(root) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/gerablog/blog.rb', line 36

def ini_dir(root)
  @config.add 'dir',
              'root' => root,
              'texts' => File.join(root, 'texts'),
              'assets' => File.join(root, 'assets'),
              'output' => File.join(root, 'output'),
              'template' => File.join(root, 'templates')
end

#ini_templateObject



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

def ini_template
  @config.add 'template',
              'category' => full_template_dir('category.rbhtml'),
              'categories' => full_template_dir('categories.rbhtml'),
              'feed' => full_template_dir('feed.rbxml'),
              'post' => full_template_dir('post.rbhtml'),
              'footer' => full_template_dir('footer.rbhtml'),
              'header' => full_template_dir('header.rbhtml'),
              'index' => full_template_dir('index.rbhtml')
end

#load_config(config_file) ⇒ Object



73
74
75
76
# File 'lib/gerablog/blog.rb', line 73

def load_config(config_file)
  @config = ParseConfig.new(config_file)
  reset_parser
end

#new_blogObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gerablog/blog.rb', line 96

def new_blog
  @config['dir'].values.map { |dir| GeraBlog.create_dir(dir) }
  GeraBlog.make_dest_dir(
    File.join(__dir__, '..', '..', 'assets'),
    @config['dir']['root']
  )

  GeraBlog.make_dest_dir(
    File.join(__dir__, '..', '..', 'templates'),
    @config['dir']['root']
  )
end

#reset_parserObject



60
61
62
# File 'lib/gerablog/blog.rb', line 60

def reset_parser
  @parser = Tenjin::Engine.new path: @config['dir']['templates']
end

#saveObject



197
198
199
200
201
202
203
204
205
206
# File 'lib/gerablog/blog.rb', line 197

def save
  @posts, @categories = GeraBlog::Render.new(@config).render

  @posts.sort_by! { |p| File.basename p[:filename] }.reverse!

  write_posts
  write_general_rss
  write_general_html
  write_by_category_files
end

#save_configObject



78
79
80
81
82
# File 'lib/gerablog/blog.rb', line 78

def save_config
  @config.write(
    File.open(File.join(@config['dir']['root'], 'gerablog.conf'), 'w')
  )
end

#write_by_category_filesObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gerablog/blog.rb', line 171

def write_by_category_files
  @posts.map { |p| p[:category] }.uniq.each do |category|
    title = {
      url: File.join(@config['blog']['url'], 'texts', category),
      description: "#{@config['blog']['description']} (#{category})"
    }

    category_posts = @posts.select { |p| p[:category] == category }

    write_category_index(
      'feed.xml',
      @config['template']['feed'],
      category,
      title,
      category_posts
    )
    write_category_index(
      'index.html',
      @config['template']['category'],
      category,
      title,
      category_posts
    )
  end
end

#write_category_index(file, template, category, title, posts) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gerablog/blog.rb', line 123

def write_category_index(file, template, category, title, posts)
  context = {
    title: title,
    posts: posts,
    config: @config,
    categories: @categories
  }

  File.write(
    File.join(@config['dir']['output'], 'texts', category, file),
    @parser.render(template, context)
  )
end

#write_general_htmlObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gerablog/blog.rb', line 154

def write_general_html
  context = {
    config: @config,
    categories: @categories,
    title: {
      title: @config['blog']['title'],
      description: @config['blog']['description']
    },
    posts: @posts.take(Integer(@config['blog']['itens_in_index']))
  }

  File.write(
    File.join(@config['dir']['output'], 'index.html'),
    @parser.render(@config['template']['index'], context)
  )
end

#write_general_rssObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/gerablog/blog.rb', line 143

def write_general_rss
  File.write(
    File.join(@config['dir']['output'], 'feed.xml'),
    @parser.render(
      @config['template']['feed'],
      config: @config,
      posts: @posts.take(Integer(@config['blog']['itens_in_rss']))
    )
  )
end

#write_postsObject



137
138
139
140
141
# File 'lib/gerablog/blog.rb', line 137

def write_posts
  create_dirs

  @posts.map { |post| File.write(post[:filename], post[:content]) }
end