| 1 | # PrettyXML |
|---|
| 2 | # Thomas Sawyer (transami) |
|---|
| 3 | # May 2002
|
|---|
| 4 |
|
|---|
| 5 | require 'rexml/document'
|
|---|
| 6 |
|
|---|
| 7 | module PrettyXML
|
|---|
| 8 | |
|---|
| 9 | # Returns the pretty result formatted into html |
|---|
| 10 | def PrettyXML.pretty_in_html(source) |
|---|
| 11 | out = PrettyXML.pretty(source) |
|---|
| 12 | out.gsub!(/&/n, '&') |
|---|
| 13 | out.gsub!(/>/n, '>') |
|---|
| 14 | out.gsub!(/</n, '<') |
|---|
| 15 | out.gsub!(/\n/,'<br>') |
|---|
| 16 | out.gsub!(/\t/,' ') |
|---|
| 17 | out.gsub!(/\"/n, '"') |
|---|
| 18 | return out |
|---|
| 19 | end |
|---|
| 20 | |
|---|
| 21 | # Return source all pretty'd up. |
|---|
| 22 | # tab specifies indention |
|---|
| 23 | # tab = 0 for "\t" or a positive integer for ' ' * tab spaces |
|---|
| 24 | def PrettyXML.pretty(source, tab=0) |
|---|
| 25 | @pretty_listener = PrettyListener.new(tab)
|
|---|
| 26 | REXML::Document.parse_stream(source, @pretty_listener) |
|---|
| 27 | return @pretty_listener.prettyxml |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 |
|
|---|
| 31 | # private ----------------------------------------------- |
|---|
| 32 |
|
|---|
| 33 | class PrettyListener |
|---|
| 34 |
|
|---|
| 35 | def initialize(tab=0)
|
|---|
| 36 | @buildxml = "" |
|---|
| 37 | @depth = 0 |
|---|
| 38 | if tab == 0 then |
|---|
| 39 | @tb = "\t" |
|---|
| 40 | else |
|---|
| 41 | @tb = ' ' * tab |
|---|
| 42 | end
|
|---|
| 43 | end
|
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | def xmldecl(ver, enc, stand) |
|---|
| 47 | @buildxml = "#{@buildxml}<?xml" |
|---|
| 48 | if ver |
|---|
| 49 | @buildxml = "#{@buildxml} version='#{ver}'" |
|---|
| 50 | end |
|---|
| 51 | if enc |
|---|
| 52 | @buildxml = "#{@buildxml} encoding='#{enc}'" |
|---|
| 53 | end |
|---|
| 54 | if stand |
|---|
| 55 | @buildxml = "#{@buildxml} standalone='#{stand}'" |
|---|
| 56 | end |
|---|
| 57 | @buildxml = "#{@buildxml}?>" |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | def doctype(name, *contents) |
|---|
| 62 | @buildxml = "#{@buildxml}\n<!DOCTYPE #{name}" |
|---|
| 63 | contents.each do |c| |
|---|
| 64 | @buildxml = "#{@buildxml} #{c}" |
|---|
| 65 | end |
|---|
| 66 | @buildxml = "#{@buildxml.strip}>" |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | def instruction(name, instruction) |
|---|
| 71 | @buildxml = "#{@buildxml}\n<?#{name} #{instruction.strip} ?>" |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | def comment(comment) |
|---|
| 76 | @buildxml = "#{@buildxml}\n<!-- #{comment} -->" |
|---|
| 77 | end |
|---|
| 78 | |
|---|
| 79 |
|
|---|
| 80 | def tag_start(name, attributes) |
|---|
| 81 | @buildxml = "#{@buildxml}\n" + @tb * @depth + "<#{name}" |
|---|
| 82 | attributes.each do |a| |
|---|
| 83 | @buildxml = "#{@buildxml} #{attr_to_s(a)}" |
|---|
| 84 | end
|
|---|
| 85 | @buildxml = "#{@buildxml}>" |
|---|
| 86 | @depth += 1 |
|---|
| 87 | @closed_element = false
|
|---|
| 88 | end
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | def tag_end(name)
|
|---|
| 92 | @depth -= 1 |
|---|
| 93 | if @closed_element then |
|---|
| 94 | @buildxml = "#{@buildxml}\n" + @tb * @depth + "</#{name}>" |
|---|
| 95 | else |
|---|
| 96 | @buildxml = "#{@buildxml}</#{name}>" |
|---|
| 97 | end |
|---|
| 98 | @closed_element = true
|
|---|
| 99 | end |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | def text(text) |
|---|
| 103 | if text =~ /\n/ |
|---|
| 104 | text = "\n" + text |
|---|
| 105 | text = text.gsub(/(\n)/, "\n" + @tb * @depth) |
|---|
| 106 | text.strip! |
|---|
| 107 | #text = text + "\n" + @tb * (@depth - 1) |
|---|
| 108 | end |
|---|
| 109 | @buildxml = "#{@buildxml}#{text}" |
|---|
| 110 | #@closed_element = false |
|---|
| 111 | end |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | def method_missing(a, *b) |
|---|
| 115 | raise "Method Missing: #{a}, #{b}" |
|---|
| 116 | end |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | # |
|---|
| 120 | def attr_to_s attr |
|---|
| 121 | return(attr[0] + "=" + attr[1]) |
|---|
| 122 | end |
|---|
| 123 | |
|---|
| 124 | # |
|---|
| 125 | def prettyxml |
|---|
| 126 | return @buildxml |
|---|
| 127 | end |
|---|
| 128 |
|
|---|
| 129 | end
|
|---|
| 130 | |
|---|
| 131 | end |
|---|