从哲基尔插件返回文件列表目录? [英] Return list of files in directory from Jekyll plugin?

查看:178
本文介绍了从哲基尔插件返回文件列表目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在杰基尔插件创建过滤器或标记,这样我就可以在它的内容返回目录循环。我发现这些:

http://pastebin.com/LRfMVN5Y

http://snippets.dzone.com/posts/show/302

到目前为止,我有:

 模块哲基尔
  类FilesTag<液体::标签    高清初始化(TAG_NAME,文字,记号)
      超
      @Text =文本
    结束    高清渲染(上下文)
        ##{@文}#{} Time.now
        Dir.glob(图像/ *)各{|我| #{一世} }
        #Dir.glob(图像/ *)
        #Hash [* Dir.glob(图像/ *)收集{| V | [V,V * 2]} .flatten]
    结束
  结束
结束液体:: Template.register_tag('文件',杰基尔:: FilesTag)

我能成功返回图像列表作为一个字符串,并打印出来:

  {%文件test_string%}

但对我的生活,我不能循环阵列上,无论我怎么回报从Dir.glob数组/哈希值。我只是想能够做到:

  {文件中%%图像}
    图片
{%ENDFOR%}

我将需要能够不断地返回数组的东西,因为我将使用网站上的各种收藏。我只是需要一个准系统插件来建立在

谢谢!


更新:我部分地解决它。此方法的工作,但使用endloop_directory代替ENDFOR,这似乎有点难看我需要。此外,过滤器是无法采取像一个参数* {JPG,PNG}因为没有办法逃避的{}中的HTML。开到如何在一个属性传递一个正则表达式字符串建议...

  #usage:
#{%loop_directory目录:图像迭代器图像滤镜:*。JPG排序:降序%}
#&所述; IMG SRC ={{图像}}/>
#{%endloop_directory%}
模块哲基尔
    类LoopDirectoryTag<液体::座        包括液体:: StandardFilters
        语法= /(#{液体:: QuotedFragment} +)/?        高清初始化(TAG_NAME,标记,记号)
            @属性= {}            @属性['目录'] ='';
            @属性['迭代'] ='项目';
            @属性['过滤器'] ='项目';
            @属性['排序'] ='升';            #参数解析
            如果标记=〜语法
                markup.scan(液体:: TagAttributes)做|键,值|
                    @属性[关键] =价值
                结束
            其他
                提高SyntaxError.new(给'loop_directory插件坏选项。)
            结束            #如果@属性['目录'。零?
            #提高SyntaxError.new(你没有指定一个loop_directory目录)。
            #结束            超
        结束        高清渲染(上下文)
            context.registers [:loop_directory] ​​|| = Hash.new(0)            照片= Dir.glob(File.join(@属性['目录'],@属性['过滤器']))            如果@属性['排序']。casecmp(降序)== 0
                #查找文件并对其进行排序反向词汇。意即
                #的文件名称开头年月日会先被排序最新。
                images.sort! {| X,Y | Y'LT; = GT; X }
            其他
                #通常排序升序
                images.sort!
            结束            长度= images.length
            结果= []            context.stack做
                images.each_with_index做|项指数|
                    背景[@属性['迭代'] =项目
                    上下文['forloop'] =
                    {
                        '名'=> @属性['迭代'],
                        长度=>长度,
                        索引=>指数+1,
                        '的index0'=>指数,
                        RINDEX'=>长度 - 指数,
                        rindex0'=>长度 - 指数 - 1,
                        '第一'=> (指数== 0)
                        '最后'=> (指数==长度 - 1)
                    }                    结果<< render_all(@nodelist,上下文)
                结束
            结束            结果
        结束
    结束
结束液体:: Template.register_tag('loop_directory',杰基尔:: LoopDirectoryTag)


解决方案

有是在Github上主科pull请求此功能等待被合并到哲基尔1.0.0beta;他们只是在等待来自创作者TPW最终批准。

您可以查看code,现在复制它供自己使用,并保留一只眼睛时被合并。然后,可以使用该功能下载合并哲基尔并使用它无需插件,这样做:

创业板安装化身 - pre

这将让你从GitHub边缘的版本。

这里的PR - 新液标签上市文件:目录:

https://github.com/mojombo/jekyll/pull/585

I can't figure out how to create a filter or tag in a jekyll plugin, so that I can return a directory and loop over its contents. I found these:

http://pastebin.com/LRfMVN5Y

http://snippets.dzone.com/posts/show/302

So far I have:

module Jekyll
  class FilesTag < Liquid::Tag

    def initialize(tag_name, text, tokens)
      super
      @text = text
    end

    def render(context)
        #"#{@text} #{Time.now}"
        Dir.glob("images/*").each { |i| "#{i}" }
        #Dir.glob("images/*")
        #Hash[*Dir.glob("images/*").collect { |v| [v, v*2] }.flatten]
    end
  end
end

Liquid::Template.register_tag('files', Jekyll::FilesTag)

I can successfully return the list of images as a string and print it with:

{% files test_string %}

But for the life of me, I can't loop over the array, no matter how I return the array/hash from Dir.glob. I just want to be able to do:

{% for image in files %}
    image
{% endfor %}

I'm going to need to be able to return arrays of things constantly for the various collections I'll be using on the site. I just need a barebones plugin to build upon.

Thanks!


UPDATE: I partially solved it. This method works but requires using endloop_directory instead of endfor, which seems a bit ugly to me. Also, the filter is unable to take a parameter like *.{jpg,png} because there is no way to escape the {} in the html. Open to suggestions on how to pass a regex string in an attribute...

#usage:
#{% loop_directory directory:images iterator:image filter:*.jpg sort:descending %}
#   <img src="{{ image }}" />
#{% endloop_directory %}
module Jekyll
    class LoopDirectoryTag < Liquid::Block

        include Liquid::StandardFilters
        Syntax = /(#{Liquid::QuotedFragment}+)?/

        def initialize(tag_name, markup, tokens)
            @attributes = {}

            @attributes['directory'] = '';
            @attributes['iterator'] = 'item';
            @attributes['filter'] = 'item';
            @attributes['sort'] = 'ascending';

            # Parse parameters
            if markup =~ Syntax
                markup.scan(Liquid::TagAttributes) do |key, value|
                    @attributes[key] = value
                end
            else
                raise SyntaxError.new("Bad options given to 'loop_directory' plugin.")
            end

            #if @attributes['directory'].nil?
            #   raise SyntaxError.new("You did not specify a directory for loop_directory.")
            #end

            super
        end

        def render(context)
            context.registers[:loop_directory] ||= Hash.new(0)

            images = Dir.glob(File.join(@attributes['directory'], @attributes['filter']))

            if @attributes['sort'].casecmp( "descending" ) == 0
                # Find files and sort them reverse-lexically. This means
                # that files whose names begin with YYYYMMDD are sorted newest first.
                images.sort! {|x,y| y <=> x }
            else
                # sort normally in ascending order
                images.sort!
            end

            length = images.length
            result = []

            context.stack do
                images.each_with_index do |item, index|
                    context[@attributes['iterator']] = item
                    context['forloop'] =
                    {
                        'name' => @attributes['iterator'],
                        'length' => length,
                        'index' => index + 1,
                        'index0' => index,
                        'rindex' => length - index,
                        'rindex0' => length - index - 1,
                        'first' => (index == 0),
                        'last' => (index == length - 1) 
                    }

                    result << render_all(@nodelist, context)
                end
            end

            result
        end
    end
end

Liquid::Template.register_tag('loop_directory', Jekyll::LoopDirectoryTag)

解决方案

There is a Pull Request on the Github Master Branch for this feature waiting to be merged into the Jekyll 1.0.0beta; they are just waiting for final approval from the creator TPW.

You can view the code and copy it for your own use now, and keep an eye out for when in gets merged. Then you can download the merged Jekyll with that feature and use it without a plugin, by doing:

gem install jekyll --pre

Which will get you the edge version from Github.

Here's the PR - New Liquid tag for listing files: directory:

https://github.com/mojombo/jekyll/pull/585

这篇关于从哲基尔插件返回文件列表目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆