module MarkdownParser::Rules

Public Class Methods

append_queue(line) click to toggle source

adds at the beginning of the line the string in the @state

# File lib/markdown_parser/rules.rb, line 95
def self.append_queue(line)
  to_append = @state[:to_append]
  @state[:to_append] = nil
  to_append.nil? ? line : to_append + line
end
apply(line) click to toggle source
# File lib/markdown_parser/rules.rb, line 12
def self.apply(line)
  apply_inline_style(apply_line(line))
end
apply_blockquote(line) click to toggle source

blockquote method

# File lib/markdown_parser/rules.rb, line 64
def self.apply_blockquote(line)
  "<p><blockquote>#{line[2..-1]}</blockquote></p>"
end
apply_code(line) click to toggle source

multi-line code method

# File lib/markdown_parser/rules.rb, line 69
def self.apply_code(line)
  line = @state[:code_tag_opened] ? "</code></pre>" : "<pre><code>"
  toggle(:code_tag_opened)
  line
end
apply_header(line) click to toggle source

haeder method

# File lib/markdown_parser/rules.rb, line 57
def self.apply_header(line)
  depth = title_depth(line)
  return line if depth == 0
  line.gsub(eval("/^#{'#' * depth}\s/"), "<h#{depth}>").gsub(/$/, "</h#{depth}>")
end
apply_inline_style(line) click to toggle source
# File lib/markdown_parser/rules.rb, line 41
def self.apply_inline_style(line)
  # **strong**
  line.gsub!(/\*\*(?<word>[^\*]*)\*\*/, "<strong>\\k<word></strong>")
  # _em_
  line.gsub!(/\_(?<word>[^_]*)\_/, "<em>\\k<word></em>")
  # `code`
  line.gsub!(/`(?<word>[^`]*)`/, "<code>\\k<word></code>")
  # [alt message](image_url)
  line.gsub!(/!\[(?<alt>[^\]]*)\]\((?<link>[^\)]*)\)/, '<img src="\k<link>" alt="\k<alt>" />')
  # [text](limk)
  line.gsub!(/\[(?<text>[^\]]*)\]\((?<link>[^\)]*)\)/, '<a href="\k<link>">\k<text></a>')

  append_queue line
end
apply_line(line) click to toggle source
# File lib/markdown_parser/rules.rb, line 16
def self.apply_line(line)
  line = EscapeUtils.unescape_html(line)

  before_parse :ensure_to_close_list, line

  return "#{line}\n" if @state[:code_tag_opened] and !line.match(/^```$/)

  case line
  when /^#+\s/ # titles
    apply_header(line)
  when /^(------|======)\s*$/ # alternative titles
    apply_header(line)
  when /^(---|___|\*\*\*)\s*$/ # line separator
    "<hr />"
  when /^>\s.+$/ # blockqoutes
    apply_blockquote(line)
  when /^```.*/ # code
    apply_code(line)
  when /^(-|\*|\+|\d+)\s.+/ # list item
    apply_list(line)
  else
    line.empty? ? line : "#{line}"
  end
end
apply_list(line) click to toggle source

list item method, also add ul tag when is the first to be added

# File lib/markdown_parser/rules.rb, line 76
def self.apply_list(line)
  line = "<li>#{line[2..-1]}</li>"
  line = "<ul>" + line unless @state[:last_was_list]
  toggle(:list_opened)
  @state[:last_was_list] = true
  line
end
before_parse(symbol, param) click to toggle source

execute the method passed as symbol and adds the param to it

# File lib/markdown_parser/rules.rb, line 102
def self.before_parse(symbol, param)
  send(symbol, param)
end
ensure_to_close_list(line) click to toggle source

check if the last line added was a list and if the current line isn't, so, add a </ul>n

# File lib/markdown_parser/rules.rb, line 107
def self.ensure_to_close_list(line)
  if @state[:last_was_list] && !['- ', '+ ', '* '].include?(line[0,2])
    @state[:last_was_list] = false
    @state[:to_append] = "</ul>"
  end
end
title_depth(str) click to toggle source

define how many #'s a string has'

# File lib/markdown_parser/rules.rb, line 90
def self.title_depth(str)
  str.match(/^#+\s*/).to_s.size - 1
end
toggle(symbol) click to toggle source

toggle boolean variables

# File lib/markdown_parser/rules.rb, line 85
def self.toggle(symbol)
  @state[symbol] = !@state[symbol]
end