class Mbrao::ParsingEngines::PlainText

A class for parsing plain text files.

Public Instance Methods

filter_content(content, locales = [], options = {}) click to toggle source

Filters content of a post by locale.

@param content [Content] The content to filter. @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default Mbrao locale will be requested. @param options [Hash] Options to customize parsing. @return [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.
# File lib/mbrao/parsing_engines/plain_text.rb, line 54
def filter_content(content, locales = [], options = {})
  body = content.body.ensure_string.strip
  content_tags = sanitize_tags(options[:content_tags], ["{{content: %ARGS%}}", "{{/content}}"])
  locales = ::Mbrao::Content.validate_locales(locales, content)

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

  # Now filter results
  perform_filter_content(result, locales)
end
parse_metadata(content, options = {}) click to toggle source

Parses metadata part and returns all valid metadata.

@param content [String] The content to parse. @param options [Hash] Options to customize parsing. @return [Hash] All valid metadata for the content.

# File lib/mbrao/parsing_engines/plain_text.rb, line 37
def parse_metadata(content, options = {})
  rv = YAML.load(content)
  rv ||= {}
  raise ArgumentError unless rv.is_a?(Hash)
  rv
rescue => e
  raise(::Mbrao::Exceptions::InvalidMetadata, e.to_s) unless options[:default]
  options[:default]
end
separate_components(content, options = {}) click to toggle source

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

@param content [String] The content to parse. @param options [Hash] Options to customize parsing. @return [Array] An array of metadata and contents parts.

# File lib/mbrao/parsing_engines/plain_text.rb, line 17
def separate_components(content, options = {})
  metadata, content, scanner, start_tag, end_tag = prepare_for_separation(content, options)

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

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

  [metadata.ensure_string.strip, content]
end