Module: SemiStatic::Pygmentize

Defined in:
lib/semi-static/pygmentize.rb

Constant Summary collapse

LEXER_FORMAT =

Format of a valid lexer name

/^[a-z]+$/i
@@enabled =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enabledObject

:nodoc:



33
34
35
# File 'lib/semi-static/pygmentize.rb', line 33

def self.enabled #:nodoc:
    @@enabled
end

.enabled=(value) ⇒ Object

:nodoc:



36
37
38
# File 'lib/semi-static/pygmentize.rb', line 36

def self.enabled=(value) #:nodoc:
    @@enabled = value
end

.pygmentize(code, lang) ⇒ Object

Highlight the given code with the given lexer.

code: The code to highlight. lang: The lexer to use.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/semi-static/pygmentize.rb', line 16

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

Instance Method Details

#pygmentize(code, lang) ⇒ Object

:nodoc:



7
8
9
# File 'lib/semi-static/pygmentize.rb', line 7

def pygmentize(code, lang) #:nodoc:
    Pygmentize.pygmentize code, lang
end