Sha256: 6392f89e559b30e4a5a1a8c73eb4b3f9691edd47af7c5edf1b110c9e233d7f27

Contents?: true

Size: 858 Bytes

Versions: 2

Compression:

Stored size: 858 Bytes

Contents

require "pandoc_object_filters"
require "json"

module PandocObjectFilters
  class Filter
    attr_accessor :doc, :format, :meta

    def initialize(input = $stdin, output = $stdout, argv = ARGV, &block)
      @input = input
      @output = output
      @argv = argv
      @block = block
    end

    def filter(&block)
      process(block) do
        PandocObjectFilters::Element.walk(@doc, &@block)
        @doc
      end
    end

    def filter!(&block)
      process(block) { PandocObjectFilters::Element.walk!(@doc, &@block) }
    end

    private

    def process(block)
      @block = block unless @block
      @doc = PandocObjectFilters::Element::Document.new(JSON.parse(@input.read))
      @format = @argv.first
      @meta = @doc.meta
      result = yield
      @output.puts JSON.dump(PandocObjectFilters::Element.to_ast(result))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pandoc_object_filters-0.2.0 lib/pandoc_object_filters/filter.rb
pandoc_object_filters-0.1.0 lib/pandoc_object_filters/filter.rb