From dfbbf87ba9f02c0ca439a79f6eb7e475cd9bf84e Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 2 Sep 2020 09:32:19 -0400 Subject: [PATCH] Use BlockBody from Document using composition rather than inheritence This way liquid-c can more cleanly use a Liquid::C::BlockBody object for the block body by overriding Liquid::Document#new_body. --- lib/liquid/document.rb | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/liquid/document.rb b/lib/liquid/document.rb index e160886..8338b06 100644 --- a/lib/liquid/document.rb +++ b/lib/liquid/document.rb @@ -1,15 +1,26 @@ # frozen_string_literal: true module Liquid - class Document < BlockBody + class Document def self.parse(tokens, parse_context) - doc = new + doc = new(parse_context) doc.parse(tokens, parse_context) doc end + attr_reader :parse_context, :body + + def initialize(parse_context) + @parse_context = parse_context + @body = new_body + end + + def nodelist + @body.nodelist + end + def parse(tokens, parse_context) - super do |end_tag_name, _end_tag_params| + @body.parse(tokens, parse_context) do |end_tag_name, _end_tag_params| unknown_tag(end_tag_name, parse_context) if end_tag_name end rescue SyntaxError => e @@ -25,5 +36,19 @@ module Liquid raise SyntaxError, parse_context.locale.t("errors.syntax.unknown_tag", tag: tag) end end + + def render_to_output_buffer(context, output) + @body.render_to_output_buffer(context, output) + end + + def render(context) + @body.render(context) + end + + private + + def new_body + Liquid::BlockBody.new + end end end