summaryrefslogtreecommitdiffstats
path: root/lib/strings.nix
diff options
context:
space:
mode:
authorRobert Helgesson <robert@rycee.net>2021-10-03 11:28:03 +0200
committerRobert Helgesson <robert@rycee.net>2021-10-03 11:28:03 +0200
commite75f346aa309515fa220f6d2124a27f72e245b69 (patch)
tree9980881c64dd5b4f9b4076abde8a62414a7d7f30 /lib/strings.nix
parenta7eb89bdba881d71c49b94ea3e12af9b8b10768d (diff)
lib: add function escapeXML
Given a string, this function returns a string that can be inserted verbatim in an XML document.
Diffstat (limited to 'lib/strings.nix')
-rw-r--r--lib/strings.nix13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index a111e1e2597b..de135d1c2746 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -362,6 +362,19 @@ rec {
if match "[a-zA-Z_][a-zA-Z0-9_'-]*" s != null
then s else escapeNixString s;
+ /* Escapes a string such that it is safe to include verbatim in an XML
+ document.
+
+ Type: string -> string
+
+ Example:
+ escapeXML ''"test" 'test' < & >''
+ => "\\[\\^a-z]\\*"
+ */
+ escapeXML = builtins.replaceStrings
+ ["\"" "'" "<" ">" "&"]
+ ["&quot;" "&apos;" "&lt;" "&gt;" "&amp;"];
+
# Obsolete - use replaceStrings instead.
replaceChars = builtins.replaceStrings or (
del: new: s: