class Jekyll::FragmentGenerator

Constants

FragmentSubPattern
FullFragmentPattern
TagEnd
TagStart

Public Instance Methods

clean_fragment(body) click to toggle source
# File lib/jekyll-dry.rb, line 66
def clean_fragment(body)
  pattern = /\s*#{FragmentSubPattern}\s*/om
  clean_body = body.gsub(FragmentSubPattern, "")
  clean_body.lstrip!
  clean_body.rstrip!
end
generate(site) click to toggle source
# File lib/jekyll-dry.rb, line 30
def generate(site)
  # Initialize hash map that will store the fragments for rendering
  class << site
    attr_accessor :fragments
  end
  site.fragments = Hash.new

  # Parse fragments on pages
  site.pages.each do |page|
    fragments = parse_page(page.content)

    site.fragments.merge!(fragments) do |key, old_val, new_val|
      # This section is invoked every time there is a duplicate key
      # in site.fragments and parsed fragments
      raise Liquid::SyntaxError.new(
        "Duplicate fragment #{key} on page #{page.url}",
        original_fragment: old_val,
        duplicate_fragment: new_val
      )
    end
  end
end
parse_page(content) click to toggle source
# File lib/jekyll-dry.rb, line 53
def parse_page(content)
  fragments = Hash.new

  pos = 0
  while m = FullFragmentPattern.match(content, pos)
    body = clean_fragment(m[:body])
    fragments.store(m[:id], body)
    puts("fragment #{m[:id]}:", body)
    pos = m.begin(:id)
  end
  fragments
end