imathis / octopress

Octopress is an obsessively designed framework for Jekyll blogging. It’s easy to configure and easy to deploy. Sweet huh?
http://github.com/imathis/octopress
9.32k stars 2.62k forks source link

render_partial plugin: Liquid Exception: undefined method `pre_filter' SOLVED, please correct master #1639

Open PCadillac opened 10 years ago

PCadillac commented 10 years ago

on unbuntu 14.04 or windows 7 ruby --version: ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] but same pb with ruby 2.1.2

as describe in the installation document using the master branch 

(c9e0078d65 Brandon Mathis imathis authored 7 days ago) all basic plugins work, besides render_partial

Simple test post:

layout: post
title: "Test"
date: 2014-08-19 11:35:46 +0200
comments: true
categories:
---
{% render_partial ../README.markdown %}

>> rake generate
...
Liquid Exception: undefined method `pre_filter' for #<Jekyll::RenderPartialTag:0x9e11e9c> in _posts/2014-08-19-test.markdown/#excerpt

Maybe linked with the last modifications (last week(s) ) ... was OK before

rgould commented 10 years ago

:+1: experiencing this as well

PCadillac commented 10 years ago

Solved .... please correct in Github delete line 54 commented in the following (plugins/render_partial.rb)

Title: Render Partial Tag for Jekyll

Author: Brandon Mathis http://brandonmathis.com

Description: Import files on your filesystem into any blog post and render them inline.

Note: Paths are relative to the source directory, if you import a file with yaml front matter, the yaml will be stripped out.

#

Syntax {% render_partial path/to/file %}

#

Example 1:

{% render_partial about/_bio.markdown %}

#

This will import source/about/_bio.markdown and render it inline.

In this example I used an underscore at the beginning of the filename to prevent Jekyll

from generating an about/bio.html (Jekyll doesn't convert files beginning with underscores)

#

Example 2:

{% render_partial ../README.markdown %}

#

You can use relative pathnames, to include files outside of the source directory.

This might be useful if you want to have a page for a project's README without having

to duplicated the contents

# #

require 'pathname' require './plugins/octopress_filters'

module Jekyll

class RenderPartialTag < Liquid::Tag include OctopressFilters def initialize(tag_name, markup, tokens) @file = nil @raw = false if markup =~ /^(\S+)\s?(\w+)?/ @file = $1.strip @raw = $2 == 'raw' end super end

def render(context)
  file_dir = (context.registers[:site].source || 'source')
  file_path = Pathname.new(file_dir).expand_path
  file = file_path + @file

  unless file.file?
    return "File #{file} could not be found"
  end
  Dir.chdir(file_path) do
    contents = file.read
    if contents =~ /\A-{3}.+[^\A]-{3}\n(.+)/m
      contents = $1.lstrip
    end

contents = OctopressFilters::pre_filter(contents)

    if @raw
      contents
    else
      partial = Liquid::Template.parse(contents)
      context.stack do
        partial.render(context)
      end
    end
  end
end

end end

Liquid::Template.register_tag('render_partial', Jekyll::RenderPartialTag)

yous commented 10 years ago

@PCadillac Please wrap the code with three backquotes like:

require 'pathname'

...

Vaseltior commented 10 years ago

:+1: experiencing this as well

lethjakman commented 9 years ago

I disagree @PCadillac, I don't think that's the solution. Removing this line will only avoid the problem without solving it as it will stop preprocessing the contents, and it no longer renders the markdown into html. I believe it should be changed to

contents = OctopressFilters::pre_filter(contents)

Or perhaps the Include isn't working properly. However if you change it to the above you get another error that method `ext' does not exist from here. It appears to expect what I believe should be a Jekyll::Page, however it's getting a string. I'm not sure how you're supposed to craft the Jekyll::Page from what you currently have either. If anyone could point me in the right direction on fixing this I'd be more than happy to do the work and file a PR. Any ideas?

uhop commented 9 years ago

@lethjakman One way to do it (a kludge, really) is to create a fake object with two attributes: content, and ext. Set the former to a string you want to process, and the latter to "html". That should do it. After calling pre_filter we can extract the result from content.

The idea is to pattern our fake object after Page, but thankfully only two attributes are used by pre_filter.