Class: Mbrao::RenderingEngines::HtmlPipeline
- Inherits:
-
Base
- Object
- Base
- Mbrao::RenderingEngines::HtmlPipeline
- Defined in:
- lib/mbrao/rendering_engines/html_pipeline.rb
Overview
A renders which use the html-pipeline gem.
Instance Attribute Summary (collapse)
-
- (Hash) default_options
Gets the default options.
-
- (Array) default_pipeline
Gets the default pipeline.
Instance Method Summary (collapse)
-
- (Object) render(content, options = {}, context = {})
Renders a content.
Instance Attribute Details
- (Hash) default_options
Gets the default options.
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 42 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, = {}, context = {}) = () context = context.ensure_hash(:symbols) begin create_pipeline(, context).call(get_body(content, ))[:output].to_s rescue Mbrao::Exceptions::UnavailableLocalization => le raise le rescue => e raise ::Mbrao::Exceptions::Rendering.new(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(nil, false, false) {|v| v.ensure_array(nil, true, true, true) { |p| p.ensure_string.to_sym } } end # Gets the default options. # # @return [Hash] The default options. def @default_options || {gfm: true, asset_root: "/"} end # Sets the default options. # # @param value [Object] The new default options. def (value) @default_options = value.ensure_hash end private # Sanitizes options. # # @param options [Hash] The options to sanitize. # @return [Hash] The sanitized options. def () = .ensure_hash(:symbols) = filter_filters() [:pipeline_options] = self..merge([:pipeline_options].ensure_hash(:symbols)) end # Get body of a content. # # @param content [Content|String] The content to sanitize. # @param options [Hash] A list of options for renderer. # @return [Array] The body to parse. def get_body(content, ) content = ::Mbrao::Content.create(nil, content.ensure_string) if !content.is_a?(::Mbrao::Content) content.get_body(.fetch(:locale, ::Mbrao::Parser.locale).ensure_string) end # Creates the pipeline for rendering. # # @param options [Hash] A list of options for renderer. # @param context [Hash] A context for rendering. # @return [HTML::Pipeline] The pipeline def create_pipeline(, context) ::HTML::Pipeline.new([:pipeline].map {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }, [:pipeline_options].merge(context)) end # Filters pipeline filters basing on the options provided. # # @param options [Hash] The original options. # @return [Hash] The options with the new set of filters. def filter_filters() [:pipeline] = get_pipeline() self.default_pipeline.each do |f| [:pipeline].delete(f.first) if !.fetch(f.last, true) end end # Gets the pipeline for the current options. # # @param options [Hash] The options to parse. # @return [Array] The pipeline to process. def get_pipeline() .fetch(:pipeline, self.default_pipeline.map(&:first)).map(&:to_sym) end end |
- (Array) default_pipeline
Gets the default pipeline.
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 42 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, = {}, context = {}) = () context = context.ensure_hash(:symbols) begin create_pipeline(, context).call(get_body(content, ))[:output].to_s rescue Mbrao::Exceptions::UnavailableLocalization => le raise le rescue => e raise ::Mbrao::Exceptions::Rendering.new(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(nil, false, false) {|v| v.ensure_array(nil, true, true, true) { |p| p.ensure_string.to_sym } } end # Gets the default options. # # @return [Hash] The default options. def @default_options || {gfm: true, asset_root: "/"} end # Sets the default options. # # @param value [Object] The new default options. def (value) @default_options = value.ensure_hash end private # Sanitizes options. # # @param options [Hash] The options to sanitize. # @return [Hash] The sanitized options. def () = .ensure_hash(:symbols) = filter_filters() [:pipeline_options] = self..merge([:pipeline_options].ensure_hash(:symbols)) end # Get body of a content. # # @param content [Content|String] The content to sanitize. # @param options [Hash] A list of options for renderer. # @return [Array] The body to parse. def get_body(content, ) content = ::Mbrao::Content.create(nil, content.ensure_string) if !content.is_a?(::Mbrao::Content) content.get_body(.fetch(:locale, ::Mbrao::Parser.locale).ensure_string) end # Creates the pipeline for rendering. # # @param options [Hash] A list of options for renderer. # @param context [Hash] A context for rendering. # @return [HTML::Pipeline] The pipeline def create_pipeline(, context) ::HTML::Pipeline.new([:pipeline].map {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }, [:pipeline_options].merge(context)) end # Filters pipeline filters basing on the options provided. # # @param options [Hash] The original options. # @return [Hash] The options with the new set of filters. def filter_filters() [:pipeline] = get_pipeline() self.default_pipeline.each do |f| [:pipeline].delete(f.first) if !.fetch(f.last, true) end end # Gets the pipeline for the current options. # # @param options [Hash] The options to parse. # @return [Array] The pipeline to process. def get_pipeline() .fetch(:pipeline, self.default_pipeline.map(&:first)).map(&:to_sym) end end |
Instance Method Details
- (Object) render(content, options = {}, context = {})
Renders a content.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mbrao/rendering_engines/html_pipeline.rb', line 51 def render(content, = {}, context = {}) = () context = context.ensure_hash(:symbols) begin create_pipeline(, context).call(get_body(content, ))[:output].to_s rescue Mbrao::Exceptions::UnavailableLocalization => le raise le rescue => e raise ::Mbrao::Exceptions::Rendering.new(e.to_s) end end |