19070: Still trying to fix test_with_arvbox
[arvados.git] / doc / zenweb-liquid.rb
index 0be882a48b45e15b5b7ec9bc8e0b2743390e60c0..3e8672e0216e0290982556b82f83eac964a34534 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: CC-BY-SA-3.0
+
 require 'zenweb'
 require 'liquid'
 
@@ -12,7 +16,7 @@ module Zenweb
     def render_liquid page, content
       liquid self.body, content, page, binding
     end
-    
+
     ##
     # Render a page's liquid and return the intermediate result
     def liquid template, content, page, binding = TOPLEVEL_BINDING
@@ -20,7 +24,7 @@ module Zenweb
       unless defined? @liquid_template
         @liquid_template = Liquid::Template.parse(template)
       end
-      
+
       vars = {}
       vars["content"] = content
 
@@ -34,7 +38,7 @@ module Zenweb
 
       vars["page"] = page.config.h.clone
       vars["page"]["url"] = page.url
-      
+
       @liquid_template.render(vars)
     end
   end
@@ -46,20 +50,25 @@ module Zenweb
       Liquid::Tag.instance_method(:initialize).bind(self).call(tag_name, markup, tokens)
 
       if markup =~ Syntax
-        @template_name = $1
+        @template_name_expr = $1
         @language = $3
         @attributes    = {}
       else
         raise SyntaxError.new("Error in tag 'code' - Valid syntax: include '[code_file]' as '[language]'")
       end
     end
-    
+
     def render(context)
       require 'coderay'
 
-      partial = load_cached_partial(context)
+      partial = load_cached_partial(@template_name_expr, context)
       html = ''
 
+      # be explicit about errors
+      context.exception_renderer = lambda do |exc|
+        exc.is_a?(Liquid::InternalError) ? "Liquid error: #{exc.cause.message}" : exc
+      end
+
       context.stack do
         html = CodeRay.scan(partial.root.nodelist.join, @language).div
       end
@@ -67,6 +76,45 @@ module Zenweb
       html
     end
 
-    Liquid::Template.register_tag('code', LiquidCode)    
+    Liquid::Template.register_tag('code', LiquidCode)
+  end
+
+  class LiquidCodeBlock < Liquid::Block
+    Syntax = /((?:as)\s+(#{Liquid::QuotedFragment}+))?/o
+
+    def initialize(tag_name, markup, tokens)
+      Liquid::Tag.instance_method(:initialize).bind(self).call(tag_name, markup, tokens)
+
+      if markup =~ Syntax
+        @language = $2
+        @attributes = {}
+      else
+        raise SyntaxError.new("Error in tag 'code' - Valid syntax: codeblock as '[language]'")
+      end
+    end
+
+    def render(context)
+      require 'coderay'
+
+      partial = super
+      html = ''
+
+      if partial[0] == '\n'
+        partial = partial[1..-1]
+      end
+
+      # be explicit about errors
+      context.exception_renderer = lambda do |exc|
+        exc.is_a?(Liquid::InternalError) ? "Liquid error: #{exc.cause.message}" : exc
+      end
+
+      context.stack do
+        html = CodeRay.scan(partial, @language).div
+      end
+
+      "<notextile>#{html}</notextile>"
+    end
+
+    Liquid::Template.register_tag('codeblock', LiquidCodeBlock)
   end
 end