Class: Mbrao::RenderingEngines::HtmlPipeline

Inherits:
Base
  • Object
show all
Defined in:
lib/mbrao/rendering_engines/html_pipeline.rb

Overview

A renders which use the html-pipeline gem.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Hash) default_options

Gets the default options.

Returns:

  • (Hash)

    The default options.



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 18

class HtmlPipeline < Mbrao::RenderingEngines::Base
  attr_accessor :default_pipeline
  attr_accessor :default_options

  # Renders a content.
  #
  # @param content [Content|String] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  def render(content, options = {}, context = {})
    options = sanitize_options(options)
    context = context.ensure_hash(accesses: :symbols)

    begin
      create_pipeline(options, context).call(get_body(content, options))[:output].to_s
    rescue Mbrao::Exceptions::UnavailableLocalization => le
      raise le
    rescue => e
      raise ::Mbrao::Exceptions::Rendering, e.to_s
    end
  end

  # Gets the default pipeline.
  #
  # @return [Array] The default pipeline.
  def default_pipeline
    @default_pipeline || [[:kramdown], [:table_of_contents, :toc], [:autolink, :links], [:emoji], [:image_max_width]]
  end

  # Sets the default pipeline.
  #
  # @return [Array] The default pipeline.
  def default_pipeline=(value)
    @default_pipeline = value.ensure_array { |v| v.ensure_array(no_duplicates: true, compact: true, flatten: true) { |p| p.ensure_string.to_sym } }
  end

  # Gets the default options.
  #
  # @return [Hash] The default options.
  def default_options
    @default_options || {gfm: true, asset_root: "/"}
  end

  # Sets the default options.
  #
  # @param value [Object] The new default options.
  def default_options=(value)
    @default_options = value.ensure_hash
  end

  private

  # :nodoc:
  def sanitize_options(options)
    options = options.ensure_hash(accesses: :symbols)
    options = filter_filters(options)
    options[:pipeline_options] = default_options.merge(options[:pipeline_options].ensure_hash(accesses: :symbols))

    options
  end

  # :nodoc:
  def get_body(content, options)
    content = ::Mbrao::Content.create(nil, content.ensure_string) unless content.is_a?(::Mbrao::Content)
    content.get_body(options.fetch(:locales, ::Mbrao::Parser.locale).ensure_string)
  end

  # :nodoc:
  def create_pipeline(options, context)
    ::HTML::Pipeline.new(
      options[:pipeline].map { |f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) },
      options[:pipeline_options].merge(context)
    )
  end

  # :nodoc:
  def filter_filters(options)
    options[:pipeline] = get_pipeline(options)

    default_pipeline.each do |f|
      options[:pipeline].delete(f.first) unless options.fetch(f.last, true)
    end

    options
  end

  # :nodoc:
  def get_pipeline(options)
    options.fetch(:pipeline, default_pipeline.map(&:first)).map(&:to_sym)
  end
end

- (Array) default_pipeline

Gets the default pipeline.

Returns:

  • (Array)

    The default pipeline.



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 18

class HtmlPipeline < Mbrao::RenderingEngines::Base
  attr_accessor :default_pipeline
  attr_accessor :default_options

  # Renders a content.
  #
  # @param content [Content|String] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  def render(content, options = {}, context = {})
    options = sanitize_options(options)
    context = context.ensure_hash(accesses: :symbols)

    begin
      create_pipeline(options, context).call(get_body(content, options))[:output].to_s
    rescue Mbrao::Exceptions::UnavailableLocalization => le
      raise le
    rescue => e
      raise ::Mbrao::Exceptions::Rendering, e.to_s
    end
  end

  # Gets the default pipeline.
  #
  # @return [Array] The default pipeline.
  def default_pipeline
    @default_pipeline || [[:kramdown], [:table_of_contents, :toc], [:autolink, :links], [:emoji], [:image_max_width]]
  end

  # Sets the default pipeline.
  #
  # @return [Array] The default pipeline.
  def default_pipeline=(value)
    @default_pipeline = value.ensure_array { |v| v.ensure_array(no_duplicates: true, compact: true, flatten: true) { |p| p.ensure_string.to_sym } }
  end

  # Gets the default options.
  #
  # @return [Hash] The default options.
  def default_options
    @default_options || {gfm: true, asset_root: "/"}
  end

  # Sets the default options.
  #
  # @param value [Object] The new default options.
  def default_options=(value)
    @default_options = value.ensure_hash
  end

  private

  # :nodoc:
  def sanitize_options(options)
    options = options.ensure_hash(accesses: :symbols)
    options = filter_filters(options)
    options[:pipeline_options] = default_options.merge(options[:pipeline_options].ensure_hash(accesses: :symbols))

    options
  end

  # :nodoc:
  def get_body(content, options)
    content = ::Mbrao::Content.create(nil, content.ensure_string) unless content.is_a?(::Mbrao::Content)
    content.get_body(options.fetch(:locales, ::Mbrao::Parser.locale).ensure_string)
  end

  # :nodoc:
  def create_pipeline(options, context)
    ::HTML::Pipeline.new(
      options[:pipeline].map { |f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) },
      options[:pipeline_options].merge(context)
    )
  end

  # :nodoc:
  def filter_filters(options)
    options[:pipeline] = get_pipeline(options)

    default_pipeline.each do |f|
      options[:pipeline].delete(f.first) unless options.fetch(f.last, true)
    end

    options
  end

  # :nodoc:
  def get_pipeline(options)
    options.fetch(:pipeline, default_pipeline.map(&:first)).map(&:to_sym)
  end
end

Instance Method Details

- (Object) render(content, options = {}, context = {})

Renders a content.

Parameters:

  • content (Content|String)

    The content to parse.

  • options (Hash) (defaults to: {})

    A list of options for renderer.

  • context (Hash) (defaults to: {})

    A context for rendering.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 27

def render(content, options = {}, context = {})
  options = sanitize_options(options)
  context = context.ensure_hash(accesses: :symbols)

  begin
    create_pipeline(options, context).call(get_body(content, options))[:output].to_s
  rescue Mbrao::Exceptions::UnavailableLocalization => le
    raise le
  rescue => e
    raise ::Mbrao::Exceptions::Rendering, e.to_s
  end
end