The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Thursday, 29 December 2011

Convert ruby code to HTML to paste it into your blog

I use the following script to read ruby code and output the HTML to the stdin of the shell:

require 'syntax/convertors/html'

code = File.read('code.rb')

convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert( code )

puts code_html


Put you ruby code in 'code.rb' and run

> ruby script_name.rb

Then cut and paste the content. You have to add the following CSS if you want to see your code properly formatted:

pre {
    background-color: #f1f1f3;
    color: #112;
    padding: 10px;
    font-size: 1.1em;
    overflow: auto;
    margin: 4px 0px;
          width: 95%;
}



/* Syntax highlighting */
pre .normal {}
pre .comment { color: #005; font-style: italic; }
pre .keyword { color: #A00; font-weight: bold; }
pre .method { color: #077; }
pre .class { color: #074; }
pre .module { color: #050; }
pre .punct { color: #447; font-weight: bold; }
pre .symbol { color: #099; }
pre .string { color: #944; background: #FFE; }
pre .char { color: #F07; }
pre .ident { color: #004; }
pre .constant { color: #07F; }
pre .regex { color: #B66; background: #FEF; }
pre .number { color: #F99; }
pre .attribute { color: #5bb; }
pre .global { color: #7FB; }
pre .expr { color: #227; }
pre .escape { color: #277; }


The style of the CSS code above has been created by changing the type of highlighter:


convertor = Syntax::Convertors::HTML.for_syntax "css"

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home