to_wordpress_html ruby_file_path: Code highlighter for wordpress

Started tech blogging and found it is necessary to have a nice code highlighter, and I was actually looking out for a blogging tool that would appear very simpler, nice, less time to post, and still have good control over minor aspects.  Trying to achieve all this using wordpress, and found a good friend coderay (when looked inside html from m.onkey.org).

Now.. to have any code snippet here on the blog I just have to say “to_wordpress_html” as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def to_wordpress_html file_path
  return unless file_path

  require 'coderay'
  html = CodeRay.scan_file( file_path, :ruby).div(:line_numbers => :table)

  html = "<div style=\"overflow:auto;margin-top: 5px;\">#{html}</div>"

  html.sub!('class="code"><pre>', ' class="code"><pre style="padding-left: 2px;margin-bottom: 0;padding-bottom: 0;"> ')
  html.sub!('<pre><a href="#n1" ',' <pre style="border-right: 1px solid gray;padding-left: 2px;padding-right: 2px;margin-bottom: 0;padding-bottom: 0;"><a href="#n1" ')

  html.sub!('class="CodeRay"',      ' class="CodeRay" style="margin-bottom:0;" ')
  html.sub!('class="line-numbers"', ' class="line-numbers" style="padding:0;" ')
  html.sub!('class="code"',         ' class="code" style="padding:0;" ')
end  

if not ARGV[0]
  puts "Require ruby file."
else
  File.open('code.html','w'){ |file| file.write to_wordpress_html(ARGV[0]) }
end

# ruby coderay_for_wordpress.rb coderay_for_wordpress.rb

May be I will later add support for lines from and to, and more…

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s