这对我来说更复杂:
我有这样的内容被拉进了一个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"作为液体标签的参数,但我不确定如何将其连接到插件中。
远程版本我会有类似title = regmatch for title,body = ...,img = ...
感谢您的帮助!
答案 0 :(得分: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