Syntax highlighting de Ruby en mi blog
Posted: June 11th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming | Tags: css, html, Open Source, Programming, Ruby | No Comments »Tomando como base lo que dice en este post, hice un script en ruby que genera html con clases que se pueden poner coloritos con css:
#!/usr/bin/env ruby require 'rubygems' require 'syntax/convertors/html' class Syntax::Convertors::HTML def convert( text, klass="" ) html = "<pre class=\"#{klass}\">" 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>" html end end convertor = Syntax::Convertors::HTML.for_syntax "ruby" puts convertor.convert( $stdin.read, "ruby" )
En el script monkeypatchié un toque para que agregue la clase “ruby” al tag pre que engloba todo el código y aparte estoy usando estos estilos:
<style>
pre.ruby {
background-color: #ffffcc;
color: #000000;
padding: 10px;
font-size: 1.1em;
overflow: auto;
margin: 4px 0px;
width: 95%;
border: thin dashed;
}
.ruby .normal {}
.ruby .comment { color: #005; font-style: italic; }
.ruby .keyword { color: #A00; font-weight: bold; }
.ruby .method { color: #077; }
.ruby .class { color: #074; }
.ruby .module { color: #050; }
.ruby .punct { color: #447; font-weight: bold; }
.ruby .symbol { color: #099; }
.ruby .string { color: #944; }
.ruby .char { color: #F07; }
.ruby .ident { color: #004; }
.ruby .constant { color: #07F; }
.ruby .regex { color: #B66; }
.ruby .number { color: #D55; }
.ruby .attribute { color: #377; }
.ruby .global { color: #3B7; }
.ruby .expr { color: #227; }
</style>
Una cosa más, si estás viendo este post en otro lugar que no sea aurelianito.blogspot.com no vas a ver el resaltado de sintaxis (ya que no va a tener los estilos).
Hasta la próxima,
Aureliano
(Via aurelianito.) Original Link: Syntax highlighting de Ruby en mi blog