Module: Mbrao::PublicInterface::ClassMethods

Defined in:
lib/mbrao/parser.rb

Overview

Class methods.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (String) default_locale

Returns The mbrao default locale.

Returns:

  • (String)

    The mbrao default locale.



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
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

- (String) locale

Gets the default locale for mbrao.

Returns:

  • (String)

    The default locale.



29
30
31
# File 'lib/mbrao/parser.rb', line 29

def locale
  @locale
end

- (String) parsing_engine

Gets the default parsing engine.

Returns:

  • (String)

    The default parsing engine.



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
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

- (String) rendering_engine

Gets the default rendering engine.

Returns:

  • (String)

    The default rendering engine.



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
# File 'lib/mbrao/parser.rb', line 21

module ClassMethods
  attr_accessor :locale
  attr_accessor :parsing_engine
  attr_accessor :rendering_engine

  # Gets the default locale for mbrao.
  #
  # @return [String] The default locale.
  def locale
    attribute_or_default(@locale, "en")
  end

  # Gets the default parsing engine.
  #
  # @return [String] The default parsing engine.
  def parsing_engine
    attribute_or_default(@parsing_engine, :plain_text, :to_sym)
  end

  # Gets the default rendering engine.
  #
  # @return [String] The default rendering engine.
  def rendering_engine
    attribute_or_default(@rendering_engine, :html_pipeline, :to_sym)
  end

  # Parses a source text.
  #
  # @param content [Object] The content to parse.
  # @param options [Hash] A list of options for parsing.
  # @return [Content] The parsed data.
  def parse(content, options = {})
    instance.parse(content, options)
  end

  # Renders a content.
  #
  # @param content [Content] The content to parse.
  # @param options [Hash] A list of options for renderer.
  # @param context [Hash] A context for rendering.
  # @return [String] The rendered content.
  def render(content, options = {}, context = {})
    instance.render(content, options, context)
  end

  # Instantiates a new engine for rendering or parsing.
  #
  # @param cls [String|Symbol|Object] If a `String` or a `Symbol`, then it will be the class of the engine.
  # @param type [Symbol] The type or engine. Can be `:parsing` or `:rendering`.
  # @return [Object] A new engine.
  def create_engine(cls, type = :parsing)
    begin
      type = :parsing if type != :rendering
      ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
    rescue NameError
      raise Mbrao::Exceptions::UnknownEngine.new
    end
  end

  # Returns a unique (singleton) instance of the parser.
  #
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Parser] The unique (singleton) instance of the parser.
  def instance(force = false)
    @instance = nil if force
    @instance ||= Mbrao::Parser.new
  end

  private
    # Returns an attribute or a default value.
    #
    # @param attr [Object ]The attribute to return.
    # @param default_value [Object] The value to return if `attr` is blank.
    # @param sanitizer [Symbol] An optional method to sanitize the returned value.
    def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
      rv = attr.present? ? attr : default_value
      rv = rv.send(sanitizer) if sanitizer
      rv
    end
end

Instance Method Details

- (Object) create_engine(cls, type = :parsing)

Instantiates a new engine for rendering or parsing.

Parameters:

  • cls (String|Symbol|Object)

    If a String or a Symbol, then it will be the class of the engine.

  • type (Symbol) (defaults to: :parsing)

    The type or engine. Can be :parsing or :rendering.

Returns:

  • (Object)

    A new engine.



71
72
73
74
75
76
77
78
# File 'lib/mbrao/parser.rb', line 71

def create_engine(cls, type = :parsing)
  begin
    type = :parsing if type != :rendering
    ::Lazier.find_class(cls, "::Mbrao::#{type.to_s.classify}Engines::%CLASS%").new
  rescue NameError
    raise Mbrao::Exceptions::UnknownEngine.new
  end
end

- (Parser) instance(force = false)

Returns a unique (singleton) instance of the parser.

Parameters:

  • force (Boolean) (defaults to: false)

    If to force recreation of the instance.

Returns:

  • (Parser)

    The unique (singleton) instance of the parser.



84
85
86
87
# File 'lib/mbrao/parser.rb', line 84

def instance(force = false)
  @instance = nil if force
  @instance ||= Mbrao::Parser.new
end

- (Content) parse(content, options = {})

Parses a source text.

Parameters:

  • content (Object)

    The content to parse.

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

    A list of options for parsing.

Returns:



52
53
54
# File 'lib/mbrao/parser.rb', line 52

def parse(content, options = {})
  instance.parse(content, options)
end

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

Renders a content.

Parameters:

  • content (Content)

    The content to parse.

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

    A list of options for renderer.

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

    A context for rendering.

Returns:

  • (String)

    The rendered content.



62
63
64
# File 'lib/mbrao/parser.rb', line 62

def render(content, options = {}, context = {})
  instance.render(content, options, context)
end