Friday, June 20, 2008

Ruby Pastie.rb syntaxhighlight

Pastie.rbを修正して、一応Cobaltはプリントできるようになった。ただ他のテーマについてはまだ未対応。
(課題)
  • 各テーマのbackground-colorとcolorプロパティがUv側から取得できていない。
  • 替わりに、ハッシュで設定ファイルを作ってこれを読むようにしている。
  • Cobalt以外のテーマのこれらのプロパティ値がいまのところ不明。
Pastie.rb

   1  #!/usr/bin/env ruby -wKU

   2  # encoding: utf-8

   3  require "rubygems"

   4  require "uv"

   5  

   6  theme_colors = {"active4d" => [], "all_hallows_eve" => [],

   7                  "amy" => [], "blackboard" => [],

   8                  "brilliance_black" => [], "brilliance_dull" => [],

   9                  "cobalt" => ['#002444','#e6e1dc'], "dawn" => [],

  10                  "eiffel" => [], "espresso_libre" => [],

  11                  "idle" => [], "iplastic" => [],

  12                  "lazy" => [], "mac_classic" => [],

  13                  "magicwb_amiga" => [], "pastels_on_dark" => [],

  14                  "slush_poppies" => [], "spacecadet" => [],

  15                  "sunburst" => [], "twilight" => [],

  16                  "zenburnesque" => []}

  17  

  18  

  19  input = ARGF

  20  theme = "cobalt"

  21  syntax = "ruby"

  22  numbering = true

  23  

  24  html = Uv.parse( input, "xhtml", syntax, numbering, theme )

  25  

  26  css_file = File.join(Uv.path, 'render', 'xhtml', 'files', 'css', "#{theme}.css")

  27  

  28  styles = {}

  29  css = open(css_file, 'r').read

  30  css.gsub(/pre¥.#{theme}¥s+¥.(¥w+)¥s+¥{¥s*(¥w+.*¥s*¥w*.*)¥s*¥}/) do

  31    styles[$1] = $2.gsub(/¥s/, "")

  32  end

  33  styles["#{theme}"] = %Q{background-color:#{theme_colors[theme][0]};color:#{theme_colors[theme][1]}}

  34  

  35  styles.each do |key, value|

  36    html.gsub!(/class="#{key}"/i, %Q{style="#{value}"})

  37  end

  38  puts html

No comments: