Sha256: f42ed72e86e76c5156e1415055b1eb3c1287284b228a13d601b40fc7cee16fe6

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module SemiStatic
    module Pygmentize
        LEXER_FORMAT = /^[a-z]+$/i
        
        def pygmentize(code, lang)
            Pygmentize.pygmentize code, lang
        end

        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
            @@enabled
        end
        def self.enabled=(value)
            @@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

2 entries across 2 versions & 2 rubygems

Version Path
zzot-semi-static-0.0.2 lib/semi-static/pygmentize.rb
zzot-zzot-semi-static-0.0.1 lib/semi-static/pygmentize.rb