pretty-xml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/pretty-xml.rb +61 -0
  2. metadata +55 -0
@@ -0,0 +1,61 @@
1
+ require 'nokogiri'
2
+
3
+ module PrettyXML
4
+
5
+ def write(buffer)
6
+
7
+ xsl =<<XSL
8
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
9
+ <xsl:output method="xml" encoding="UTF-8"/>
10
+ <xsl:param name="indent-increment" select="' '"/>
11
+
12
+ <xsl:template name="newline">
13
+ <xsl:text disable-output-escaping="yes">
14
+ </xsl:text>
15
+ </xsl:template>
16
+
17
+ <xsl:template match="comment() | processing-instruction()">
18
+ <xsl:param name="indent" select="''"/>
19
+ <xsl:call-template name="newline"/>
20
+ <xsl:value-of select="$indent"/>
21
+ <xsl:copy />
22
+ </xsl:template>
23
+
24
+ <xsl:template match="text()">
25
+ <xsl:param name="indent" select="''"/>
26
+ <xsl:call-template name="newline"/>
27
+ <xsl:value-of select="$indent"/>
28
+ <xsl:value-of select="normalize-space(.)"/>
29
+ </xsl:template>
30
+
31
+ <xsl:template match="text()[normalize-space(.)='']"/>
32
+
33
+ <xsl:template match="*">
34
+ <xsl:param name="indent" select="''"/>
35
+ <xsl:call-template name="newline"/>
36
+ <xsl:value-of select="$indent"/>
37
+ <xsl:choose>
38
+ <xsl:when test="count(child::*) > 0">
39
+ <xsl:copy>
40
+ <xsl:copy-of select="@*"/>
41
+ <xsl:apply-templates select="*|text()">
42
+ <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
43
+ </xsl:apply-templates>
44
+ <xsl:call-template name="newline"/>
45
+ <xsl:value-of select="$indent"/>
46
+ </xsl:copy>
47
+ </xsl:when>
48
+ <xsl:otherwise>
49
+ <xsl:copy-of select="."/>
50
+ </xsl:otherwise>
51
+ </xsl:choose>
52
+ </xsl:template>
53
+ </xsl:stylesheet>
54
+ XSL
55
+
56
+ doc = Nokogiri::XML(buffer)
57
+ xslt = Nokogiri::XSLT(xsl)
58
+ out = xslt.transform(doc)
59
+ out.to_xml
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty-xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/pretty-xml.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Pretty print XML depends upon Nokogiri to perform XML transformation.
54
+ test_files: []
55
+