class Minidown::Document

Constants

RefRegexp
TagRegexp

Attributes

lines[RW]
nodes[RW]
options[RW]
within_block[R]

Public Class Methods

new(lines, options = {}) click to toggle source
# File lib/minidown/document.rb, line 15
def initialize lines, options = {}
  @options = options
  @lines = lines
  @nodes = []
  @within_block = false
  @links_ref = {}
end

Public Instance Methods

parse() click to toggle source
# File lib/minidown/document.rb, line 23
def parse
  parse_references

  while line = @lines.shift
    parse_line line
  end
end
parse_line(line) click to toggle source
# File lib/minidown/document.rb, line 60
def parse_line line
  regexp = Minidown::Utils::Regexp
  case
  when regexp[:blank_line] =~ line
    # blankline
    newline line
  when !pre_blank? && (result = regexp[:h1_or_h2] =~ line; next_line = $2; result) && ParagraphElement === nodes.last
    # ======== or -------
    break_if_list line do
      lines.unshift next_line if next_line && !next_line.empty?
      html_tag nodes.pop, (line[0] == '='.freeze ? 'h1'.freeze : 'h2'.freeze)
    end
  when regexp[:start_with_shape] =~ line
    # ####h4
    break_if_list line do
      text $2
      html_tag nodes.pop, "h#{$1.size}"
    end
  when regexp[:start_with_quote] =~ line
    # > blockquote
    inblock{block $1}
  when regexp[:dividing_line] =~ line
    # * * * - - -
    break_if_list line do
      dividing_line line
    end
  when regexp[:unorder_list] =~ line
    # * + -
    indent, str = $1.size, $2
    inblock{ul str, indent}
  when regexp[:order_list] =~ line
    # 1. order
    indent, str = $1.size, $2
    inblock{ol str, indent}
  when regexp[:code_block] =~ line
    # ``` or ~~~
    inblock{code_block $1}
  when !@within_block && pre_blank? && regexp[:indent_code] =~ line
    #    code
    indent_code $1
  when regexp[:pipe_symbol] =~ line && regexp[:table] =~ line
    # column1 | column2 | ...
    table = TableElement.new self, line, $1
    raw_column_spec = @lines.shift
    if table.check_column_spec raw_column_spec
      inblock{table.parse}
    else
      @lines.unshift raw_column_spec if raw_column_spec
      paragraph line
    end
  else
    # paragraph
    paragraph line
  end
end
parse_references() click to toggle source
# File lib/minidown/document.rb, line 44
def parse_references
  while line = @lines.pop
    line.gsub! RefRegexp[:link_ref_define] do
      id, url = $1, $2
      $3 =~ RefRegexp[:link_title]
      title = $1
      links_ref[id.downcase] = {url: url, title: title}
      ''
    end
    unless line.empty?
      @lines << line
      break
    end
  end
end
to_html() click to toggle source
# File lib/minidown/document.rb, line 31
def to_html
  @html ||= (doc = ''
   @nodes.each{|e| doc << e.to_html}
   doc)
end

Private Instance Methods

break_if_list(line) { || ... } click to toggle source
# File lib/minidown/document.rb, line 132
def break_if_list line
  node = nodes.last
  if @within_block && (UnorderListElement === node || OrderListElement === node)
    @lines.unshift line
    nodes << nil
  else
    yield
  end
end
inblock() { || ... } click to toggle source
# File lib/minidown/document.rb, line 122
def inblock
  if @within_block
    yield
  else
    @within_block = true
    yield
    @within_block = false
  end
end
pre_blank?() click to toggle source
# File lib/minidown/document.rb, line 117
def pre_blank?
  node = @nodes.last
  node.nil? || node.blank?
end