Class: Mbrao::ParsingEngines::PlainText

Inherits:
Base
  • Object
show all
Defined in:
lib/mbrao/parsing_engines/plain_text.rb

Overview

A class for parsing plain text files.

Instance Method Summary collapse

Methods inherited from Base

#parse

Instance Method Details

#filter_content(content, locales = [], options = {}) ⇒ String|HashWithIndifferentAccess

Filters content of a post by locale.

Parameters:

  • content (Content)

    The content to filter.

  • locales (String|Array) (defaults to: [])

    The desired locales. Can include * to match all. If none are specified, the default Mbrao locale will be requested.

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

    Options to customize parsing.

Returns:

  • (String|HashWithIndifferentAccess)

    Return the filtered content in the desired locales. If only one locale is required, then a String is returned, else a HashWithIndifferentAccess with locales as keys.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mbrao/parsing_engines/plain_text.rb', line 52

def filter_content(content, locales = [], options = {})
  body = content.body.ensure_string.strip
   = sanitize_tags(options[:content_tags], ["{{content: %ARGS%}}", "{{/content}}"])
  locales = ::Mbrao::Content.validate_locales(locales, content)

  # Split the content
  result = scan_content(body, .first, .last)

  # Now filter results
  perform_filter_content(result, locales)
end

#parse_metadata(content, options = {}) ⇒ Hash

Parses metadata part and returns all valid metadata.

Parameters:

  • content (String)

    The content to parse.

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

    Options to customize parsing.

Returns:

  • (Hash)

    All valid metadata for the content.



37
38
39
40
41
42
43
# File 'lib/mbrao/parsing_engines/plain_text.rb', line 37

def (content, options = {})
  begin
    YAML.load(content)
  rescue Exception => e
    options[:default] ? options[:default] : (raise ::Mbrao::Exceptions::InvalidMetadata.new(e.to_s))
  end
end

#separate_components(content, options = {}) ⇒ Array

Parses a whole post content and return its metadata and content parts.

Parameters:

  • content (String)

    The content to parse.

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

    Options to customize parsing.

Returns:

  • (Array)

    An array of metadata and contents parts.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mbrao/parsing_engines/plain_text.rb', line 17

def separate_components(content, options = {})
  , content, scanner, start_tag, end_tag = prepare_for_separation(content, options)

  if scanner.scan_until(start_tag) then
     = scanner.scan_until(end_tag)

    if  then
       = .partition(end_tag).first
      content = scanner.rest.strip
    end
  end

  [.ensure_string.strip, content]
end