Class: Toto::Site

Inherits:
Object show all
Defined in:
lib/toto.rb

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



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

def initialize config
  @config = config
end

Instance Method Details

#/Object



96
97
98
# File 'lib/toto.rb', line 96

def /
  self[:root]
end

#[](*args) ⇒ Object



66
67
68
# File 'lib/toto.rb', line 66

def [] *args
  @config[*args]
end

#[]=(key, value) ⇒ Object



70
71
72
# File 'lib/toto.rb', line 70

def []= key, value
  @config.set key, value
end

#archives(filter = "") ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/toto.rb', line 81

def archives filter = ""
  entries = ! self.articles.empty??
    self.articles.select do |a|
      filter !~ /^\d{4}/ || File.basename(a) =~ /^#{filter}/
    end.reverse.map do |article|
      Article.new article, @config
    end : []

  return {:archives => Archives.new(entries, @config)}
end

#article(route) ⇒ Object



92
93
94
# File 'lib/toto.rb', line 92

def article route
  Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load
end

#go(route, type = :html) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/toto.rb', line 115

def go route, type = :html
  route << self./ if route.empty?
  type, path = type =~ /html|xml|json/ ? type.to_sym : :html, route.join('/')
  context = lambda do |data, page|
    Context.new(data, @config, path).render(page, type)
  end

  body, status = if Context.new.respond_to?(:"to_#{type}")
    if path =~ permalink_style
      Context.new(article(route), @config, path).render(:article, type)
    elsif route.first =~ /\d{4}/
      Context.new(archives(route * '-'), @config, path).render(:archives, type)
    elsif respond_to?(route = route.first.to_sym)
      Context.new(send(route, type), @config, path).render(route, type)
    elsif (repo = @config[:github][:repos].grep(/#{path}/).first) &&
          !@config[:github][:user].empty?
      context[Repo.new(repo, @config), :repo]
    else
      context[{}, path.to_sym]
    end
  else
    http 400
  end

rescue Errno::ENOENT => e
  return {:body => http(404).first, :type => :html, :status => 404}
else
  return {:body => body || "", :type => type, :status => status || 200}
end

#index(type = :html) ⇒ Object



74
75
76
77
78
79
# File 'lib/toto.rb', line 74

def index type = :html
  articles = type == :html ? self.articles.reverse : self.articles
  {:articles => articles.map do |article|
    Article.new article, @config
  end}.merge archives
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/toto.rb', line 100

def permalink_style
  style = ''
  @config[:permalink].split("/").each do |sub|
    case sub
    when ":year"  then style += '\d{4}\/'
    when ":month" then style += '\d{2}\/'
    when ":day"   then style += '\d{2}\/'
    when ":title" then style += '[a-zA-Z0-9\-_]+'
    else
      style += '.+\/'
    end
  end
  Regexp.new style
end