Sha256: 6277c8a3924c3949cc2067ca9edc54aa046f26186baf21392db7fb20815a3943

Contents?: true

Size: 654 Bytes

Versions: 2

Compression:

Stored size: 654 Bytes

Contents

module Beryl
  class HTMLRenderer
    def render(element)
      return element[:props][:nodeValue] if element[:type] == 'text'
      "#{open_tag(element)}#{children(element)}#{close_tag(element)}"
    end

    private

    def children(element)
      element[:children].each_with_object('') do |child, html|
        html << render(child)
      end
    end

    def close_tag(element)
      "</#{element[:type]}>"
    end

    def open_tag(element)
      "<#{element[:type]}#{props(element[:props])}>"
    end

    def props(props)
      props.each_with_object('') do |(key, value), html|
        html << " #{key}=\"#{value}\""
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beryl-0.3.1 lib/beryl/html_renderer.rb
beryl-0.3.0 lib/beryl/html_renderer.rb