con_duxml 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/con_duxml.rb +1 -1
- data/lib/con_duxml/duxml_ext/element.rb +1 -0
- data/lib/con_duxml/duxml_ext/linkable.rb +1 -0
- data/lib/con_duxml/duxml_ext/transform_class.rb +6 -0
- data/lib/con_duxml/transform.rb +6 -6
- data/lib/con_duxml/transform/transform_private.rb +54 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb76f2afcac22ab8c56593829fc84ec21ff1ac6a
|
4
|
+
data.tar.gz: 5de14320187997e34c4aba43ee3183b9e25ddc45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 209f32ee7f0108e8fd88340455eac5504b2fb11f7f1a10ca20016a391c89a35bcf52a24daa2d97d1875fdb5838d04b81a565b8c1f4280a2efe2143338cf36551
|
7
|
+
data.tar.gz: a1dfeabc917a8b7836b7e545b0082f9256f918725d76929845e99c8503af4c40783929f613bd11e45cab9c56299155be2d79456d2ee83a7be2c67fa062fe77ab
|
data/lib/con_duxml.rb
CHANGED
@@ -30,8 +30,8 @@ module ConDuxml
|
|
30
30
|
@src_ns = transforms[:src_ns]
|
31
31
|
source = doc.locate(add_name_space_prefix(transforms[:source])).first
|
32
32
|
@output.grammar = transforms[:grammar] if transforms[:grammar]
|
33
|
-
a = activate(transforms.first, source).first
|
34
33
|
add_observer @output.history
|
34
|
+
a = activate(transforms.first, source).first
|
35
35
|
@output << a
|
36
36
|
end
|
37
37
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# Copyright (c) 2016 Freescale Semiconductor Inc.
|
1
2
|
require 'duxml/meta/history/change'
|
2
3
|
|
3
4
|
include Duxml
|
@@ -14,4 +15,9 @@ class TransformClass < ChangeClass
|
|
14
15
|
def description
|
15
16
|
"#{output.description} created from #{input.description} by #{instructions.description}"
|
16
17
|
end
|
18
|
+
|
19
|
+
# @return [false] required to prevent grammar from attempting to validate transform
|
20
|
+
def error?
|
21
|
+
false
|
22
|
+
end
|
17
23
|
end
|
data/lib/con_duxml/transform.rb
CHANGED
@@ -6,8 +6,9 @@ include Duxml
|
|
6
6
|
|
7
7
|
# All public methods can be invoked by a transform element; please hide methods you don't want users to invoke in Private
|
8
8
|
module Transform
|
9
|
-
|
9
|
+
private
|
10
10
|
include Private
|
11
|
+
public
|
11
12
|
|
12
13
|
# most recent XML node from which content has been taken; for constructing relative paths
|
13
14
|
@source
|
@@ -32,11 +33,10 @@ module Transform
|
|
32
33
|
find_xform_event(node).input
|
33
34
|
end
|
34
35
|
|
35
|
-
# @param
|
36
|
-
#
|
37
|
-
|
38
|
-
|
39
|
-
@source.locate(add_name_space_prefix path).first.text
|
36
|
+
# @param node [Element, String] XML node including Strings
|
37
|
+
# @return [Array[String, Element]] array of child nodes of target
|
38
|
+
def content(node)
|
39
|
+
node.respond_to?(:nodes) ? node.nodes : node
|
40
40
|
end
|
41
41
|
|
42
42
|
# @param *args [*several_variants] see Duxml::Element#new; the only difference here is that this method has access to the content source
|
@@ -1,19 +1,25 @@
|
|
1
1
|
# Copyright (c) 2016 Freescale Semiconductor Inc.
|
2
|
+
require 'observer'
|
2
3
|
|
3
4
|
# Private is where methods should go that you DO NOT want invoked by a transform file!
|
4
5
|
module Private
|
5
|
-
|
6
|
+
include Observable
|
6
7
|
|
7
8
|
# @param xform [Element] transform element
|
8
|
-
# @param
|
9
|
+
# @param src [Element] XML xform containing content to be transformed
|
9
10
|
# @return [Array[Element]] transformed content; always an array of nodes, even if just one
|
10
|
-
def activate(xform,
|
11
|
-
@source =
|
11
|
+
def activate(xform, src)
|
12
|
+
@source = src
|
12
13
|
get_sources(xform).collect do |src|
|
13
14
|
args = get_args(xform, src)
|
14
|
-
|
15
|
+
meth = get_method(xform)
|
16
|
+
a = meth.arity
|
17
|
+
if a == -1 or args.size == a or args.size.between?(-1 - a, 0 - a)
|
18
|
+
output = meth.call(*args)
|
19
|
+
else
|
20
|
+
output = ''
|
21
|
+
end
|
15
22
|
changed
|
16
|
-
m = methods
|
17
23
|
notify_observers(:Transform, xform, src, output)
|
18
24
|
changed false
|
19
25
|
output
|
@@ -47,33 +53,61 @@ module Private
|
|
47
53
|
end
|
48
54
|
|
49
55
|
# @param xform [Element] transform element
|
50
|
-
# @param
|
56
|
+
# @param src [Element] source XML Element
|
51
57
|
# @return [Array[String, Element]] string returned by self[:args] is separated by ';' into correctly formatted argument values for transform method
|
52
|
-
def get_args(xform,
|
58
|
+
def get_args(xform, src)
|
53
59
|
args = xform.attributes.keys.sort.collect do |attr|
|
54
60
|
if attr.to_s.match(/arg[0-9]*/)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
targets.empty? ? xform[attr] : targets
|
61
|
+
arg_str = xform[attr].strip
|
62
|
+
case arg_str
|
63
|
+
when /,/ then separate_enumerables(arg_str, src)
|
64
|
+
when /^([\w]+): (\S.*)$/, /^([\S]+) => (\S.*)$/ then {$1.to_sym => normalize_arg($2, src)}
|
65
|
+
when /^'(.+)' => (.+)$/ then {$1 => normalize_arg($2, src)}
|
66
|
+
when /\// then src.locate(add_name_space_prefix arg_str).first
|
67
|
+
when /^'([\s\w]+)'$/ then $MATCH
|
68
|
+
else # arg is path to node
|
69
|
+
target = src.locate(add_name_space_prefix arg_str).first
|
70
|
+
target or ''
|
66
71
|
end
|
67
72
|
end
|
68
73
|
end.compact
|
69
74
|
children = xform.nodes.collect do |child|
|
70
|
-
activate(child,
|
75
|
+
activate(child, src)
|
71
76
|
end
|
72
77
|
args << children.flatten if children.any?
|
73
78
|
args
|
74
79
|
end
|
75
80
|
|
81
|
+
def normalize_arg(str, src)
|
82
|
+
case str
|
83
|
+
when /'.+'/ then str[1..-2]
|
84
|
+
when /^[0-9]$/ then str.to_i
|
85
|
+
when 'true' then true
|
86
|
+
when 'false' then false
|
87
|
+
else src.locate(add_name_space_prefix str).first
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# @param str [String] comma separated values that can be either an array or a Hash
|
92
|
+
# @return [Array[String]] returns separated values as array of strings
|
93
|
+
def separate_enumerables(str, src)
|
94
|
+
words = str.split(',').collect do |w| w.strip end
|
95
|
+
h = {}
|
96
|
+
a = []
|
97
|
+
words.each do |s|
|
98
|
+
case s
|
99
|
+
when /^'([\s\w]+)'$/ then a << $1
|
100
|
+
when /^(\w+): (\S.*)$/, /^(\w+) => (\S.*)$/ then h[$1.to_sym] = normalize_arg($2, src)
|
101
|
+
when /^'(.+)' => (\S.*)$/ then h[$1] = normalize_arg($2, src)
|
102
|
+
else a << normalize_arg(s, src)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
a.empty? ? h : a
|
106
|
+
end
|
107
|
+
|
108
|
+
|
76
109
|
def add_name_space_prefix(str)
|
110
|
+
str
|
77
111
|
str.split('/').collect do |w|
|
78
112
|
w.match(/\w+/) ? "#{src_ns ? src_ns+':' : ''}#{w}" : w
|
79
113
|
end.join('/')
|