module Pasta::Ingredients

Constants

ANYTHING
ATX_HEADING
BLOCKQUOTE
CODE
CODEBLOCK
EMPHASIS
EMPTY_LINE
HTML_ENTITIES

Parses both links and images $1 => “!”, $2 => link text, $4 => inline url, $5 => reference id

LIST
PAGE_BREAK
PARAGRAPH
SETEXT_HEADING

Public Class Methods

included(base) click to toggle source
# File lib/pasta/ingredients.rb, line 4
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

anything(text,html) click to toggle source
# File lib/pasta/ingredients/anything.rb, line 6
def anything(text,html)
  p text
  html << text
  [text,html]
end
atx_headings(text, html) click to toggle source
# File lib/pasta/ingredients/atx_headings.rb, line 6
def atx_headings(text, html)   
  text.sub!(ATX_HEADING) do |match| 
    inner = $2
    if grammars.key? :heading
      grammars[:heading].each do |rule|
        inner, text = send(rule, $2, '')
      end
    end
    html << "<h#{$1.length}>#{inner.strip}</h#{$1.length}>\n" unless $2.nil?
    match = ''
  end
  [text, html]
end
blockquotes(text, html) click to toggle source
# File lib/pasta/ingredients/blockquotes.rb, line 6
def blockquotes(text, html)
  text.sub!(BLOCKQUOTE) do |match|
    inner_text, inner_html = $1, ''
    inner_text.gsub!(/^>/, '')
    while inner_text != '' do
      if grammars.key? :blockquotes
        grammars[:blockquotes].each do |rule|
          inner_text, inner_html = send(rule, inner_text, inner_html)
        end
      end
    end
    html << "<blockquote>\n#{inner_html.strip}\n</blockquote>\n\n"
    match = ''
  end
  [text, html]   
end
code(text, html) click to toggle source
# File lib/pasta/ingredients/code.rb, line 6
def code(text, html)
  text.gsub!(CODE) do |match|
    require 'cgi'
    html = "<code>#{CGI::escapeHTML $2}</code>"
    match = html
  end
  [text, html]
end
codeblocks(text, html) click to toggle source
# File lib/pasta/ingredients/codeblocks.rb, line 6
def codeblocks(text, html)     
  text.sub!(CODEBLOCK) do |match|
    require 'cgi'
    # clean the leading whitespace from the block and escape html
    new_text = $2
    new_text.gsub!(/^#{$1}/, '')
    new_text = CGI::escapeHTML new_text
    html << "<pre><code>#{new_text}</code></pre>\n\n"
    match = ''
  end
  [text, html]
end
emphasis(text, html) click to toggle source
# File lib/pasta/ingredients/emphasis.rb, line 6
def emphasis(text, html)
  html = text.gsub(EMPHASIS) do |match|
    tag = $1.length == 1 ? 'em' : 'strong'
    match = "<#{tag}>#{$2}</#{tag}>"
  end
  [text, html]
end
empty_lines(text, html) click to toggle source
# File lib/pasta/ingredients/empty_lines.rb, line 6
def empty_lines(text, html)
  text.sub!(EMPTY_LINE) do |match|
    match = ''
  end
  [text, html]
end
grammars(name=nil) click to toggle source

Public. Returns the grammars of a recipe

name - the name of a grammar rule as a Symbol

Example

grammars(:html)
# => <Proc:>

Returns a Proc containing grammar rules

# File lib/pasta/ingredients.rb, line 17
def grammars(name=nil)
  return name.nil? ? self.class.grammars : self.class.grammars[name]
end
html_entities(text, html) click to toggle source
# File lib/pasta/ingredients/html_entities.rb, line 8
def html_entities(text, html)
  HTML_ENTITIES.each do |entity, transformation|
    html = text.gsub(/[#{entity}]/) do |match|
      match = transformation
    end
  end
  [text, html]
end
list_items(text) click to toggle source
# File lib/pasta/ingredients/lists.rb, line 22
def list_items(text)
  html = ''
  text.gsub(/^([-*+]|\d+\.) (.+?)$(?=([\n\r]([*+-]|(\d+\.))|\z))/m) do |match| 
    html << "<li>#{$2.gsub(/\n\s+/, ' ').strip}</li>\n"
    match = ''
  end
  html
end
lists(text, html) click to toggle source
# File lib/pasta/ingredients/lists.rb, line 6
def lists(text, html)
  text.sub!(LIST) do |match|
    inner_text, inner_html, list_type = $1, '', 'ul'
    
    # determine the list_type
    list_type = 'ol' unless %w{- * +}.include? $1.strip[0]
    
    # extract list items
    inner_html = list_items(inner_text)
    
    html << "<#{list_type}>\n#{inner_html}</#{list_type}>\n\n"
    match = ''
  end
  [text, html]
end
page_breaks(text, html) click to toggle source
# File lib/pasta/ingredients/page_breaks.rb, line 6
def page_breaks(text, html)
  text.sub!(PAGE_BREAK) do |match|
    html << "<hr />"
    match = ''
  end
  [text, html]
end
paragraphs(text, html) click to toggle source
# File lib/pasta/ingredients/paragraphs.rb, line 6
def paragraphs(text, html)
  text.sub!(PARAGRAPH) do |match| 

    inner_text, inner_html = $1, ''
    inner_text.gsub!(/[ ]{2,}[\n\r]/, "<br />\n")      
    inner_text.strip!

    if grammars.key? :paragraphs
      grammars[:paragraphs].each do |rule|
        inner_text, inner_html = send(rule, inner_text, '')
      end
      inner_text = inner_html unless inner_html == ''
    end

    html << "<p>#{inner_text}</p>\n\n" unless inner_text == ''
    match = ''
  end
  [text, html]  
end
setext_headings(text, html) click to toggle source
# File lib/pasta/ingredients/setext_headings.rb, line 6
def setext_headings(text, html)    
  text.sub!(SETEXT_HEADING) do |match| 
    if grammars.key? :heading
      grammars[:heading].each do |rule|
        inner = send(rule, $1, '')
      end
    end
    level = ($2[0] == '=') ? 1 : 2
    html << "<h#{level}>#{$1}</h#{level}>\n" unless $1.nil?
    match = ''
  end    
  [text, html]
end