lib/octopress-ink/assets/asset.rb in octopress-ink-1.0.0.rc.13 vs lib/octopress-ink/assets/asset.rb in octopress-ink-1.0.0.rc.14

- old
+ new

@@ -3,10 +3,12 @@ module Assets class Asset attr_reader :plugin, :dir, :base, :root, :file attr_accessor :exists + FRONT_MATTER = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m + def initialize(plugin, base, file) @file = file @base = base @plugin = plugin @root = plugin.assets_path @@ -55,10 +57,14 @@ end @found_file = Pathname.new files[0] end end + def ext + File.extname(filename) + end + def read path.read end def add @@ -94,31 +100,58 @@ def destination File.join(dir, file) end - private - - def source_dir - if exists? user_override_path - user_dir - else - plugin_dir + def content + unless @content + if read =~ FRONT_MATTER + @content = $POSTMATCH + else + @content = read + end end + @content end # Render file through Liquid if it contains YAML front-matter # def render - content = path.read + unless @rendered_content + if asset_payload = payload + @rendered_content = Liquid::Template.parse(content).render!(payload) + else + @rendered_content = content + end + end - if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m - payload = Octopress.site.site_payload - content = $POSTMATCH - payload['page'] = SafeYAML.load($1) - Liquid::Template.parse(content).render!(payload) + @rendered_content + end + + def payload + unless @payload + @payload = Octopress.site.site_payload + @payload['page'] = data + end + + @payload + end + + def data + if read =~ FRONT_MATTER + SafeYAML.load($1) else - content + {} + end + end + + private + + def source_dir + if exists? user_override_path + user_dir + else + plugin_dir end end def plugin_dir File.join root, base