0

对我来说,这是一个更复杂的:

我有这样的内容被拉进一个 jekyll 帖子:

# Lorem ipsum dolor sit amet.
Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.
~
# Et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation.
~
# Ullamco laboris nisi.
Ut aliquip ex ea commodo consequat.
~

我将其拉入我的布局中,如下所示:{{ post.content | jekreged: 1 | markdownify }}

Jekreged 是我编写的一个自定义液体插件,它根据 拆分内容~,然后指定要包含的部分。布局需要撕开这样的帖子。

我正在尝试对此进行调整,然后运行我可以从液体标签中专门调用的匹配命令的子集。

这是我正在尝试排除故障的示例(也是不工作的示例)。

module Jekyll
  module AssetFilter
    def jekreged(input, chunk)
      drugs = input.split("~")[chunk]
      title = (drugs).match(/^#{1}.+$/)
      jekreged = "#{title}"
    end
  end
end

Liquid::Template.register_filter(Jekyll::AssetFilter)

我没有得到任何输出。理想情况下,我希望能够将“标题”指定为液体标签的参数,但我不确定如何将其连接到插件中。

远程版本我会有类似 title = regmatch for title, body = ..., img = ...

感谢您的任何帮助!

4

1 回答 1

0

对它进行了尝试(将来,一些示例输入/输出将有很长的路要走)。

module Jekyll
  module AssetFilter
    def jekreged(input, matcher)
      titles = input.split("\n~\n").select { |title| title.include? matcher }
      if titles.size > 1
        raise "Can't determine title from #{matcher.inspect}, found #{titles.inspect}"
      elsif titles.size.zero?
        raise "#{matcher.inspect} didn't match any of #{titles.inspect}"
      end
      titles.first
    end
  end
end

describe 'jekreged' do
  include Jekyll::AssetFilter
  let(:titles) { <<-TITLES.gsub /^  /, "" }
  # Lorem ipsum dolor sit amet.
  Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.
  ~
  # Et dolore magna aliqua.
  Ut enim ad minim veniam, quis nostrud exercitation.
  ~
  # Ullamco laboris nisi.
  Ut aliquip ex ea commodo consequat.
  ~
  TITLES

  it 'finds the title that has the string in it' do
    jekreged(titles, "Consectetur" ).should == "# Lorem ipsum dolor sit amet.\nConsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore."
    jekreged(titles, "minim veniam").should == "# Et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation."
    jekreged(titles, "aliquip"     ).should == "# Ullamco laboris nisi.\nUt aliquip ex ea commodo consequat."
  end

  it 'raises an error if there is more than one title that matches' do
    expect { jekreged titles, 'Ut' }.to raise_error /Can't determine title/
  end

  it 'raises an error if there are no titles that match' do
    expect { jekreged titles, 'asdfasdfasdf' }.to raise_error /didn't match/
  end
end
于 2012-08-28T00:50:06.250 回答