X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b54a5ea817d3d2087eaa07dcf98ec8a82af56d06..9e806b0c387cf7146e0d0cf3169fd227963c19b9:/doc/zenweb-liquid.rb diff --git a/doc/zenweb-liquid.rb b/doc/zenweb-liquid.rb index 545a0d8f5a..3e8672e021 100644 --- a/doc/zenweb-liquid.rb +++ b/doc/zenweb-liquid.rb @@ -1,4 +1,9 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: CC-BY-SA-3.0 + require 'zenweb' +require 'liquid' module ZenwebLiquid VERSION = '0.0.1' @@ -11,16 +16,15 @@ 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 - require 'liquid' Liquid::Template.file_system = Liquid::LocalFileSystem.new(File.join(File.dirname(Rake.application().rakefile), "_includes")) unless defined? @liquid_template @liquid_template = Liquid::Template.parse(template) end - + vars = {} vars["content"] = content @@ -34,8 +38,83 @@ module Zenweb vars["page"] = page.config.h.clone vars["page"]["url"] = page.url - + @liquid_template.render(vars) end end + + class LiquidCode < Liquid::Include + Syntax = /(#{Liquid::QuotedFragment}+)(\s+(?: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 + @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(@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 + + html + end + + 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 + + "#{html}" + end + + Liquid::Template.register_tag('codeblock', LiquidCodeBlock) + end end