0006.txt

来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· 文本 代码 · 共 62 行

TXT
62
字号
To write your own custom highlighter module, you just need to:# inherit from @Syntax::Convertors::Abstract@# implement the @convert@ methodYou can use the @syntax/convertors/html.rb@ file as an example:{{{lang=ruby,number=true,caption=syntax/convertors/html.rbrequire 'syntax/convertors/abstract'module Syntax  module Convertors # A simple class for converting a text into HTML.    class HTML < Abstract      # Converts the given text to HTML, using spans to represent token groups      # of any type but <tt>:normal</tt> (which is always unhighlighted). If      # +pre+ is +true+, the html is automatically wrapped in pre tags.      def convert( text, pre=true )        html = ""        html << "<pre>" if pre        regions = []        @tokenizer.tokenize( text ) do |tok|          value = html_escape(tok)   case tok.instruction            when :region_close then              regions.pop              html << "</span>"            when :region_open then              regions.push tok.group              html << "<span class=\"#{tok.group}\">#{value}"            else         if tok.group == ( regions.last || :normal )                html << value else                html << "<span class=\"#{tok.group}\">#{value}</span>"              end          end        end        html << "</span>" while regions.pop        html << "</pre>" if pre        html      end      private        # Replaces some characters with their corresponding HTML entities.        def html_escape( string )          string.gsub( /&/, "&amp;" ).                 gsub( /</, "&lt;" ).                 gsub( />/, "&gt;" ).                 gsub( /"/, "&quot;" )        end    end  endend}}}Within the @#convert@ method, you will automatically have access to the @tokenizer@ instance variable--instantiated for you by the framework. The rest is up to you.

⌨️ 快捷键说明

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