Sha256: 5b33ec3b3ca9720b31e9f6ce6c21465b35628300f6e6732d96c1350831321bcc

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module SemiStatic
    module Pygmentize
        ##
        # Format of a valid lexer name
        LEXER_FORMAT = /^[a-z]+$/i
        
        def pygmentize(code, lang) #:nodoc:
            Pygmentize.pygmentize code, lang
        end
        
        ##
        # Highlight the given code with the given lexer.
        #
        # +code+: The code to highlight.
        # +lang+: The lexer to use.
        def self.pygmentize(code, lang)
            unless lang =~ LEXER_FORMAT
                raise ArgumentError, "invalid lexer: #{lang}"
            end
        
            Tempfile.open('semistatic-pygmentize') do |temp|
                temp.write code
                temp.close
            
                cmd = "pygmentize -f html -l #{lang} #{temp.path}"
                IO.popen(cmd) do |proc|
                    return proc.read
                end
            end
        end
        
        @@enabled = false
        def self.enabled #:nodoc:
            @@enabled
        end
        def self.enabled=(value) #:nodoc:
            @@enabled = value
        end
    end
end

module MaRuKu #:nodoc:
    module Out #:nodoc:
        module HTML #:nodoc:
            alias_method :to_html_code_without_pygments, :to_html_code
            def to_html_code_with_pygments
                if SemiStatic::Pygmentize.enabled
                    source = self.raw_code
                    lang = self.attributes[:lang] || 'text'
                    html = SemiStatic::Pygmentize.pygmentize source, lang
                    doc = Document.new html, :respect_whitespace => :all

                    add_ws doc.root
                else
                    to_html_code_without_pygments
                end
            end
            alias_method :to_html_code, :to_html_code_with_pygments
        end
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zzot-semi-static-0.0.3 lib/semi-static/pygmentize.rb
zzot-semi-static-0.0.4 lib/semi-static/pygmentize.rb
zzot-semi-static-0.0.5 lib/semi-static/pygmentize.rb
zzot-semi-static-0.0.6 lib/semi-static/pygmentize.rb