redcloth.rb

来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 1,131 行 · 第 1/3 页

RB
1,131
字号
                "#{ sta }<#{ ht }#{ atts }>#{ content }</#{ ht }>"            end        end    end    LINK_RE = /            ([\s\[{(]|[#{PUNCT}])?     # $pre            "                          # start            (#{C})                     # $atts            ([^"]+?)                   # $text            \s?            (?:\(([^)]+?)\)(?="))?     # $title            ":            (\S+?)                     # $url            (\/)?                      # $slash            ([^\w\/;]*?)               # $post            (?=<|\s|$)        /x     def inline_textile_link( text )         text.gsub!( LINK_RE ) do |m|            pre,atts,text,title,url,slash,post = $~[1..7]            url, url_title = check_refs( url )            title ||= url_title                        atts = pba( atts )            atts = " href=\"#{ url }#{ slash }\"#{ atts }"            atts << " title=\"#{ title }\"" if title            atts = shelve( atts ) if atts                        "#{ pre }<a#{ atts }>#{ text }</a>#{ post }"        end    end    MARKDOWN_REFLINK_RE = /            \[([^\[\]]+)\]      # $text            [ ]?                # opt. space            (?:\n[ ]*)?         # one optional newline followed by spaces            \[(.*?)\]           # $id        /x     def inline_markdown_reflink( text )         text.gsub!( MARKDOWN_REFLINK_RE ) do |m|            text, id = $~[1..2]            if id.empty?                url, title = check_refs( text )            else                url, title = check_refs( id )            end                        atts = " href=\"#{ url }\""            atts << " title=\"#{ title }\"" if title            atts = shelve( atts )                        "<a#{ atts }>#{ text }</a>"        end    end    MARKDOWN_LINK_RE = /            \[([^\[\]]+)\]      # $text            \(                  # open paren            [ \t]*              # opt space            <?(.+?)>?           # $href            [ \t]*              # opt space            (?:                 # whole title            (['"])              # $quote            (.*?)               # $title            \3                  # matching quote            )?                  # title is optional            \)        /x     def inline_markdown_link( text )         text.gsub!( MARKDOWN_LINK_RE ) do |m|            text, url, quote, title = $~[1..4]            atts = " href=\"#{ url }\""            atts << " title=\"#{ title }\"" if title            atts = shelve( atts )                        "<a#{ atts }>#{ text }</a>"        end    end    TEXTILE_REFS_RE =  /(^ *)\[([^\n]+?)\](#{HYPERLINK})(?=\s|$)/    MARKDOWN_REFS_RE = /(^ *)\[([^\n]+?)\]:\s+<?(#{HYPERLINK})>?(?:\s+"((?:[^"]|\\")+)")?(?=\s|$)/m    def refs( text )        @rules.each do |rule_name|            method( rule_name ).call( text ) if rule_name.to_s.match /^refs_/        end    end    def refs_textile( text )         text.gsub!( TEXTILE_REFS_RE ) do |m|            flag, url = $~[2..3]            @urlrefs[flag.downcase] = [url, nil]            nil        end    end        def refs_markdown( text )        text.gsub!( MARKDOWN_REFS_RE ) do |m|            flag, url = $~[2..3]            title = $~[6]            @urlrefs[flag.downcase] = [url, title]            nil        end    end    def check_refs( text )         ret = @urlrefs[text.downcase] if text        ret || [text, nil]    end    IMAGE_RE = /            (<p>|.|^)            # start of line?            \!                   # opening            (\<|\=|\>)?          # optional alignment atts            (#{C})               # optional style,class atts            (?:\. )?             # optional dot-space            ([^\s(!]+?)          # presume this is the src            \s?                  # optional space            (?:\(((?:[^\(\)]|\([^\)]+\))+?)\))?   # optional title            \!                   # closing            (?::#{ HYPERLINK })? # optional href        /x     def inline_textile_image( text )         text.gsub!( IMAGE_RE )  do |m|            stln,algn,atts,url,title,href,href_a1,href_a2 = $~[1..8]            atts = pba( atts )            atts = " src=\"#{ url }\"#{ atts }"            atts << " title=\"#{ title }\"" if title            atts << " alt=\"#{ title }\""             # size = @getimagesize($url);            # if($size) $atts.= " $size[3]";            href, alt_title = check_refs( href ) if href            url, url_title = check_refs( url )            out = ''            out << "<a#{ shelve( " href=\"#{ href }\"" ) }>" if href            out << "<img#{ shelve( atts ) } />"            out << "</a>#{ href_a1 }#{ href_a2 }" if href                        if algn                 algn = h_align( algn )                if stln == "<p>"                    out = "<p style=\"float:#{ algn }\">#{ out }"                else                    out = "#{ stln }<div style=\"float:#{ algn }\">#{ out }</div>"                end            else                out = stln + out            end            out        end    end    def shelve( val )         @shelf << val        " :redsh##{ @shelf.length }:"    end        def retrieve( text )         @shelf.each_with_index do |r, i|            text.gsub!( " :redsh##{ i + 1 }:", r )        end    end    def incoming_entities( text )         ## turn any incoming ampersands into a dummy character for now.        ## This uses a negative lookahead for alphanumerics followed by a semicolon,        ## implying an incoming html entity, to be skipped        text.gsub!( /&(?![#a-z0-9]+;)/i, "x%x%" )    end    def no_textile( text )         text.gsub!( /(^|\s)==([^=]+.*?)==(\s|$)?/,            '\1<notextile>\2</notextile>\3' )        text.gsub!( /^ *==([^=]+.*?)==/m,            '\1<notextile>\2</notextile>\3' )    end    def clean_white_space( text )         # normalize line breaks        text.gsub!( /\r\n/, "\n" )        text.gsub!( /\r/, "\n" )        text.gsub!( /\t/, '    ' )        text.gsub!( /^ +$/, '' )        text.gsub!( /\n{3,}/, "\n\n" )        text.gsub!( /"$/, "\" " )        # if entire document is indented, flush        # to the left side        flush_left text    end    def flush_left( text )        indt = 0        if text =~ /^ /            while text !~ /^ {#{indt}}\S/                indt += 1            end unless text.empty?            if indt.nonzero?                text.gsub!( /^ {#{indt}}/, '' )            end        end    end    def footnote_ref( text )         text.gsub!( /\b\[([0-9]+?)\](\s)?/,            '<sup><a href="#fn\1">\1</a></sup>\2' )    end        OFFTAGS = /(code|pre|kbd|notextile)/    OFFTAG_MATCH = /(?:(<\/#{ OFFTAGS }>)|(<#{ OFFTAGS }[^>]*>))(.*?)(?=<\/?#{ OFFTAGS }|\Z)/mi    OFFTAG_OPEN = /<#{ OFFTAGS }/    OFFTAG_CLOSE = /<\/?#{ OFFTAGS }/    HASTAG_MATCH = /(<\/?\w[^\n]*?>)/m    ALLTAG_MATCH = /(<\/?\w[^\n]*?>)|.*?(?=<\/?\w[^\n]*?>|$)/m    def glyphs_textile( text, level = 0 )        if text !~ HASTAG_MATCH            pgl text            footnote_ref text        else            codepre = 0            text.gsub!( ALLTAG_MATCH ) do |line|                ## matches are off if we're between <code>, <pre> etc.                if $1                    if line =~ OFFTAG_OPEN                        codepre += 1                    elsif line =~ OFFTAG_CLOSE                        codepre -= 1                        codepre = 0 if codepre < 0                    end                 elsif codepre.zero?                    glyphs_textile( line, level + 1 )                else                    htmlesc( line, :NoQuotes )                end                # p [level, codepre, line]                line            end        end    end    def rip_offtags( text )        if text =~ /<.*>/            ## strip and encode <pre> content            codepre, used_offtags = 0, {}            text.gsub!( OFFTAG_MATCH ) do |line|                if $3                    offtag, aftertag = $4, $5                    codepre += 1                    used_offtags[offtag] = true                    if codepre - used_offtags.length > 0                        htmlesc( line, :NoQuotes ) unless used_offtags['notextile']                        @pre_list.last << line                        line = ""                    else                        htmlesc( aftertag, :NoQuotes ) if aftertag and not used_offtags['notextile']                        line = "<redpre##{ @pre_list.length }>"                        @pre_list << "#{ $3 }#{ aftertag }"                    end                elsif $1 and codepre > 0                    if codepre - used_offtags.length > 0                        htmlesc( line, :NoQuotes ) unless used_offtags['notextile']                        @pre_list.last << line                        line = ""                    end                    codepre -= 1 unless codepre.zero?                    used_offtags = {} if codepre.zero?                end                 line            end        end        text    end    def smooth_offtags( text )        unless @pre_list.empty?            ## replace <pre> content            text.gsub!( /<redpre#(\d+)>/ ) { @pre_list[$1.to_i] }        end    end    def inline( text )         [/^inline_/, /^glyphs_/].each do |meth_re|            @rules.each do |rule_name|                method( rule_name ).call( text ) if rule_name.to_s.match( meth_re )            end        end    end    def h_align( text )         H_ALGN_VALS[text]    end    def v_align( text )         V_ALGN_VALS[text]    end    def textile_popup_help( name, windowW, windowH )        ' <a target="_blank" href="http://hobix.com/textile/#' + helpvar + '" onclick="window.open(this.href, \'popupwindow\', \'width=' + windowW + ',height=' + windowH + ',scrollbars,resizable\'); return false;">' + name + '</a><br />'    end    # HTML cleansing stuff    BASIC_TAGS = {        'a' => ['href', 'title'],        'img' => ['src', 'alt', 'title'],        'br' => [],        'i' => nil,        'u' => nil,         'b' => nil,        'pre' => nil,        'kbd' => nil,        'code' => ['lang'],        'cite' => nil,        'strong' => nil,        'em' => nil,        'ins' => nil,        'sup' => nil,        'sub' => nil,        'del' => nil,        'table' => nil,        'tr' => nil,        'td' => ['colspan', 'rowspan'],        'th' => nil,        'ol' => nil,        'ul' => nil,        'li' => nil,        'p' => nil,        'h1' => nil,        'h2' => nil,        'h3' => nil,        'h4' => nil,        'h5' => nil,        'h6' => nil,         'blockquote' => ['cite']    }    def clean_html( text, tags = BASIC_TAGS )        text.gsub!( /<!\[CDATA\[/, '' )        text.gsub!( /<(\/*)(\w+)([^>]*)>/ ) do            raw = $~            tag = raw[2].downcase            if tags.has_key? tag                pcs = [tag]                tags[tag].each do |prop|                    ['"', "'", ''].each do |q|                        q2 = ( q != '' ? q : '\s' )                        if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i                            attrv = $1                            next if prop == 'src' and attrv =~ %r{^(?!http)\w+:}                            pcs << "#{prop}=\"#{$1.gsub('"', '\\"')}\""                            break                        end                    end                end if tags[tag]                "<#{raw[1]}#{pcs.join " "}>"            else                " "            end        end    endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?