redcloth.rb
来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 1,131 行 · 第 1/3 页
RB
1,131 行
# vim:ts=4:sw=4:# = RedCloth - Textile and Markdown Hybrid for Ruby## Homepage:: http://whytheluckystiff.net/ruby/redcloth/# Author:: why the lucky stiff (http://whytheluckystiff.net/)# Copyright:: (cc) 2004 why the lucky stiff (and his puppet organizations.)# License:: BSD## (see http://hobix.com/textile/ for a Textile Reference.)## Based on (and also inspired by) both:## PyTextile: http://diveintomark.org/projects/textile/textile.py.txt# Textism for PHP: http://www.textism.com/tools/textile/### = RedCloth## RedCloth is a Ruby library for converting Textile and/or Markdown# into HTML. You can use either format, intermingled or separately.# You can also extend RedCloth to honor your own custom text stylings.## RedCloth users are encouraged to use Textile if they are generating# HTML and to use Markdown if others will be viewing the plain text.## == What is Textile?## Textile is a simple formatting style for text# documents, loosely based on some HTML conventions.## == Sample Textile Text## h2. This is a title## h3. This is a subhead## This is a bit of paragraph.## bq. This is a blockquote.## = Writing Textile## A Textile document consists of paragraphs. Paragraphs# can be specially formatted by adding a small instruction# to the beginning of the paragraph.## h[n]. Header of size [n].# bq. Blockquote.# # Numeric list.# * Bulleted list.## == Quick Phrase Modifiers## Quick phrase modifiers are also included, to allow formatting# of small portions of text within a paragraph.## \_emphasis\_# \_\_italicized\_\_# \*strong\*# \*\*bold\*\*# ??citation??# -deleted text-# +inserted text+# ^superscript^# ~subscript~# @code@# %(classname)span%## ==notextile== (leave text alone)## == Links## To make a hypertext link, put the link text in "quotation # marks" followed immediately by a colon and the URL of the link.# # Optional: text in (parentheses) following the link text, # but before the closing quotation mark, will become a Title # attribute for the link, visible as a tool tip when a cursor is above it.# # Example:## "This is a link (This is a title) ":http://www.textism.com# # Will become:# # <a href="http://www.textism.com" title="This is a title">This is a link</a>## == Images## To insert an image, put the URL for the image inside exclamation marks.## Optional: text that immediately follows the URL in (parentheses) will # be used as the Alt text for the image. Images on the web should always # have descriptive Alt text for the benefit of readers using non-graphical # browsers.## Optional: place a colon followed by a URL immediately after the # closing ! to make the image into a link.# # Example:## !http://www.textism.com/common/textist.gif(Textist)!## Will become:## <img src="http://www.textism.com/common/textist.gif" alt="Textist" />## With a link:## !/common/textist.gif(Textist)!:http://textism.com## Will become:## <a href="http://textism.com"><img src="/common/textist.gif" alt="Textist" /></a>## == Defining Acronyms## HTML allows authors to define acronyms via the tag. The definition appears as a # tool tip when a cursor hovers over the acronym. A crucial aid to clear writing, # this should be used at least once for each acronym in documents where they appear.## To quickly define an acronym in Textile, place the full text in (parentheses) # immediately following the acronym.# # Example:## ACLU(American Civil Liberties Union)## Will become:## <acronym title="American Civil Liberties Union">ACLU</acronym>## == Adding Tables## In Textile, simple tables can be added by seperating each column by# a pipe.## |a|simple|table|row|# |And|Another|table|row|## Attributes are defined by style definitions in parentheses.## table(border:1px solid black).# (background:#ddd;color:red). |{}| | | |## == Using RedCloth# # RedCloth is simply an extension of the String class, which can handle# Textile formatting. Use it like a String and output HTML with its# RedCloth#to_html method.## doc = RedCloth.new "## h2. Test document## Just a simple test."## puts doc.to_html## By default, RedCloth uses both Textile and Markdown formatting, with# Textile formatting taking precedence. If you want to turn off Markdown# formatting, to boost speed and limit the processor:## class RedCloth::Textile.new( str )class RedCloth < String VERSION = '3.0.4' DEFAULT_RULES = [:textile, :markdown] # # Two accessor for setting security restrictions. # # This is a nice thing if you're using RedCloth for # formatting in public places (e.g. Wikis) where you # don't want users to abuse HTML for bad things. # # If +:filter_html+ is set, HTML which wasn't # created by the Textile processor will be escaped. # # If +:filter_styles+ is set, it will also disable # the style markup specifier. ('{color: red}') # attr_accessor :filter_html, :filter_styles # # Accessor for toggling hard breaks. # # If +:hard_breaks+ is set, single newlines will # be converted to HTML break tags. This is the # default behavior for traditional RedCloth. # attr_accessor :hard_breaks # Accessor for toggling lite mode. # # In lite mode, block-level rules are ignored. This means # that tables, paragraphs, lists, and such aren't available. # Only the inline markup for bold, italics, entities and so on. # # r = RedCloth.new( "And then? She *fell*!", [:lite_mode] ) # r.to_html # #=> "And then? She <strong>fell</strong>!" # attr_accessor :lite_mode # # Accessor for toggling span caps. # # Textile places `span' tags around capitalized # words by default, but this wreaks havoc on Wikis. # If +:no_span_caps+ is set, this will be # suppressed. # attr_accessor :no_span_caps # # Establishes the markup predence. Available rules include: # # == Textile Rules # # The following textile rules can be set individually. Or add the complete # set of rules with the single :textile rule, which supplies the rule set in # the following precedence: # # refs_textile:: Textile references (i.e. [hobix]http://hobix.com/) # block_textile_table:: Textile table block structures # block_textile_lists:: Textile list structures # block_textile_prefix:: Textile blocks with prefixes (i.e. bq., h2., etc.) # inline_textile_image:: Textile inline images # inline_textile_link:: Textile inline links # inline_textile_span:: Textile inline spans # glyphs_textile:: Textile entities (such as em-dashes and smart quotes) # # == Markdown # # refs_markdown:: Markdown references (for example: [hobix]: http://hobix.com/) # block_markdown_setext:: Markdown setext headers # block_markdown_atx:: Markdown atx headers # block_markdown_rule:: Markdown horizontal rules # block_markdown_bq:: Markdown blockquotes # block_markdown_lists:: Markdown lists # inline_markdown_link:: Markdown links attr_accessor :rules # Returns a new RedCloth object, based on _string_ and # enforcing all the included _restrictions_. # # r = RedCloth.new( "h1. A <b>bold</b> man", [:filter_html] ) # r.to_html # #=>"<h1>A <b>bold</b> man</h1>" # def initialize( string, restrictions = [] ) restrictions.each { |r| method( "#{ r }=" ).call( true ) } super( string ) end # # Generates HTML from the Textile contents. # # r = RedCloth.new( "And then? She *fell*!" ) # r.to_html( true ) # #=>"And then? She <strong>fell</strong>!" # def to_html( *rules ) rules = DEFAULT_RULES if rules.empty? # make our working copy text = self.dup @urlrefs = {} @shelf = [] textile_rules = [:refs_textile, :block_textile_table, :block_textile_lists, :block_textile_prefix, :inline_textile_image, :inline_textile_link, :inline_textile_code, :inline_textile_span, :glyphs_textile] markdown_rules = [:refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule, :block_markdown_bq, :block_markdown_lists, :inline_markdown_reflink, :inline_markdown_link] @rules = rules.collect do |rule| case rule when :markdown markdown_rules when :textile textile_rules else rule end end.flatten # standard clean up incoming_entities text clean_white_space text # start processor @pre_list = [] rip_offtags text no_textile text hard_break text unless @lite_mode refs text blocks text end inline text smooth_offtags text retrieve text text.gsub!( /<\/?notextile>/, '' ) text.gsub!( /x%x%/, '&' ) clean_html text if filter_html text.strip! text end ####### private ####### # # Mapping of 8-bit ASCII codes to HTML numerical entity equivalents. # (from PyTextile) # TEXTILE_TAGS = [[128, 8364], [129, 0], [130, 8218], [131, 402], [132, 8222], [133, 8230], [134, 8224], [135, 8225], [136, 710], [137, 8240], [138, 352], [139, 8249], [140, 338], [141, 0], [142, 0], [143, 0], [144, 0], [145, 8216], [146, 8217], [147, 8220], [148, 8221], [149, 8226], [150, 8211], [151, 8212], [152, 732], [153, 8482], [154, 353], [155, 8250], [156, 339], [157, 0], [158, 0], [159, 376]]. collect! do |a, b| [a.chr, ( b.zero? and "" or "&#{ b };" )] end # # Regular expressions to convert to HTML. # A_HLGN = /(?:(?:<>|<|>|\=|[()]+)+)/ A_VLGN = /[\-^~]/ C_CLAS = '(?:\([^)]+\))' C_LNGE = '(?:\[[^\]]+\])' C_STYL = '(?:\{[^}]+\})' S_CSPN = '(?:\\\\\d+)' S_RSPN = '(?:/\d+)' A = "(?:#{A_HLGN}?#{A_VLGN}?|#{A_VLGN}?#{A_HLGN}?)" S = "(?:#{S_CSPN}?#{S_RSPN}|#{S_RSPN}?#{S_CSPN}?)" C = "(?:#{C_CLAS}?#{C_STYL}?#{C_LNGE}?|#{C_STYL}?#{C_LNGE}?#{C_CLAS}?|#{C_LNGE}?#{C_STYL}?#{C_CLAS}?)" # PUNCT = Regexp::quote( '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' ) PUNCT = Regexp::quote( '!"#$%&\'*+,-./:;=?@\\^_`|~' ) PUNCT_NOQ = Regexp::quote( '!"#$&\',./:;=?@\\`|' ) PUNCT_Q = Regexp::quote( '*-_+^~%' ) HYPERLINK = '(\S+?)([^\w\s/;=\?]*?)(?=\s|<|$)' # Text markup tags, don't conflict with block tags SIMPLE_HTML_TAGS = [ 'tt', 'b', 'i', 'big', 'small', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'a', 'img', 'br', 'br', 'map', 'q', 'sub', 'sup', 'span', 'bdo' ] QTAGS = [ ['**', 'b'], ['*', 'strong'], ['??', 'cite', :limit], ['-', 'del', :limit], ['__', 'i'], ['_', 'em', :limit], ['%', 'span', :limit], ['+', 'ins', :limit], ['^', 'sup'], ['~', 'sub'] ] QTAGS.collect! do |rc, ht, rtype| rcq = Regexp::quote rc re = case rtype when :limit
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?