سؤال

following the plugins instructions, I build my own generator that creates a template page. However, it doesn't behave like I though. The generated template are not parsed by Liquid and are not rendered after the generation.

Do you know how I could do ?

Currently by generator is like this:

module Jekyll
   class TagPages < Generator
   safe true
   priority :highest

      def generate(site)
         site.tags.each { |tag, posts|
             html = ''
             html << <<-HTML
---
layout: tag_page
---
<ul>
HTML
             posts.each { |post| html << "<li>#{post.url}</li>" }
             html << "</ul>"
             File.open("tags/#{tag}.html", 'w+') do |file|
                 file.puts html
             end
         }
      end
   end
end

So, for example for the tag free-software, it creates the page tags/free-software.html which looks like this.

---
layout: tag_page
---
<ul>
<li>/link_to_my_post_relative_to_free_software.html</li>
<li>/another_amazing_post.html</li></ul>

So if I want that the liquid system render this page ? Is there a way to be sure that Generator are called really at the beginning of the generation of the site ?

EDIT:

According to the source code of jekyll, generate is called before render (line 80). So if someone could tell me what I do wrong, it could help.

EDIT 2:

Even if render is called after generate, it doesn't read the files from disk between this two steps, so it ignores the generated files.

هل كانت مفيدة؟

المحلول

I would make it a liquid tag. It's a better way to do that.

نصائح أخرى

To resolve this ancient mystery: generate is indeed called before render, but it is called after inventory. By the time your generator runs, Jekyll already "knows" the list of files to process (and their contents), and your generated file isn't among them (or, if there was an old one lying about, then the old contents are known and your generated contents ignored).

To solve it, add a new Jekyll::Page to site.pages, dumping the inventoried one if any:

def generate(site)
    site.static_files.select {|sf| sf.path.end_with?('/your_infile_name')}.each {|your_infile|

        # generate your_new_file from your_infile

        relative_dir = your_infile.relative_path[1..-15] # -length of your new_file_name 
        new_page =  Jekyll::Page.new(site, Dir.pwd, relative_dir, new_file_name)
        site.pages.delete_if {|p| p.path == relative_dir + file_name}  << new_page
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top