From e2214d4c0f8328fb56476b03c8f3284a557dc630 Mon Sep 17 00:00:00 2001 From: Alois Mahdal Date: Oct 23 2018 18:07:03 +0000 Subject: 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. --- diff --git a/packaging/debian/control b/packaging/debian/control index 8068c45..4b2ab90 100644 --- a/packaging/debian/control +++ b/packaging/debian/control @@ -172,6 +172,23 @@ Description: Shellfu/sh module for building interactive terminal menus This sub-package contains 'charmenu', a Shellfu/sh module for building interactive terminal menus +Package: shellfu-sh-coerce +Architecture: all +Depends: shellfu-sh +Description: Shellfu/sh module with data coercion helpers + Shellfu is an attempt to add modularity to your shell scripts. + . + With Shellfu you can develop your shell modules separately from your + scripts, and test, use, explore or study them without need to be aware + of details such as where the actual files are placed. + . + Shellfu is mostly intended for cases when there's need for non-trivial + amount of reliable body of shell scripts, and access to advanced modular + languages such as Python or Perl is limited. + . + This sub-package contains 'coerce', Shellfu module containing few + data coercion helpers. + Package: shellfu-sh-exit Architecture: all Depends: shellfu-sh diff --git a/packaging/template.spec b/packaging/template.spec index af4f22b..ed21204 100644 --- a/packaging/template.spec +++ b/packaging/template.spec @@ -97,6 +97,13 @@ Obsoletes: shellfu-bash-extras < 0.10 This sub-package contains 'charmenu', a Shellfu/sh module for building interactive terminal menus +%package sh-coerce +Summary: Shellfu/sh module with data coercion helpers +Requires: shellfu-sh +%description sh-isa +This sub-package contains 'coerce', Shellfu module containing few +data coercion helpers. + %package sh-exit Summary: Shellfu/sh module for standard exit statuses Requires: shellfu-sh diff --git a/src/include-sh/coerce.sh b/src/include-sh/coerce.sh new file mode 100644 index 0000000..796e956 --- /dev/null +++ b/src/include-sh/coerce.sh @@ -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 ^^ +}