Sha256: ee4e431087e878b5d13ca18bc985134a091829cbe2bf5c64aaf0af6cedcc42f1

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module Liquid
  module CustomBlocks
    class WithGlossaristContext < Block
      def initialize(tag_name, markup, tokens)
        super

        @contexts = []
        @filters = {}

        contexts, filters = markup.split(";", 2)

        if filters && !filters.empty?
          filters.strip.gsub(/^['"]|['"]$/, "").split(";").each do |filter|
            property, value = filter.split("=")

            @filters[property] = value
          end
        end

        contexts.split(",").each do |context|
          context_name, file_path = context.split("=").map(&:strip)

          @contexts << {
            name: context_name,
            file_path: file_path,
          }
        end
      end

      def load_collection(folder_path)
        @@collections ||= {}

        return @@collections[folder_path] if @@collections[folder_path]

        collection = ::Glossarist::ManagedConceptCollection.new
        collection.load_from_files(folder_path)
        @@collections[folder_path] = collection
      end

      def render(context)
        @contexts.each do |local_context|
          context_file = local_context[:file_path].strip
          collection = load_collection(context_file)

          context[local_context[:name]] = Liquid::Drops::ConceptsDrop.new(collection, @filters)
        end

        super
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
metanorma-plugin-glossarist-0.2.3 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.2.2 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.2.1 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.2.0 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.1.8 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.1.7 lib/liquid/custom_blocks/with_glossarist_context.rb
metanorma-plugin-glossarist-0.1.6 lib/liquid/custom_blocks/with_glossarist_context.rb