public
Description: Yet Another Planet Refactoring
Homepage: http://intertwingly.net/blog/2007/12/19/Yet-Another-Planet-Refactoring
Clone URL: git://github.com/rubys/mars.git
Search Repo:
Sam Ruby (author)
Thu Apr 03 17:55:30 -0700 2008
commit  594cd30192668c6310b3236b978d5d4a6d706fb7
tree    fb2c98375a3f69e66dc22da049a8c7e26438d717
parent  775bc2a397c7812ae67b9979f288c3c835aab059
mars / planet / style.rb
100644 47 lines (40 sloc) 1.061 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Planet
  module Xslt
    begin
      # http://greg.rubyfr.net/pub/packages/ruby-xslt/files/README.html
      require 'xml/libxslt'
      @@processor = :libxslt
    rescue LoadError
      @@processor = :xsltproc
    end
 
    def Xslt.process stylesheet, doc
      if @@processor == :libxslt
 
        translate = XML::XSLT.new
        translate.xml = doc
        translate.xsl = stylesheet
        translate.serve
 
      else
 
        if stylesheet.index('<')
          require 'tempfile'
          file = Tempfile.open("style")
          begin
            file.write(stylesheet)
            file.close
            return process(file.path, doc)
          ensure
            file.unlink
          end
        end
 
        require 'open3'
        result = Open3.popen3("xsltproc #{stylesheet} -") do |pin, pout, perr|
          terr = Thread.new {STDERR.puts perr.readline until perr.eof?}
          tout = Thread.new {pout.read}
          doc.write pin
          pin.close
          terr.join
          tout.value
        end
 
      end
    end
 
  end
end