Merge branch 'master' into 2257-inequality-conditions
[arvados.git] / doc / zenweb-liquid.rb
1 require 'zenweb'
2 require 'liquid'
3
4 module ZenwebLiquid
5   VERSION = '0.0.1'
6 end
7
8 module Zenweb
9
10   class Page
11
12     def render_liquid page, content
13       liquid self.body, content, page, binding
14     end
15     
16     ##
17     # Render a page's liquid and return the intermediate result
18     def liquid template, content, page, binding = TOPLEVEL_BINDING
19       Liquid::Template.file_system = Liquid::LocalFileSystem.new(File.join(File.dirname(Rake.application().rakefile), "_includes"))
20       unless defined? @liquid_template
21         @liquid_template = Liquid::Template.parse(template)
22       end
23       
24       vars = {}
25       vars["content"] = content
26
27       vars["site"] = site.config.h.clone
28       pages = {}
29       site.pages.each do |f, p|
30         pages[f] = p.config.h.clone
31         pages[f]["url"] = p.url
32       end
33       vars["site"]["pages"] = pages
34
35       vars["page"] = page.config.h.clone
36       vars["page"]["url"] = page.url
37       
38       @liquid_template.render(vars)
39     end
40   end
41
42   class LiquidCode < Liquid::Include
43     Syntax = /(#{Liquid::QuotedFragment}+)(\s+(?:as)\s+(#{Liquid::QuotedFragment}+))?/o
44
45     def initialize(tag_name, markup, tokens)
46       Liquid::Tag.instance_method(:initialize).bind(self).call(tag_name, markup, tokens)
47
48       if markup =~ Syntax
49         @template_name = $1
50         @language = $3
51         @attributes    = {}
52       else
53         raise SyntaxError.new("Error in tag 'code' - Valid syntax: include '[code_file]' as '[language]'")
54       end
55     end
56     
57     def render(context)
58       require 'coderay'
59
60       partial = load_cached_partial(context)
61       html = ''
62
63       context.stack do
64         html = CodeRay.scan(partial.root.nodelist.join, @language).div
65       end
66
67       html
68     end
69
70     Liquid::Template.register_tag('code', LiquidCode)    
71   end
72 end