Sha256: 2fc4cdfabe2dbf716ce647ceff472a185dbe9b867b64c365ef2fd074bbcff2d3

Contents?: true

Size: 1.84 KB

Versions: 20

Compression:

Stored size: 1.84 KB

Contents

To write your own custom highlighter module, you just need to:

# inherit from @Syntax::Convertors::Abstract@
# implement the @convert@ method

You can use the @syntax/convertors/html.rb@ file as an example:

{{{lang=ruby,number=true,caption=syntax/convertors/html.rb
require '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

  end
end
}}}

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.

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
typo-3.99.0 vendor/syntax/doc/manual/parts/0006.txt
typo-3.99.3 vendor/syntax/doc/manual/parts/0006.txt
typo-3.99.1 vendor/syntax/doc/manual/parts/0006.txt
typo-3.99.2 vendor/syntax/doc/manual/parts/0006.txt
typo-4.0.2 vendor/syntax/doc/manual/parts/0006.txt
typo-4.0.1 vendor/syntax/doc/manual/parts/0006.txt
typo-3.99.4 vendor/syntax/doc/manual/parts/0006.txt
typo-4.0.0 vendor/syntax/doc/manual/parts/0006.txt
typo-4.0.3 vendor/syntax/doc/manual/parts/0006.txt
typo-4.1.1 vendor/syntax/doc/manual/parts/0006.txt
typo-5.0.2 vendor/syntax/doc/manual/parts/0006.txt
typo-5.0.1 vendor/syntax/doc/manual/parts/0006.txt
typo-4.1 vendor/syntax/doc/manual/parts/0006.txt
typo-5.0.3.98.1 vendor/syntax/doc/manual/parts/0006.txt
typo-5.0 vendor/syntax/doc/manual/parts/0006.txt
typo-5.0.3.98 vendor/syntax/doc/manual/parts/0006.txt
typo-5.1.2 vendor/syntax/doc/manual/parts/0006.txt
typo-5.1.1 vendor/syntax/doc/manual/parts/0006.txt
typo-5.1.3 vendor/syntax/doc/manual/parts/0006.txt
typo-5.1 vendor/syntax/doc/manual/parts/0006.txt