-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathxml_encode.rb
More file actions
30 lines (28 loc) · 962 Bytes
/
xml_encode.rb
File metadata and controls
30 lines (28 loc) · 962 Bytes
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
# frozen_string_literal: true
# @summary Encode strings for XML files
#
# This function can encode strings such that they can be used directly in XML files.
# It supports encoding for both XML text (CharData) or attribute values (AttValue).
Puppet::Functions.create_function(:'stdlib::xml_encode') do
# @param str The string to encode
# @param type Whether to encode for text or an attribute
# @return Returns the encoded CharData or AttValue string suitable for use in XML
# @example Creating an XML file from a template
# file { '/path/to/config.xml':
# ensure => file,
# content => epp(
# 'mymodule/config.xml.epp',
# {
# password => $password.stdlib::xml_encode,
# },
# ),
# }
dispatch :xml_encode do
param 'String', :str
optional_param "Enum['text','attr']", :type
return_type 'String'
end
def xml_encode(str, type = 'text')
str.encode(xml: type.to_sym)
end
end