module Quesadilla::Extractor::Markdown

Extract Markdown.

This module has no public methods.

Constants

1 = Email

1 = URL

BOLD_ITALIC_REGEX

1 = Delimiter, 2 = Text

BOLD_REGEX

1 = Delimiter, 2 = Text

CODE_REGEX

1 = Delimiter, 2 = Text

ITALIC_REGEX

1 = Delimiter, 2 = Text

2 = Text, 3 = URL, 6 = Title

NESTED_BRACKETS_REGEX

Gruber’s regex is recursive, but I can’t figure out how to do it in Ruby without the ‘g` option. Maybe I should use StringScanner instead. For now, I think it’s fine. Everything appears to work as expected.

STRIKETHROUGH_REGEX

1 = Delimiter, 2 = Text

Private Instance Methods

extract_markdown() click to toggle source
# File lib/quesadilla/extractor/markdown.rb, line 69
def extract_markdown
  extract_markdown_code if @options[:markdown_code]

  if @options[:markdown_links]
    extract_markdown_autolink_links
    extract_markdown_autolink_email
    extract_markdown_links
  end

  extract_markdown_span(BOLD_ITALIC_REGEX, ENTITY_TYPE_TRIPLE_EMPHASIS) if @options[:markdown_triple_emphasis]
  extract_markdown_span(BOLD_REGEX, ENTITY_TYPE_DOUBLE_EMPHASIS) if @options[:markdown_double_emphasis]
  extract_markdown_span(ITALIC_REGEX, ENTITY_TYPE_EMPHASIS) if @options[:markdown_emphasis]
  extract_markdown_span(STRIKETHROUGH_REGEX, ENTITY_TYPE_STRIKETHROUGH) if @options[:markdown_strikethrough]
end
extract_markdown_code() click to toggle source
# File lib/quesadilla/extractor/markdown.rb, line 117
def extract_markdown_code
  extract_markdown_span(CODE_REGEX, 'code') do |entity, match|
    # Strip tabs from the display text
    display = match[2]
    display.gsub!(/^[ \t]*/, '')
    display.gsub!(/[ \t]*$/, '')
    entity[:display_text] = display
    entity
  end
end
extract_markdown_span(regex, type) { |entity, match| ... } click to toggle source
# File lib/quesadilla/extractor/markdown.rb, line 86
def extract_markdown_span(regex, type)
  # Match until there's no results
  while match = @working_text.match(regex)
    original = match[0]
    length = original.length

    # Find the start position of the original
    start = @working_text.index(original)

    # Create the entity
    entity = {
      type: type,
      text: original,
      display_text: match[2],
      indices: [
        start,
        start + length
      ]
    }

    # Let block modify
    entity = yield(entity, match) if block_given?

    # Add the entity
    @entities << entity

    # Remove from the working text
    @working_text.sub!(original, REPLACE_TOKEN * length)
  end
end