lib/octopress/page.rb in octopress-3.0.0.alpha3 vs lib/octopress/page.rb in octopress-3.0.0.alpha4

- old
+ new

@@ -8,11 +8,11 @@ @content = options['content'] || content end def write if File.exist?(path) && !@options['force'] - abort "File #{relative_path} already exists" if File.exist?(path) + abort "File #{relative_path} already exists" end FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') { |f| f.write(@content) } if STDOUT.tty? @@ -25,31 +25,37 @@ def relative_path local = Dir.pwd + '/' path.sub(local, '') end + def source + Configuration.jekyll_config(@options)['source'] + end + def path + return @path if @path file = @options['path'] # If path ends with a slash, make it an index - file << "index" if file =~ /\/$/ + file += "index" if file =~ /\/$/ # if path has no extension, add the default extension - file << ".#{extension}" unless file =~ /\.\w+$/ + file += ".#{extension}" unless file =~ /\.\w+$/ - File.join(@config['source'], file) + @path = File.join(source, file) end def extension @options['extension'].sub(/^\./, '') end def set_default_options @options['type'] ||= 'page' - @options['layout'] = @config['octopress']['new_page_layout'] + @options['layout'] = @config['new_page_layout'] @options['date'] = convert_date @options['date'] - @options['extension'] ||= @config['octopress']['new_page_extension'] + @options['extension'] ||= @config['new_page_extension'] + @options['template'] ||= @config['new_page_template'] end def convert_date(date) if date begin @@ -62,21 +68,32 @@ # Load the user provide or default template for a new post. # def content file = @options['template'] - file = File.join(Octopress.site.source, file) if file + file = File.join(source, '_templates', file) if file if file - raise "No template found at #{file}" unless File.exist? file + abort "No template found at #{file}" unless File.exist? file parse_template Pathname.new(file).read else parse_template default_content end end + # Render Liquid vars in YAML front-matter. def parse_template(input) - template = Liquid::Template.parse(input) - template.render(@options) + + # If possible only parse the YAML front matter. + # If YAML front-matter dashes aren't present parse the whole + # template and add dashes. + # + parsed = if input =~ /\A-{3}\s+(.+?)\s+-{3}\s+(.+)/m + template = Liquid::Template.parse($1) + "---\n#{template.render(@options)}\n---\n\n#{$2}" + else + template = Liquid::Template.parse(input) + "---\n#{template.render(@options)}\n---\n\n" + end end def date_slug Time.parse(@options['date']).strftime('%Y-%m-%d') end