0
votes

Here's a more complex one for me:

I have content like this being pulled into a jekyll post:

# 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.
~

I'm pulling this into my layout like this: {{ post.content | jekreged: 1 | markdownify }}

Jekreged is a custom liquid plugin I wrote that splits the content based on the ~ and then specifies which piece to include. The layout requires ripping apart a post like that.

I am trying to adapt this to then run a subset of match commands that I can call specifically from the liquid tag.

here's the example (and the one not working) that I am trying to troubleshoot.

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)

I get no output from this. What I would ideally like is to be able to specify "title" as a parameter from the liquid tag but I'm not sure how to connect that through into the plugin.

Long range version I'll have something like title = regmatch for title, body = ..., img = ...

Thanks for any and all help!

1

1 Answers

0
votes

took a shot at it (in the future, some example inputs/outputs would go a long way).

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