class Plotline::CustomMarkdownParser

Constants

IMAGE_PATTERN

Matches a single image in the Markdown format: ![alt text](/path/to/image.jpg)

PHOTOSET_HTML
PHOTOSET_PATTERN

Matches photoset enclosing tags: <!– photoset –> … <!– /photoset –>

PHOTOSET_ROW_HTML

Public Class Methods

new(presenter) click to toggle source
# File lib/plotline/custom_markdown_parser.rb, line 3
def initialize(presenter)
  @presenter = presenter
end

Public Instance Methods

parse(text) click to toggle source
# File lib/plotline/custom_markdown_parser.rb, line 18
def parse(text)
  text = parse_photosets(text)
  text = parse_single_images(text)

  text
end
parse_photosets(text) click to toggle source
# File lib/plotline/custom_markdown_parser.rb, line 25
def parse_photosets(text)
  text.gsub(PHOTOSET_PATTERN) do |s|
    # Photoset row is a a set of images separated by 2 new line characters
    rows = $1.gsub("\r", "").strip.split("\n\n").map do |row|
      # Each line in row is considered an "item" (image)
      items  = row.split("\n").reject { |i| i.strip.blank? }
      images = items.map { |image| parse_image(image, :photoset_item) }

      PHOTOSET_ROW_HTML % { items: images.join("\n") }
    end

    PHOTOSET_HTML % { rows: "\n" + rows.join("\n") + "\n" }
  end
end
parse_single_images(text) click to toggle source
# File lib/plotline/custom_markdown_parser.rb, line 40
def parse_single_images(text)
  parse_image(text, :single_image_html)
end

Private Instance Methods

parse_image(text, callback) click to toggle source
# File lib/plotline/custom_markdown_parser.rb, line 46
def parse_image(text, callback)
  text.gsub(IMAGE_PATTERN) do
    attrs = parse_special_attributes($4)

    item = @presenter.send(callback, src: $2, alt: $1, attrs: attrs)
    item = item.gsub(/<figcaption>\s?<\/figcaption>/, '') # remove empty captions:
    item.gsub(/^\s+/, '') # remove indentation from the beginning of lines
  end
end
parse_special_attributes(raw_attrs) click to toggle source

Parses additional attributes placed within brackets:

![](/foo.jpg){.regular hero lang=fr} ![](/bar.jpg){.big the-site data-behavior=lightbox}

Note: works with images only.

# File lib/plotline/custom_markdown_parser.rb, line 62
def parse_special_attributes(raw_attrs)
  return {} if raw_attrs.blank?
  items = raw_attrs.split(/\s+/)

  id      = items.select { |i| i =~ /^#.+/ }.first.gsub('#', '')
  classes = items.select { |i| i =~ /^\..+/ }.map { |c| c.gsub('.', '') }
  attrs   = Hash[items.select { |i| i.include?('=') }.map { |i| i.split('=') }]

  attrs.merge({
    'id' => id,
    'class' => classes.join(' ')
  })
end