class TumblrThemer::Theme

Attributes

body[R]
params[R]

Public Class Methods

new(dirname, params={}) click to toggle source
# File lib/tumblr-themer/theme.rb, line 5
def initialize dirname, params={}
  @base = File.expand_path(dirname)
  @params = params

  @body = File.read(File.join(@base,'index.html'))
  @posts = {}
  Dir[File.join(@base,'posts/*.html')].each do |f|
    basename = File.basename(f).sub(/\.html$/,'')
    html = File.read(f)
    @posts[basename] = html
  end

  posts_html = @posts.collect do |basename, html|
    str = "{block:#{camelize(basename)}}"
    str << html
    str << "{/block:#{camelize(basename)}}"
  end

  @body.gsub!("{PostsCode}",posts_html.join("\n"))

  render_partials
end

Public Instance Methods

blog_data() click to toggle source
# File lib/tumblr-themer/theme.rb, line 44
def blog_data
  @blog_data || (get_data && @blog_data)
end
get_data() click to toggle source
# File lib/tumblr-themer/theme.rb, line 28
def get_data
  return @post_data if defined?(@post_data)
  if params[:id]
    data = TumblrThemer::API.all_posts(:id => params[:id])
  else
    data = TumblrThemer::API.posts
  end

  @blog_data = data['blog']
  @post_data = data['posts']
end
page() click to toggle source
# File lib/tumblr-themer/theme.rb, line 48
def page
  if params[:page]
    params[:page].to_i
  else
    1
  end
end
post_data() click to toggle source
# File lib/tumblr-themer/theme.rb, line 40
def post_data
  @post_data || (get_data && @post_data)
end
render() click to toggle source
# File lib/tumblr-themer/theme.rb, line 56
def render
  html = TumblrThemer::HtmlSnippet.new(@body)

  self.class.tag_iterators.each do |name, opts|
    vals = instance_exec(self,&opts[:blk])
    html.block(name) do |str|
      vals.collect.with_index do |val,i|
        opts[:klass].render(str,val,i)
      end.join("\n")
    end
  end

  self.class.blocks.each do |name,blk|
    html.block(name,instance_exec(self,&blk))
  end

  self.class.tags.each do |name, blk|
    html.tag(name,instance_exec(self,&blk))
  end

  html.str
end

Private Instance Methods

render_partials() click to toggle source
# File lib/tumblr-themer/theme.rb, line 117
def render_partials
  partials = {}
  Dir[File.join(@base,'partials/*.html')].each do |f|
    basename = "{partial:"<<camelize(File.basename(f).sub(/\.html$/,''))<<"}"
    html = File.read(f)
    partials[basename] = html
  end

  regex = Regexp.new("{partial:(.+?)}")
  i=0

  while match = regex.match(@body)
    if i > 5000
      raise RecusiveTag, "you probably have a recursive partial, make sure to fix that"
    else
      i += 1
    end

    if html = partials[match[0]]
      @body.gsub!(match[0],html)
    else
      raise UndefinedPartial, "Unknown partial: #{match[1]}, please place in partials/#{underscore(match[1])}.html"
    end
  end
end