# This file defines the String#to_xhtml method which # is invoked to transform plain text into XHTML. # # This particular implementation features the Markdown # formatting system via Maruku, syntax coloring via CodeRay, # and smart source code sizing (block versus inline display). #-- # Copyright protects this work. # See LICENSE file for details. #++ require 'cgi' require 'digest/sha1' begin require 'rubygems' gem 'maruku', '~> 0.5' gem 'coderay', '>= 0.8' rescue LoadError end require 'coderay' require 'maruku' class String ## # The content of these XHTML tags will be preserved while # they are being processed by Textile. By doing this, we # avoid unwanted Textile transformations, such as quotation # marks becoming curly ( ), in source code. # PROTECTED_TAGS = { :pre => :block, # tag => is it a block or inline element? :code => :inline, :tt => :inline } ## # The content of these XHTML tags will be preserved # *verbatim* throughout the text-to-XHTML conversion process. # VERBATIM_TAGS = { :noformat => :block # tag => is it a block or inline element? } ## # Transforms this string into XHTML. # def to_xhtml with_protected_tags(self, VERBATIM_TAGS, true) do |text| html = with_protected_tags(text, PROTECTED_TAGS, false) do |s| s.thru_maruku end # Markdown's "code spans" should really be "pre spans" while html.gsub! %r{(
)(.*?)
(
)}m, '\1\2\3'
end
# allow "code spans" annotated with Maruku's IAL (Inline
# Attribute List) Markdown extension to be syntax colored
while html.gsub! %r{]+>(]+>.*?
)
}m, '\1'
end
# allow user to type blocks on single lines # without affecting the display of their content html.gsub! %r{()[ \t]*\r?\n|\r?\n[ \t]*()}, '\1\2' # ensure tables have a border: this *greatly* improves # readability in text-based web browsers like w3m and lynx html.gsub! %r/