Class: Hashup::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/hashup/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSite

Returns a new instance of Site.



10
11
12
13
14
# File 'lib/hashup/site.rb', line 10

def initialize
  Slim::Engine.set_default_options pretty: true, sort_attrs: false
  @configs = self.load_config "config.yml"
  @infos = self.load_infos
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



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

def configs
  @configs
end

#infosObject (readonly)

Returns the value of attribute infos.



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

def infos
  @infos
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hashup/site.rb', line 16

def generate
  # generate posts
  get_posts.each do |post|
    article = Hashup::Post.new post 
    @infos["post"] = OpenStruct.new(article.)
    generated_post = Tilt.new("themes/_layout/_layout.slim").render(OpenStruct.new(@infos)) {
      @infos["article"] = article.contents
      Slim::Template.new("themes/post.slim").render(OpenStruct.new(@infos))
    }
    puts "#{post} generated.."
    File.open("#{@configs["output_dir"]}/#{File.basename(post, ".ma")}.html", 'w+') do |f|
      f.puts generated_post
    end
  end

  # generate index.html
  File.open("#{@configs["output_dir"]}/index.html", 'w+') do |f|
    index = Tilt.new("themes/_layout/_layout.slim").render(OpenStruct.new(@infos)) {
      @infos["list"] = self.get_generated_posts_list
      Slim::Template.new("themes/index.slim").render(OpenStruct.new(@infos))
    }
    f.puts index
    puts "index.html generated"
  end
end

#get_generated_posts_listObject



46
47
48
49
50
51
52
# File 'lib/hashup/site.rb', line 46

def get_generated_posts_list
  posts_list = []
  get_posts.each do |post|
    posts_list << (Hashup::Post.new post)
  end
  posts_list
end

#get_postsObject



42
43
44
# File 'lib/hashup/site.rb', line 42

def get_posts
  Dir.glob "#{@configs["content_dir"]}/#{@configs["posts_folder"]}/**/*.{ma, mad, md, markdown}"
end

#load_config(config_file) ⇒ Object



63
64
65
66
# File 'lib/hashup/site.rb', line 63

def load_config config_file
  configrable = Hashup::Configuration.new config_file
  configrable.configs 
end

#load_infosObject



54
55
56
57
58
59
60
61
# File 'lib/hashup/site.rb', line 54

def load_infos
  infos = {}
  Dir.glob("**/*.{yml, yaml}").map do |info|
    next unless info =~ Regexp.new(@configs["content_dir"])
    infos.merge! (YAML.load_file info)
  end
  infos
end