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
mars / planet / xsltformatter.rb
100755 48 lines (39 sloc) 1.092 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
48
require 'planet/formatter'
 
  class XsltFormatter < PlanetFormatter
    begin
      # http://greg.rubyfr.net/pub/packages/ruby-xslt/files/README.html
      require 'xml/libxslt'
      @@processor = :libxslt
    rescue LoadError
      @@processor = :xsltproc
    end
 
    def 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