File: body_content.rb

package info (click to toggle)
ruby-html-pipeline 2.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 2,265; sh: 13; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module HTML
  class Pipeline
    # Public: Runs a String of content through an HTML processing pipeline,
    # providing easy access to a generated DocumentFragment.
    class BodyContent
      attr_reader :result

      # Public: Initialize a BodyContent.
      #
      # body     - A String body.
      # context  - A Hash of context options for the filters.
      # pipeline - A HTML::Pipeline object with one or more Filters.
      def initialize(body, context, pipeline)
        @body = body
        @context = context
        @pipeline = pipeline
      end

      # Public: Gets the memoized result of the body content as it passed through
      # the Pipeline.
      #
      # Returns a Hash, or something similar as defined by @pipeline.result_class.
      def result
        @result ||= @pipeline.call @body, @context
      end

      # Public: Gets the updated body from the Pipeline result.
      #
      # Returns a String or DocumentFragment.
      def output
        @output ||= result[:output]
      end

      # Public: Parses the output into a DocumentFragment.
      #
      # Returns a DocumentFragment.
      def document
        @document ||= HTML::Pipeline.parse output
      end
    end
  end
end