Class: Plotline::CustomMarkdownParser

Inherits:
Object
  • Object
show all
Defined in:
lib/plotline/custom_markdown_parser.rb

Constant Summary collapse

PHOTOSET_PATTERN =

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

/---(\s*\n.*?)\n?^---\s*$\n?/m
IMAGE_PATTERN =

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

/\!\[([^\]]*)\]\(([^)]+)\)(\{([^{]+)\})?/
PHOTOSET_HTML =
"<div class=\"photoset\">%{rows}</div>\n\n"
PHOTOSET_ROW_HTML =
"<div class=\"photoset-row\">%{items}</div>"

Instance Method Summary collapse

Constructor Details

#initialize(presenter) ⇒ CustomMarkdownParser

Returns a new instance of CustomMarkdownParser.



3
4
5
# File 'lib/plotline/custom_markdown_parser.rb', line 3

def initialize(presenter)
  @presenter = presenter
end

Instance Method Details

#parse(text) ⇒ Object



18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
# File 'lib/plotline/custom_markdown_parser.rb', line 40

def parse_single_images(text)
  parse_image(text, :single_image_html)
end