module MiniSyntax
module Highlighter
module PHP
def self.highlight(code)
keywords = %w(abstract and array as break case catch cfunction class clone const continue declare default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends final for foreach function global goto if implements interface instanceof namespace new old_function or private protected public static switch throw try use var while xor)
keywords += %w(die echo empty exit eval include include_once isset list require require_once return print unset )
code.gsub! /\b(#{keywords.join('|')})\b/, "\\1"
code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "\\1"
code.gsub! /\$[a-zA-Z0-9_]+/, "\\0"
code.gsub! /("(.*?)"|'.*?')/ do |q|
q.gsub! %r(<(b|i|em|var)>(.*?)\1>), "\\2"
if q[0..5] == '"'
q.gsub! /(\$[a-zA-Z0-9_]+)(\[(.+?)\])?/ do
hash = $3['$'] ? %Q([#{$3}]) : $2 if $2
%Q(#{$1}#{hash}
)
end
end
%Q(#{q}
)
end
code.gsub! %r((//.*?)$) do |comment|
if comment.gsub(%r((.*?)
), "\\1") =~ %r()
comment
else
comment.gsub! %r(?(b|i|em|var|code)>), ""
%Q(#{comment})
end
end
code
end
end
end
end
MiniSyntax.register(:php, MiniSyntax::Highlighter::PHP)