Browse Source

Add coerce.sh for character set coercion

For situations when there are constraints on what characters are
allowed, this module provides several non-reversible conversion
functions.
Alois Mahdal 5 years ago
parent
commit
e2214d4c0f
3 changed files with 103 additions and 0 deletions
  1. 17
    0
      packaging/debian/control
  2. 7
    0
      packaging/template.spec
  3. 79
    0
      src/include-sh/coerce.sh

+ 17
- 0
packaging/debian/control View File

@@ -172,6 +172,23 @@ Description: Shellfu/sh module for building interactive terminal menus
172 172
  This sub-package contains 'charmenu', a Shellfu/sh module for building
173 173
  interactive terminal menus
174 174
 
175
+Package: shellfu-sh-coerce
176
+Architecture: all
177
+Depends: shellfu-sh
178
+Description: Shellfu/sh module with data coercion helpers
179
+ Shellfu is an attempt to add modularity to your shell scripts.
180
+ .
181
+ With Shellfu you can develop your shell modules separately from your
182
+ scripts, and test, use, explore or study them without need to be aware
183
+ of details such as where the actual files are placed.
184
+ .
185
+ Shellfu is mostly intended for cases when there's need for non-trivial
186
+ amount of reliable body of shell scripts, and access to advanced modular
187
+ languages such as Python or Perl is limited.
188
+ .
189
+ This sub-package contains 'coerce', Shellfu module containing few
190
+ data coercion helpers.
191
+
175 192
 Package: shellfu-sh-exit
176 193
 Architecture: all
177 194
 Depends: shellfu-sh

+ 7
- 0
packaging/template.spec View File

@@ -97,6 +97,13 @@ Obsoletes: shellfu-bash-extras < 0.10
97 97
 This sub-package contains 'charmenu', a Shellfu/sh module for building
98 98
 interactive terminal menus
99 99
 
100
+%package sh-coerce
101
+Summary: Shellfu/sh module with data coercion helpers
102
+Requires: shellfu-sh
103
+%description sh-isa
104
+This sub-package contains 'coerce', Shellfu module containing few
105
+data coercion helpers.
106
+
100 107
 %package sh-exit
101 108
 Summary: Shellfu/sh module for standard exit statuses
102 109
 Requires: shellfu-sh

+ 79
- 0
src/include-sh/coerce.sh View File

@@ -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
+}