Class: Glinda::Site

Inherits:
Object show all
Includes:
NewRelic::Agent::MethodTracer
Defined in:
lib/glinda.rb

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



75
76
77
# File 'lib/glinda.rb', line 75

def initialize config
  @config = config
end

Instance Method Details

#/Object



123
124
125
# File 'lib/glinda.rb', line 123

def /
  self[:root]
end

#[](*args) ⇒ Object



79
80
81
# File 'lib/glinda.rb', line 79

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

#[]=(key, value) ⇒ Object



83
84
85
# File 'lib/glinda.rb', line 83

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

#archives(filter = "", tag = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/glinda.rb', line 95

def archives filter = "", tag = nil
  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 : []

  if tag.nil?
    return :archives => Archives.new(entries, @config)
  else
    tagged_entries = entries.select do |e|
      if e[:tags]
        tags = e[:tags].split(',').map(&:strip).map(&:slugize)
        tags.include? tag.slugize
      else
        false
      end
    end

    return :archives => Archives.new(tagged_entries, @config), :tag => tag
  end
end

#article(route) ⇒ Object



119
120
121
# File 'lib/glinda.rb', line 119

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

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/glinda.rb', line 127

def go route, env = {}, 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, env).render(page, type)
  end

  body, status = if Context.new.respond_to?(:"to_#{type}")
    if route.first =~ /\d{4}/
      case route.size
        when 1..3
          context[archives(route * '-'), :archives]
        when 4
          context[article(route), :article]
        else http 400
      end
    elsif route.first == 'tags'
      data = {}
      if route.length == 2
        context[archives("", route[1]), :tags]
      else
        context[{tag: nil}, :tags]
      end
    elsif respond_to?(path)
      context[send(path, type), path.to_sym]
    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



87
88
89
90
91
92
# File 'lib/glinda.rb', line 87

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