|
| 1 | +module Jekyll |
| 2 | + |
| 3 | + class SessionPage < Page |
| 4 | + def initialize(site, base, dir) |
| 5 | + @site = site |
| 6 | + @base = base |
| 7 | + @dir = dir |
| 8 | + @name = 'index.html' |
| 9 | + |
| 10 | + self.process(@name) |
| 11 | + self.read_yaml(File.join(base, '_layouts'), 'session.html') |
| 12 | + self.data['tasks'] = get_tasks |
| 13 | + # load other options |
| 14 | + if File.exists?(File.join(base, '_sessions', dir, 'info.yml')) |
| 15 | + self.data.merge!(YAML.load_file(File.join(base, '_sessions', dir, 'info.yml'))) |
| 16 | + end |
| 17 | + |
| 18 | + self.data['title'] ||= dir |
| 19 | + end |
| 20 | + |
| 21 | + def get_tasks |
| 22 | + Dir.entries(File.join(@base, '_sessions', @dir)).reject{|x| %w{. .. info.yml}.include?(x)}.reject{|x| x[0]=='_'}.sort.map do |n| |
| 23 | + t = TaskPage.new(@site, @base, @dir, n) |
| 24 | + #t.render |
| 25 | + t |
| 26 | + end |
| 27 | + end |
| 28 | + def render(*args) |
| 29 | + self.data['tasks'].each {|t| t.data['output'] = t.tap {|t| t.render(*args)}.output } |
| 30 | + super |
| 31 | + end |
| 32 | + |
| 33 | + end |
| 34 | + |
| 35 | + class SessionPageGenerator < Generator |
| 36 | + safe true |
| 37 | + |
| 38 | + def generate(site) |
| 39 | + # if we have a session template |
| 40 | + if site.layouts.key? 'session' |
| 41 | + # put in top level directory |
| 42 | + if Dir.exists?('_sessions') |
| 43 | + subdirs = Dir.entries('_sessions').reject{|x| %w{. ..}.include?(x)} |
| 44 | + subdirs.each do |dir_name| |
| 45 | + |
| 46 | + site.pages << SessionPage.new(site, site.source, dir_name) |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + # In some ways I want a TaskPage. In others I don't. |
| 54 | + # I definitely want something to read in individual task files and render them. |
| 55 | + # But in a way more akin to a blog page summary than individual pages |
| 56 | + |
| 57 | + # Plan: make TaskPages. Don't have a TaskPage generator. Attach TaskPages to relevant SessionPage. |
| 58 | + # Render them using TaskPage.content ?? Or does the content get passed in in a different way. |
| 59 | + # You don't call post.content ... :( ... Actually it's ok. Content does seem to be passed to liquid. |
| 60 | + # When does it get put into the template though? https://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb#L225 |
| 61 | + # But that just calls page.render: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/page.rb#L105 |
| 62 | + # But that just goes here: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb#L90 |
| 63 | + # The only bit I don't want is Convertible.write https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb#L141 |
| 64 | + # Which is called for pages, posts etc. here: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb#L292 |
| 65 | + # So what I really want it to do is allow it to render, but not be placed in the list of pages. |
| 66 | + # But then that has to be done manually anyway, so we're fine! |
| 67 | + |
| 68 | + # This doesn't seem to work. I have problems rendering the TaskPages at the right time. |
| 69 | + # Maybe I should merge the TaskPages into the SessionPage's payload |
| 70 | + # Actually I didn't have to do this. The above worked. |
| 71 | + |
| 72 | + class TaskPage < Page |
| 73 | + def initialize(site, base, dir, name) |
| 74 | + @site = site |
| 75 | + @base = base |
| 76 | + @dir = dir |
| 77 | + @name = name # won't be used |
| 78 | + |
| 79 | + self.process(@name) # separates into @basename and @ext |
| 80 | + self.read_yaml(File.join(base,'_sessions', dir), name) |
| 81 | + |
| 82 | + self.data['name'] = @basename |
| 83 | + |
| 84 | + # self.data['title'] = "#{category_title_prefix}#{category}" # should have title already |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + class ExerciseTag < Liquid::Block |
| 91 | + # somehow access converter |
| 92 | + |
| 93 | + def render(context) |
| 94 | + # content = super |
| 95 | + # parsed_content = Liquid::Template.parse(content) |
| 96 | + # output = context.stack do |
| 97 | + # parsed_content.render(context) |
| 98 | + # end |
| 99 | + # how do I convert this thing??? |
| 100 | + "<div class='exercise alert alert-info' markdown='1'><strong>Task:</strong> " + super + "</div>" |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + |
| 105 | +end |
| 106 | + |
| 107 | +Liquid::Template.register_tag('exercise', Jekyll::ExerciseTag) |
0 commit comments