|
@@ -0,0 +1,79 @@
|
|
1
|
+#!/bin/sh
|
|
2
|
+
|
|
3
|
+#
|
|
4
|
+# Data coercion helpers
|
|
5
|
+#
|
|
6
|
+# This module provides several simple functions to help transform
|
|
7
|
+# data to fit certain constraints.
|
|
8
|
+#
|
|
9
|
+
|
|
10
|
+#
|
|
11
|
+# Replacement character
|
|
12
|
+#
|
|
13
|
+COERCE__REPCHAR=${COERCE__REPCHAR:-�}
|
|
14
|
+
|
|
15
|
+coerce__nocolor() {
|
|
16
|
+ #
|
|
17
|
+ # Remove ANSI color codes
|
|
18
|
+ #
|
|
19
|
+ perl -CS -Mutf8 -MTerm::ANSIColor=colorstrip -ne 'print colorstrip $_;'
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+coerce__noctl() {
|
|
23
|
+ #
|
|
24
|
+ # Remove non-printable characters
|
|
25
|
+ #
|
|
26
|
+ perl -CS -Mutf8 -pe 'tr|[:graph:]\n\t ||c;'
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+coerce__nofdraw() {
|
|
30
|
+ #
|
|
31
|
+ # Replace frame-drawing characters with ASCII
|
|
32
|
+ #
|
|
33
|
+ # Replace frame-drawing characters according
|
|
34
|
+ # to following mapping:
|
|
35
|
+ #
|
|
36
|
+ # ┌ ┬ ┐ └ ┴ ┘ ├ ┤ │ ┼ ─
|
|
37
|
+ #
|
|
38
|
+ # ' ' ' . . . | | | | -
|
|
39
|
+ #
|
|
40
|
+ # This converts frame-drawing to ASCII, making it
|
|
41
|
+ # safer when fonts on terminals are not rendering
|
|
42
|
+ # properly.
|
|
43
|
+ #
|
|
44
|
+ perl -CS -Mutf8 -pe "
|
|
45
|
+ tr{┌┬┐}{.};
|
|
46
|
+ tr{└┴┘}{'};
|
|
47
|
+ tr{├┼┤│}{|};
|
|
48
|
+ tr{─}{-};
|
|
49
|
+ "
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+coerce__for_yaml() {
|
|
53
|
+ #
|
|
54
|
+ # Replace yaml-invalid characters
|
|
55
|
+ #
|
|
56
|
+ # Yaml won't allow all characters:
|
|
57
|
+ #
|
|
58
|
+ # > [...] The allowed character range explicitly excludes the C0 control
|
|
59
|
+ # > block #x0-#x1F (except for TAB #x9, LF #xA, and CR #xD which are
|
|
60
|
+ # > allowed), DEL #x7F, the C1 control block #x80-#x9F (except for NEL
|
|
61
|
+ # > #x85 which is allowed), the surrogate block #xD800-#xDFFF, #xFFFE,
|
|
62
|
+ # > and #xFFFF.
|
|
63
|
+ #
|
|
64
|
+ # so take stdin and replace all unacceptable characters with '�'.
|
|
65
|
+ #
|
|
66
|
+ perl -CS -Mutf8 -pe "tr/$(__coerce__for_yaml_bad)/$COERCE__REPCHAR/"
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+__coerce__for_yaml_bad() {
|
|
70
|
+ #
|
|
71
|
+ # Print all YAML-bad chars
|
|
72
|
+ #
|
|
73
|
+ printf '\N{U+0}-\N{U+8}\N{U+B}\N{U+C}\N{U+E}-\N{U+1F}' # C0 with some gaps
|
|
74
|
+ printf '\N{U+7F}' # DEL alone
|
|
75
|
+ printf '\N{U+80}-\N{U+84}\N{U+86}-\N{U+9F}' # C1 with NEL gap
|
|
76
|
+ # printf -n '\N{U+D800}-\N{U+DFFF}' # surrogates
|
|
77
|
+ # printf -n '\N{U+FFFE}-\N{U+FFFF}' # 0xFFFE and 0xFFFF
|
|
78
|
+ #FIXME: for some reasons perl complains about these ^^
|
|
79
|
+}
|