Sha256: 6b1b5675aa430d06d69578d6bfd5fcfa710b39f1e2e99ca727fc82adfe22beeb

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

module Vedeu

  module Renderers

    # Renders a {Vedeu::VirtualBuffer} or {Vedeu::Output} as JSON.
    #
    # @api private
    class JSON < Vedeu::Renderers::File

      # Returns a new instance of Vedeu::Renderers::JSON.
      #
      # @param options [Hash]
      # @return [Vedeu::Renderers::JSON]
      def initialize(options = {})
        @options = options || {}
      end

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [String]
      def render(output)
        super(parsed(output))
      end

      private

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [String]
      def parsed(output)
        return '' if output.nil? || output.empty?

        ::JSON.pretty_generate(as_hash(output))
      end

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [Array]
      def as_hash(output)
        sorted(output).map(&:to_hash)
      end

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [Array]
      def sorted(output)
        Array(output).flatten.sort { |a, b| a.position <=> b.position }
      end

    end # JSON

  end # Renderers

end # Vedeu

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vedeu-0.4.56 lib/vedeu/output/renderers/json.rb
vedeu-0.4.55 lib/vedeu/output/renderers/json.rb
vedeu-0.4.54 lib/vedeu/output/renderers/json.rb
vedeu-0.4.53 lib/vedeu/output/renderers/json.rb
vedeu-0.4.52 lib/vedeu/output/renderers/json.rb
vedeu-0.4.51 lib/vedeu/output/renderers/json.rb