Sha256: 254b28b8844c9677770f5e3c8ac808dfa3d690b397b74709e7d5779da7b6788a

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

module Brief
  class Document
    module Rendering
      extend ActiveSupport::Concern

      # Uses a custom Redcarpet::Render::HTML subclass
      # which simply inserts data attributes on each heading element
      # so that they can be queried with CSS more deliberately.
      class HeadingWrapper < ::Redcarpet::Render::HTML
        def header(text, level)
          "<h#{level} data-level='#{level}' data-heading='#{ text }'>#{text}</h#{level}>"
        end
      end

      module ClassMethods
        def renderer_class
          HeadingWrapper
        end

        def renderer
          @renderer ||= begin
                          r = renderer_class.new(tables: true,
                                                 autolink: true,
                                                 gh_blockcode: true,
                                                 fenced_code_blocks: true,
                                                 footnotes: true)

                          ::Redcarpet::Markdown.new(r)
                        end
        end
      end

      # Documents can be rendered into HTML.
      #
      # They will first be put through a Nokogiri processor pipeline
      # which allows us to wrap things in section containers, apply data
      # attributes, and other things to the HTML so that the output HTML retains its
      # relationship to the underlying data and document structure.
      def to_html(options = {})
        if options[:wrap] == false
          unwrapped_html
        else
          wrapper = options.fetch(:wrapper, 'div')
          "<#{ wrapper } data-brief-model='#{ model_class.type_alias }' data-brief-path='#{ relative_path_identifier }'>#{ unwrapped_html }</#{wrapper}>"
        end
      end

      def unwrapped_html
        parser.to_html
      end

      protected

      def to_raw_html
        renderer.render(content)
      end

      def renderer
        @renderer ||= self.class.renderer
      end

      attr_writer :renderer
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
brief-1.4.4 lib/brief/document/rendering.rb
brief-1.4.2 lib/brief/document/rendering.rb
brief-1.4.1 lib/brief/document/rendering.rb
brief-1.3.2 lib/brief/document/rendering.rb
brief-1.3.1 lib/brief/document/rendering.rb
brief-1.3.0 lib/brief/document/rendering.rb