Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

0.4.0

- restructuring
- add creole tilt template
  • Loading branch information...
commit 77d04ad9128a63da59c888ab6c261f5c2cd92514 1 parent 0f45a94
Daniel Mendler authored
5 Manifest.txt
View
@@ -1,5 +0,0 @@
-Manifest.txt
-README.creole
-Rakefile
-lib/creole.rb
-test/test_creole.rb
35 creole.gemspec
View
@@ -1,23 +1,24 @@
# -*- encoding: utf-8 -*-
+require File.dirname(__FILE__) + '/lib/creole/version'
+require 'date'
Gem::Specification.new do |s|
- s.name = %q{creole}
- s.version = '0.3.8'
+ s.name = 'creole'
+ s.version = Creole::VERSION
+ s.date = Date.today.to_s
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Lars Christensen", "Daniel Mendler"]
- s.date = %q{2009-02-16}
- s.description = %q{Creole is a Creole-to-HTML converter for Creole, the lightweight markup language (http://wikicreole.org/).}
- s.email = ["larsch@belunktum.dk", "mail@daniel-mendler.de"]
- s.extra_rdoc_files = ["Manifest.txt", "README.creole"]
- s.files = ["Manifest.txt", "README.creole", "Rakefile", "lib/creole.rb", "test/creole_test.rb"]
- s.has_rdoc = true
- s.homepage = %q{http://github.com/minad/creole}
- s.rdoc_options = ["--main", "README.creole"]
- s.require_paths = ["lib"]
- s.rubyforge_project = %q{creole}
- s.rubygems_version = %q{1.3.1}
- s.summary = %q{Creole is a Creole-to-HTML converter for Creole, the lightweight markup language (http://wikicreole.org/).}
- s.test_files = ["test/creole_test.rb"]
+ s.authors = ['Lars Christensen', 'Daniel Mendler']
+ s.summary = 'Lightweight markup language'
+ s.description = 'Creole is a lightweight markup language (http://wikicreole.org/).'
+ s.email = ['larsch@belunktum.dk', 'mail@daniel-mendler.de']
+ s.extra_rdoc_files = %w(README.creole)
+ s.rubyforge_project = s.name
+
+ s.files = `git ls-files -- lib/* bin/* README* test/* Rakefile`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = %w(lib)
+
+ s.homepage = 'http://github.com/minad/creole'
+ s.rdoc_options = %w(--main README.creole)
s.add_development_dependency('bacon')
end
384 lib/creole.rb
View
@@ -1,44 +1,7 @@
-require 'cgi'
-require 'uri'
-
-# :main: Creole
-
-# The Creole parses and translates Creole formatted text into
-# XHTML. Creole is a lightweight markup syntax similar to what many
-# WikiWikiWebs use. Example syntax:
-#
-# = Heading 1 =
-# == Heading 2 ==
-# === Heading 3 ===
-# **Bold text**
-# //Italic text//
-# [[Links]]
-# |=Table|=Heading|
-# |Table |Cells |
-# {{image.png}}
-#
-# The simplest interface is Creole.creolize. The default handling of
-# links allow explicit local links using the [[link]] syntax. External
-# links will only be allowed if specified using http(s) and ftp(s)
-# schemes. If special link handling is needed, such as inter-wiki or
-# hierachical local links, you must inherit Creole::CreoleParser and
-# override make_local_link.
-#
-# You can customize the created anchor/image markup by overriding
-# make_*_anchor/make_image.
-
-# Main Creole parser class. Call CreoleParser#parse to parse Creole
-# formatted text.
-#
-# This class is not reentrant. A separate instance is needed for
-# each thread that needs to convert Creole to HTML.
-#
-# Inherit this to provide custom handling of links. The overrideable
-# methods are: make_local_link
-class Creole
-
- VERSION = '0.3.8'
+require 'creole/parser'
+require 'creole/version'
+module Creole
# Convert the argument in Creole format to HTML and return the
# result. Example:
#
@@ -48,345 +11,6 @@ class Creole
# This is an alias for calling Creole#parse:
# Creole.new(text).to_html
def self.creolize(text, options = {})
- new(text, options).to_html
- end
-
- # Allowed url schemes
- # Examples: http https ftp ftps
- attr_accessor :allowed_schemes
-
- # Non-standard wiki text extensions enabled?
- # E.g. underlined, deleted text etc
- attr_writer :extensions
- def extensions?; @extensions; end
-
- # Disable url escaping for local links
- # Escaping: [[/Test]] --> %2FTest
- # No escaping: [[/Test]] --> Test
- attr_writer :no_escape
- def no_escape?; @no_escape; end
-
- # Create a new CreoleParser instance.
- def initialize(text, options = {})
- @allowed_schemes = %w(http https ftp ftps)
- @text = text
- options.each_pair {|k,v| send("#{k}=", v) }
- end
-
- # Convert CCreole text to HTML and return
- # the result. The resulting HTML does not contain <html> and
- # <body> tags.
- #
- # Example:
- #
- # parser = CreoleParser.new("**Hello //World//**", :extensions => true)
- # parser.to_html
- # #=> "<p><strong>Hello <em>World</em></strong></p>"
- def to_html
- @out = ''
- @p = false
- @stack = []
- parse_block(@text)
- @out
- end
-
- protected
-
- # Escape any characters with special meaning in HTML using HTML
- # entities.
- def escape_html(string)
- CGI::escapeHTML(string)
- end
-
- # Escape any characters with special meaning in URLs using URL
- # encoding.
- def escape_url(string)
- CGI::escape(string)
- end
-
- def start_tag(tag)
- @stack.push(tag)
- @out << '<' << tag << '>'
- end
-
- def end_tag
- @out << '</' << @stack.pop << '>'
- end
-
- def toggle_tag(tag, match)
- if @stack.include?(tag)
- if @stack.last == tag
- end_tag
- else
- @out << escape_html(match)
- end
- else
- start_tag(tag)
- end
- end
-
- def end_paragraph
- end_tag while !@stack.empty?
- @p = false
- end
-
- def start_paragraph
- if @p
- @out << ' ' if @out[-1,1] != ' '
- else
- end_paragraph
- start_tag('p')
- @p = true
- end
- end
-
- # Create anchor markup for direct links. This
- # method can be overridden to generate custom
- # markup, for example to add html additional attributes.
- def make_direct_anchor(uri, text)
- '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
- end
-
- # Create anchor markup for explicit links. This
- # method can be overridden to generate custom
- # markup, for example to add html additional attributes.
- def make_explicit_anchor(uri, text)
- '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
- end
-
- # Translate an explicit local link to a desired URL that is
- # properly URL-escaped. The default behaviour is to convert local
- # links directly, escaping any characters that have special
- # meaning in URLs. Relative URLs in local links are not handled.
- #
- # Examples:
- #
- # make_local_link("LocalLink") #=> "LocalLink"
- # make_local_link("/Foo/Bar") #=> "%2FFoo%2FBar"
- #
- # Must ensure that the result is properly URL-escaped. The caller
- # will handle HTML escaping as necessary. HTML links will not be
- # inserted if the function returns nil.
- #
- # Example custom behaviour:
- #
- # make_local_link("LocalLink") #=> "/LocalLink"
- # make_local_link("Wikipedia:Bread") #=> "http://en.wikipedia.org/wiki/Bread"
- def make_local_link(link) #:doc:
- no_escape? ? link : escape_url(link)
+ Parser.new(text, options).to_html
end
-
- # Sanatize a direct url (e.g. http://wikipedia.org/). The default
- # behaviour returns the original link as-is.
- #
- # Must ensure that the result is properly URL-escaped. The caller
- # will handle HTML escaping as necessary. Links will not be
- # converted to HTML links if the function returns link.
- #
- # Custom versions of this function in inherited classes can
- # implement specific link handling behaviour, such as redirection
- # to intermediate pages (for example, for notifing the user that
- # he is leaving the site).
- def make_direct_link(url) #:doc:
- url
- end
-
- # Sanatize and prefix image URLs. When images are encountered in
- # Creole text, this function is called to obtain the actual URL of
- # the image. The default behaviour is to return the image link
- # as-is. No image tags are inserted if the function returns nil.
- #
- # Custom version of the method can be used to sanatize URLs
- # (e.g. remove query-parts), inhibit off-site images, or add a
- # base URL, for example:
- #
- # def make_image_link(url)
- # URI.join("http://mywiki.org/images/", url)
- # end
- def make_image_link(url) #:doc:
- url
- end
-
- # Create image markup. This
- # method can be overridden to generate custom
- # markup, for example to add html additional attributes or
- # to put divs around the imgs.
- def make_image(uri, alt)
- if alt
- '<img src="' << escape_html(uri) << '" alt="' << escape_html(alt) << '"/>'
- else
- '<img src="' << escape_html(uri) << '"/>'
- end
- end
-
- def make_explicit_link(link)
- begin
- uri = URI.parse(link)
- return uri.to_s if uri.scheme && @allowed_schemes.include?(uri.scheme)
- rescue URI::InvalidURIError
- end
- make_local_link(link)
- end
-
- def parse_inline(str)
- until str.empty?
- case str
- when /\A(\~)?((https?|ftps?):\/\/\S+?)(?=([\,.?!:;"'\)]+)?(\s|$))/
- if $1
- @out << escape_html($2)
- else
- if uri = make_direct_link($2)
- @out << make_direct_anchor(uri, $2)
- else
- @out << escape_html($&)
- end
- end
- when /\A\[\[\s*([^|]*?)\s*(\|\s*(.*?))?\s*\]\]/m
- link = $1
- if uri = make_explicit_link(link)
- @out << make_explicit_anchor(uri, $3 || link)
- else
- @out << escape_html($&)
- end
- when /\A\{\{\{(.*?\}*)\}\}\}/
- @out << '<tt>' << escape_html($1) << '</tt>'
- when /\A\{\{\s*(.*?)\s*(\|\s*(.*?)\s*)?\}\}/
- if uri = make_image_link($1)
- @out << make_image(uri, $3)
- else
- @out << escape_html($&)
- end
- when /\A([:alpha:]|[:digit:])+/
- @out << $&
- when /\A\s+/
- @out << ' ' if @out[-1,1] != ' '
- when /\A\*\*/
- toggle_tag 'strong', $&
- when /\A\/\//
- toggle_tag 'em', $&
- when /\A\\\\/
- @out << '<br/>'
- else
- if @extensions
- case str
- when /\A__/
- toggle_tag 'u', $&
- when /\A\-\-/
- toggle_tag 'del', $&
- when /\A\+\+/
- toggle_tag 'ins', $&
- when /\A\^\^/
- toggle_tag 'sup', $&
- when /\A\~\~/
- toggle_tag 'sub', $&
- when /\A\(R\)/i
- @out << '&#174;'
- when /\A\(C\)/i
- @out << '&#169;'
- when /\A~([^\s])/
- @out << escape_html($1)
- when /./
- @out << escape_html($&)
- end
- else
- case str
- when /\A~([^\s])/
- @out << escape_html($1)
- when /./
- @out << escape_html($&)
- end
- end
- end
- str = $'
- end
- end
-
- def parse_table_row(str)
- @out << '<tr>'
- str.scan(/\s*\|(=)?\s*((\[\[.*?\]\]|\{\{.*?\}\}|[^|~]|~.)*)(?=\||$)/) do
- if !$2.empty? || !$'.empty?
- @out << ($1 ? '<th>' : '<td>')
- parse_inline($2) if $2
- end_tag while @stack.last != 'table'
- @out << ($1 ? '</th>' : '</td>')
- end
- end
- @out << '</tr>'
- end
-
- def make_nowikiblock(input)
- input.gsub(/^ (?=\}\}\})/, '')
- end
-
- def ulol?(x); x == 'ul' || x == 'ol'; end
-
- def parse_block(str)
- until str.empty?
- case str
- when /\A\{\{\{\r?\n(.*?)\r?\n\}\}\}/m
- end_paragraph
- nowikiblock = make_nowikiblock($1)
- @out << '<pre>' << escape_html(nowikiblock) << '</pre>'
- when /\A\s*-{4,}\s*$/
- end_paragraph
- @out << '<hr/>'
- when /\A\s*(={1,6})\s*(.*?)\s*=*\s*$(\r?\n)?/
- end_paragraph
- level = $1.size
- @out << "<h#{level}>" << escape_html($2) << "</h#{level}>"
- when /\A[ \t]*\|.*$(\r?\n)?/
- if !@stack.include?('table')
- end_paragraph
- start_tag('table')
- end
- parse_table_row($&)
- when /\A\s*$(\r?\n)?/
- end_paragraph
- when /\A(\s*([*#]+)\s*(.*?))$(\r?\n)?/
- line, bullet, item = $1, $2, $3
- tag = (bullet[0,1] == '*' ? 'ul' : 'ol')
- if bullet[0,1] == '#' || bullet.size != 2 || @stack.find {|x| ulol?(x) }
- count = @stack.select { |x| ulol?(x) }.size
-
- while !@stack.empty? && count > bullet.size
- count -= 1 if ulol?(@stack.last)
- end_tag
- end
-
- end_tag while !@stack.empty? && @stack.last != 'li'
-
- if @stack.last == 'li' && count == bullet.size
- end_tag
- if @stack.last != tag
- end_tag
- count -= 1
- end
- end
-
- while count < bullet.size
- start_tag tag
- count += 1
- start_tag 'li' if count < bullet.size
- end
-
- @p = true
- start_tag('li')
- parse_inline(item)
- else
- start_paragraph
- parse_inline(line)
- end
- when /\A([ \t]*\S+.*?)$(\r?\n)?/
- start_paragraph
- parse_inline($1)
- else
- raise "Parse error at #{str[0,30].inspect}"
- end
- #p [$&, $']
- str = $'
- end
- end_paragraph
- @out
- end
-
end
379 lib/creole/parser.rb
View
@@ -0,0 +1,379 @@
+require 'cgi'
+require 'uri'
+
+# :main: Creole
+
+# The Creole parses and translates Creole formatted text into
+# XHTML. Creole is a lightweight markup syntax similar to what many
+# WikiWikiWebs use. Example syntax:
+#
+# = Heading 1 =
+# == Heading 2 ==
+# === Heading 3 ===
+# **Bold text**
+# //Italic text//
+# [[Links]]
+# |=Table|=Heading|
+# |Table |Cells |
+# {{image.png}}
+#
+# The simplest interface is Creole.creolize. The default handling of
+# links allow explicit local links using the [[link]] syntax. External
+# links will only be allowed if specified using http(s) and ftp(s)
+# schemes. If special link handling is needed, such as inter-wiki or
+# hierachical local links, you must inherit Creole::CreoleParser and
+# override make_local_link.
+#
+# You can customize the created anchor/image markup by overriding
+# make_*_anchor/make_image.
+
+# Main Creole parser class. Call CreoleParser#parse to parse Creole
+# formatted text.
+#
+# This class is not reentrant. A separate instance is needed for
+# each thread that needs to convert Creole to HTML.
+#
+# Inherit this to provide custom handling of links. The overrideable
+# methods are: make_local_link
+module Creole
+ class Parser
+
+ # Allowed url schemes
+ # Examples: http https ftp ftps
+ attr_accessor :allowed_schemes
+
+ # Non-standard wiki text extensions enabled?
+ # E.g. underlined, deleted text etc
+ attr_writer :extensions
+ def extensions?; @extensions; end
+
+ # Disable url escaping for local links
+ # Escaping: [[/Test]] --> %2FTest
+ # No escaping: [[/Test]] --> Test
+ attr_writer :no_escape
+ def no_escape?; @no_escape; end
+
+ # Create a new CreoleParser instance.
+ def initialize(text, options = {})
+ @allowed_schemes = %w(http https ftp ftps)
+ @text = text
+ options.each_pair {|k,v| send("#{k}=", v) }
+ end
+
+ # Convert CCreole text to HTML and return
+ # the result. The resulting HTML does not contain <html> and
+ # <body> tags.
+ #
+ # Example:
+ #
+ # parser = CreoleParser.new("**Hello //World//**", :extensions => true)
+ # parser.to_html
+ # #=> "<p><strong>Hello <em>World</em></strong></p>"
+ def to_html
+ @out = ''
+ @p = false
+ @stack = []
+ parse_block(@text)
+ @out
+ end
+
+ protected
+
+ # Escape any characters with special meaning in HTML using HTML
+ # entities.
+ def escape_html(string)
+ CGI::escapeHTML(string)
+ end
+
+ # Escape any characters with special meaning in URLs using URL
+ # encoding.
+ def escape_url(string)
+ CGI::escape(string)
+ end
+
+ def start_tag(tag)
+ @stack.push(tag)
+ @out << '<' << tag << '>'
+ end
+
+ def end_tag
+ @out << '</' << @stack.pop << '>'
+ end
+
+ def toggle_tag(tag, match)
+ if @stack.include?(tag)
+ if @stack.last == tag
+ end_tag
+ else
+ @out << escape_html(match)
+ end
+ else
+ start_tag(tag)
+ end
+ end
+
+ def end_paragraph
+ end_tag while !@stack.empty?
+ @p = false
+ end
+
+ def start_paragraph
+ if @p
+ @out << ' ' if @out[-1,1] != ' '
+ else
+ end_paragraph
+ start_tag('p')
+ @p = true
+ end
+ end
+
+ # Create anchor markup for direct links. This
+ # method can be overridden to generate custom
+ # markup, for example to add html additional attributes.
+ def make_direct_anchor(uri, text)
+ '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
+ end
+
+ # Create anchor markup for explicit links. This
+ # method can be overridden to generate custom
+ # markup, for example to add html additional attributes.
+ def make_explicit_anchor(uri, text)
+ '<a href="' << escape_html(uri) << '">' << escape_html(text) << '</a>'
+ end
+
+ # Translate an explicit local link to a desired URL that is
+ # properly URL-escaped. The default behaviour is to convert local
+ # links directly, escaping any characters that have special
+ # meaning in URLs. Relative URLs in local links are not handled.
+ #
+ # Examples:
+ #
+ # make_local_link("LocalLink") #=> "LocalLink"
+ # make_local_link("/Foo/Bar") #=> "%2FFoo%2FBar"
+ #
+ # Must ensure that the result is properly URL-escaped. The caller
+ # will handle HTML escaping as necessary. HTML links will not be
+ # inserted if the function returns nil.
+ #
+ # Example custom behaviour:
+ #
+ # make_local_link("LocalLink") #=> "/LocalLink"
+ # make_local_link("Wikipedia:Bread") #=> "http://en.wikipedia.org/wiki/Bread"
+ def make_local_link(link) #:doc:
+ no_escape? ? link : escape_url(link)
+ end
+
+ # Sanatize a direct url (e.g. http://wikipedia.org/). The default
+ # behaviour returns the original link as-is.
+ #
+ # Must ensure that the result is properly URL-escaped. The caller
+ # will handle HTML escaping as necessary. Links will not be
+ # converted to HTML links if the function returns link.
+ #
+ # Custom versions of this function in inherited classes can
+ # implement specific link handling behaviour, such as redirection
+ # to intermediate pages (for example, for notifing the user that
+ # he is leaving the site).
+ def make_direct_link(url) #:doc:
+ url
+ end
+
+ # Sanatize and prefix image URLs. When images are encountered in
+ # Creole text, this function is called to obtain the actual URL of
+ # the image. The default behaviour is to return the image link
+ # as-is. No image tags are inserted if the function returns nil.
+ #
+ # Custom version of the method can be used to sanatize URLs
+ # (e.g. remove query-parts), inhibit off-site images, or add a
+ # base URL, for example:
+ #
+ # def make_image_link(url)
+ # URI.join("http://mywiki.org/images/", url)
+ # end
+ def make_image_link(url) #:doc:
+ url
+ end
+
+ # Create image markup. This
+ # method can be overridden to generate custom
+ # markup, for example to add html additional attributes or
+ # to put divs around the imgs.
+ def make_image(uri, alt)
+ if alt
+ '<img src="' << escape_html(uri) << '" alt="' << escape_html(alt) << '"/>'
+ else
+ '<img src="' << escape_html(uri) << '"/>'
+ end
+ end
+
+ def make_explicit_link(link)
+ begin
+ uri = URI.parse(link)
+ return uri.to_s if uri.scheme && @allowed_schemes.include?(uri.scheme)
+ rescue URI::InvalidURIError
+ end
+ make_local_link(link)
+ end
+
+ def parse_inline(str)
+ until str.empty?
+ case str
+ when /\A(\~)?((https?|ftps?):\/\/\S+?)(?=([\,.?!:;"'\)]+)?(\s|$))/
+ if $1
+ @out << escape_html($2)
+ else
+ if uri = make_direct_link($2)
+ @out << make_direct_anchor(uri, $2)
+ else
+ @out << escape_html($&)
+ end
+ end
+ when /\A\[\[\s*([^|]*?)\s*(\|\s*(.*?))?\s*\]\]/m
+ link = $1
+ if uri = make_explicit_link(link)
+ @out << make_explicit_anchor(uri, $3 || link)
+ else
+ @out << escape_html($&)
+ end
+ when /\A\{\{\{(.*?\}*)\}\}\}/
+ @out << '<tt>' << escape_html($1) << '</tt>'
+ when /\A\{\{\s*(.*?)\s*(\|\s*(.*?)\s*)?\}\}/
+ if uri = make_image_link($1)
+ @out << make_image(uri, $3)
+ else
+ @out << escape_html($&)
+ end
+ when /\A([:alpha:]|[:digit:])+/
+ @out << $&
+ when /\A\s+/
+ @out << ' ' if @out[-1,1] != ' '
+ when /\A\*\*/
+ toggle_tag 'strong', $&
+ when /\A\/\//
+ toggle_tag 'em', $&
+ when /\A\\\\/
+ @out << '<br/>'
+ else
+ if @extensions
+ case str
+ when /\A__/
+ toggle_tag 'u', $&
+ when /\A\-\-/
+ toggle_tag 'del', $&
+ when /\A\+\+/
+ toggle_tag 'ins', $&
+ when /\A\^\^/
+ toggle_tag 'sup', $&
+ when /\A\~\~/
+ toggle_tag 'sub', $&
+ when /\A\(R\)/i
+ @out << '&#174;'
+ when /\A\(C\)/i
+ @out << '&#169;'
+ when /\A~([^\s])/
+ @out << escape_html($1)
+ when /./
+ @out << escape_html($&)
+ end
+ else
+ case str
+ when /\A~([^\s])/
+ @out << escape_html($1)
+ when /./
+ @out << escape_html($&)
+ end
+ end
+ end
+ str = $'
+ end
+ end
+
+ def parse_table_row(str)
+ @out << '<tr>'
+ str.scan(/\s*\|(=)?\s*((\[\[.*?\]\]|\{\{.*?\}\}|[^|~]|~.)*)(?=\||$)/) do
+ if !$2.empty? || !$'.empty?
+ @out << ($1 ? '<th>' : '<td>')
+ parse_inline($2) if $2
+ end_tag while @stack.last != 'table'
+ @out << ($1 ? '</th>' : '</td>')
+ end
+ end
+ @out << '</tr>'
+ end
+
+ def make_nowikiblock(input)
+ input.gsub(/^ (?=\}\}\})/, '')
+ end
+
+ def ulol?(x); x == 'ul' || x == 'ol'; end
+
+ def parse_block(str)
+ until str.empty?
+ case str
+ when /\A\{\{\{\r?\n(.*?)\r?\n\}\}\}/m
+ end_paragraph
+ nowikiblock = make_nowikiblock($1)
+ @out << '<pre>' << escape_html(nowikiblock) << '</pre>'
+ when /\A\s*-{4,}\s*$/
+ end_paragraph
+ @out << '<hr/>'
+ when /\A\s*(={1,6})\s*(.*?)\s*=*\s*$(\r?\n)?/
+ end_paragraph
+ level = $1.size
+ @out << "<h#{level}>" << escape_html($2) << "</h#{level}>"
+ when /\A[ \t]*\|.*$(\r?\n)?/
+ if !@stack.include?('table')
+ end_paragraph
+ start_tag('table')
+ end
+ parse_table_row($&)
+ when /\A\s*$(\r?\n)?/
+ end_paragraph
+ when /\A(\s*([*#]+)\s*(.*?))$(\r?\n)?/
+ line, bullet, item = $1, $2, $3
+ tag = (bullet[0,1] == '*' ? 'ul' : 'ol')
+ if bullet[0,1] == '#' || bullet.size != 2 || @stack.find {|x| ulol?(x) }
+ count = @stack.select { |x| ulol?(x) }.size
+
+ while !@stack.empty? && count > bullet.size
+ count -= 1 if ulol?(@stack.last)
+ end_tag
+ end
+
+ end_tag while !@stack.empty? && @stack.last != 'li'
+
+ if @stack.last == 'li' && count == bullet.size
+ end_tag
+ if @stack.last != tag
+ end_tag
+ count -= 1
+ end
+ end
+
+ while count < bullet.size
+ start_tag tag
+ count += 1
+ start_tag 'li' if count < bullet.size
+ end
+
+ @p = true
+ start_tag('li')
+ parse_inline(item)
+ else
+ start_paragraph
+ parse_inline(line)
+ end
+ when /\A([ \t]*\S+.*?)$(\r?\n)?/
+ start_paragraph
+ parse_inline($1)
+ else
+ raise "Parse error at #{str[0,30].inspect}"
+ end
+ #p [$&, $']
+ str = $'
+ end
+ end_paragraph
+ @out
+ end
+ end
+end
20 lib/creole/template.rb
View
@@ -0,0 +1,20 @@
+require 'tilt'
+require 'creole'
+
+module Creole
+ class Template < Tilt::Template
+ def prepare
+ @creole = Creole::Parser.new(data,
+ :allowed_schemes => options[:allowed_schemes],
+ :extensions => options[:extensions],
+ :no_escape => options[:no_escape])
+ @output = nil
+ end
+
+ def evaluate(scope, locals, &block)
+ @output ||= @creole.to_html
+ end
+ end
+end
+
+Tilt.register 'creole', Creole::Template
3  lib/creole/version.rb
View
@@ -0,0 +1,3 @@
+module Creole
+ VERSION = '0.4.0'
+end
2  test/creole_test.rb → test/parser_test.rb
View
@@ -10,7 +10,7 @@ def tce(html, creole)
end
end
-describe Creole do
+describe Creole::Parser do
it 'should parse bold' do
# Creole1.0: Bold can be used inside paragraphs
tc "<p>This <strong>is</strong> bold</p>", "This **is** bold"
12 test/template_test.rb
View
@@ -0,0 +1,12 @@
+require 'creole/template'
+
+describe Creole::Template do
+ it 'should be registered for .creole files' do
+ Tilt.mappings['creole'].should.equal Creole::Template
+ end
+
+ it 'should prepare and evaluate templates on #render' do
+ template = Creole::Template.new { |t| '= Hello World!' }
+ 3.times { template.render.should.equal '<h1>Hello World!</h1>' }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.