| |
@@ -0,0 +1,79 @@
|
| |
+ #!/bin/sh
|
| |
+
|
| |
+ #
|
| |
+ # Data coercion helpers
|
| |
+ #
|
| |
+ # This module provides several simple functions to help transform
|
| |
+ # data to fit certain constraints.
|
| |
+ #
|
| |
+
|
| |
+ #
|
| |
+ # Replacement character
|
| |
+ #
|
| |
+ COERCE__REPCHAR=${COERCE__REPCHAR:-�}
|
| |
+
|
| |
+ coerce__nocolor() {
|
| |
+ #
|
| |
+ # Remove ANSI color codes
|
| |
+ #
|
| |
+ perl -CS -Mutf8 -MTerm::ANSIColor=colorstrip -ne 'print colorstrip $_;'
|
| |
+ }
|
| |
+
|
| |
+ coerce__noctl() {
|
| |
+ #
|
| |
+ # Remove non-printable characters
|
| |
+ #
|
| |
+ perl -CS -Mutf8 -pe 'tr|[:graph:]\n\t ||c;'
|
| |
+ }
|
| |
+
|
| |
+ coerce__nofdraw() {
|
| |
+ #
|
| |
+ # Replace frame-drawing characters with ASCII
|
| |
+ #
|
| |
+ # Replace frame-drawing characters according
|
| |
+ # to following mapping:
|
| |
+ #
|
| |
+ # ┌ ┬ ┐ └ ┴ ┘ ├ ┤ │ ┼ ─
|
| |
+ #
|
| |
+ # ' ' ' . . . | | | | -
|
| |
+ #
|
| |
+ # This converts frame-drawing to ASCII, making it
|
| |
+ # safer when fonts on terminals are not rendering
|
| |
+ # properly.
|
| |
+ #
|
| |
+ perl -CS -Mutf8 -pe "
|
| |
+ tr{┌┬┐}{.};
|
| |
+ tr{└┴┘}{'};
|
| |
+ tr{├┼┤│}{|};
|
| |
+ tr{─}{-};
|
| |
+ "
|
| |
+ }
|
| |
+
|
| |
+ coerce__for_yaml() {
|
| |
+ #
|
| |
+ # Replace yaml-invalid characters
|
| |
+ #
|
| |
+ # Yaml won't allow all characters:
|
| |
+ #
|
| |
+ # > [...] The allowed character range explicitly excludes the C0 control
|
| |
+ # > block #x0-#x1F (except for TAB #x9, LF #xA, and CR #xD which are
|
| |
+ # > allowed), DEL #x7F, the C1 control block #x80-#x9F (except for NEL
|
| |
+ # > #x85 which is allowed), the surrogate block #xD800-#xDFFF, #xFFFE,
|
| |
+ # > and #xFFFF.
|
| |
+ #
|
| |
+ # so take stdin and replace all unacceptable characters with '�'.
|
| |
+ #
|
| |
+ perl -CS -Mutf8 -pe "tr/$(__coerce__for_yaml_bad)/$COERCE__REPCHAR/"
|
| |
+ }
|
| |
+
|
| |
+ __coerce__for_yaml_bad() {
|
| |
+ #
|
| |
+ # Print all YAML-bad chars
|
| |
+ #
|
| |
+ printf '\N{U+0}-\N{U+8}\N{U+B}\N{U+C}\N{U+E}-\N{U+1F}' # C0 with some gaps
|
| |
+ printf '\N{U+7F}' # DEL alone
|
| |
+ printf '\N{U+80}-\N{U+84}\N{U+86}-\N{U+9F}' # C1 with NEL gap
|
| |
+ # printf -n '\N{U+D800}-\N{U+DFFF}' # surrogates
|
| |
+ # printf -n '\N{U+FFFE}-\N{U+FFFF}' # 0xFFFE and 0xFFFF
|
| |
+ #FIXME: for some reasons perl complains about these ^^
|
| |
+ }
|
| |
For situations when there are constraints on what characters are
allowed, this module provides several non-reversible conversion
functions.