polyrex-xslt 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/polyrex-xslt.rb +90 -0
  2. metadata +55 -0
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: polyrex-xslt.rb
4
+
5
+ class PolyrexXSLT
6
+
7
+ attr_accessor :schema, :xslt_schema
8
+
9
+ def initialize(options={})
10
+ o = {schema: '', xslt_schema: ''}.merge(options)
11
+ @schema, @xslt_schema = o.values
12
+ end
13
+
14
+ def to_xslt()
15
+
16
+ header =<<HEADER
17
+ <?xml version="1.0" encoding="UTF-8"?>
18
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
19
+ <xsl:output method="xml" indent="yes" />
20
+
21
+ HEADER
22
+
23
+ a_element = @schema.split('/').map{|x| x[/\w+/]}
24
+
25
+ a_html = @xslt_schema.split('/').map do |x|
26
+
27
+ name = x[/\w+/]
28
+ children = ($')[/^\[([^\]]+)\]$/,1]
29
+ list = children.split(',').map {|y| y.split(':') } if children
30
+
31
+ [name, list]
32
+ end
33
+
34
+ a = a_element.zip(a_html).map.with_index do |a,i|
35
+ out = []
36
+ tag = a.shift
37
+ field = i > 0 ? 'records/' + tag : tag
38
+ out << "<xsl:template match='#{field}'>" + "\n"
39
+ a.flatten!(1)
40
+ if a.last.is_a? Array then
41
+
42
+ out << scan_e(a, tag)
43
+ out << "</xsl:template>\n\n"
44
+ else
45
+ out << " <%s>\n" % a.first
46
+ out << " <xsl:apply-templates select='summary'/>\n"
47
+ out << " <xsl:apply-templates select='records'/>\n"
48
+ out << " <\\%s>\n" % a.first
49
+ out << "</xsl:template>\n\n"
50
+ out << "<xsl:template match='%s/summary'>\n" % [tag]
51
+ out << "</xsl:template>\n\n"
52
+ end
53
+
54
+ out
55
+ end
56
+
57
+ header + a.flatten.join + "</xsl:stylesheet>"
58
+ end
59
+
60
+ private
61
+
62
+ def scan_e(a, prev_tag='', indent=' ')
63
+
64
+ out = []
65
+
66
+ unless a.first.is_a? Array then
67
+ tag = a.shift
68
+ out << indent + "<%s>\n" % tag
69
+ out << indent + " <xsl:apply-templates select='summary'/>\n"
70
+ out << indent + "<\\%s>\n" % tag
71
+ out << "</xsl:template>\n\n"
72
+ out << "<xsl:template match='%s/summary'>\n" % [prev_tag]
73
+
74
+ a.flatten!(1)
75
+ if a.last.is_a? Array then
76
+ out << scan_e(a, tag, indent + ' ')
77
+ else
78
+ out << indent + ' ' + a.first + "\n"
79
+ end
80
+
81
+ else
82
+ a.map do |x|
83
+ out << indent + "<%s><xsl:value-of select='%s'/></%s>\n" % [x[0],x[-1],x[0]]
84
+ end
85
+ end
86
+
87
+ out
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polyrex-xslt
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: 2011-01-30 00:00:00 +00: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/polyrex-xslt.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: polyrex-xslt
54
+ test_files: []
55
+