module Pasta::Ingredients
Constants
- ANYTHING
- ATX_HEADING
- AUTOMATIC_LINKS
- BLOCKQUOTE
- CODE
- CODEBLOCK
- EMPHASIS
- EMPTY_LINE
- HTML_ENTITIES
- LINKS
Parses both links and images $1 => “!”, $2 => link text, $4 => inline url, $5 => reference id
- LINK_DEFINITION
- 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
automatic_links(text, html)
click to toggle source
# File lib/pasta/ingredients/automatic_links.rb, line 6 def automatic_links(text, html) text.gsub!(AUTOMATIC_LINKS) do |match| url = "#{$1}://#{$2}" match = "<a href=\"#{url}\">#{url}</a>" 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
clean_link_definitions(text)
click to toggle source
# File lib/pasta/ingredients/links.rb, line 44 def clean_link_definitions(text) @link_definitions.each do |key,val| text.gsub!(/^ {0,3}\[#{key}\]: (.+?)(^\s*[\n\r]|\z)/mi, '') end text 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
find_link_definitions(text)
click to toggle source
# File lib/pasta/ingredients/links.rb, line 37 def find_link_definitions(text) @link_definitions = {} text.gsub(LINK_DEFINITION) do |match| @link_definitions[$1.downcase] = {target: $2, used: false} end 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
links(text, html)
click to toggle source
# File lib/pasta/ingredients/links.rb, line 10 def links(text, html) find_link_definitions(text) text.gsub!(LINKS) do |match| link_text, target = $2, '' if $4 # It's an inline link target = $4 else # It's a reference link id = $5 == '' ? $2.downcase : $5.downcase break unless @link_definitions && @link_definitions.key?(id) target = @link_definitions[id][:target] end # format image or link url, title = parse_link target if $1 == '!' match = "<img src=\"#{url}\" alt=\"#{link_text}\"#{title} />" else match = "<a href=\"#{url}\"#{title}>#{link_text}</a>" end end text = clean_link_definitions(text) [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
parse_link(text)
click to toggle source
# File lib/pasta/ingredients/links.rb, line 51 def parse_link(text) target = text.split(' ') url = target[0].gsub(/[<>]/, '') title = " title=#{target[1..-1].join(' ').strip}" if target.length > 1 [url, title] 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