From ebb7a3d10a3f16c9c5401ba03071193e97a3afb8 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Wed, 9 Mar 2022 00:22:03 +0100 Subject: [PATCH] Add ability to source catmd.sh itself (providing catmd() also as function) Outsourced line reading function to linefeeder --- catmd.sh | 61 +++++++++++++++++++++------------------- linefeeder/linefeeder.sh | 32 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 29 deletions(-) create mode 100755 linefeeder/linefeeder.sh diff --git a/catmd.sh b/catmd.sh index 6ecd03c..f3266a6 100755 --- a/catmd.sh +++ b/catmd.sh @@ -1,5 +1,7 @@ #!/bin/bash +# Script to beautify markdown syntax on command line + DEBUG=: #DEBUG=echo @@ -311,34 +313,6 @@ parseline() { printf "\n" } -catmd() { - for arg in "$@"; do - case "$1" in - -) - # treat - as input stream - break;; - -*) - # Ignore args for now - shift;; - esac - done - - local line= - local myargs=("$@") - - # Use standard input if now files are present - [ $# -eq 0 ] && myargs[0]="/dev/stdin" - - for file in "${myargs[@]}"; do - [ "$file" == "-" ] && file="/dev/stdin" - while IFS='' read -r line; do - parseline "$line" - done <"${file}" - # If last line in a file has no newline, read ends but still populates $line - [[ $line ]] && parseline "$line" - echo -en "$FO_RESET" - done -} ## Font formating FO_RESET=$(tput sgr0) @@ -401,4 +375,33 @@ FO_LINKURL="${FO_UL}" FO_CODEBLOCK="$COF_LIGHTGRAY$COB_LIGHTGRAY" FO_QUOTE='\033[96;100m' -catmd "$@" +# Use linefeeder for input handling +WDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)" +source ${WDIR}/linefeeder/linefeeder.sh + +# Provide catmd also as function if this script is sourced +catmd() { + linefeeder "$@" + echo -en "$FO_RESET" +} + +### Check if script is sourced +### https://stackoverflow.com/a/28776166 +sourced=0 +if [ -n "$ZSH_EVAL_CONTEXT" ]; then + case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac +elif [ -n "$KSH_VERSION" ]; then + [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1 +elif [ -n "$BASH_VERSION" ]; then + (return 0 2>/dev/null) && sourced=1 +else # All other shells: examine $0 for known shell binary filenames + # Detects `sh` and `dash`; add additional shell filenames as needed. + case ${0##*/} in sh|dash) sourced=1;; esac +fi +### + +# Run directly only if not sourced +if [ $sourced -eq 0 ]; then + linefeeder "$@" + echo -en "$FO_RESET" +fi diff --git a/linefeeder/linefeeder.sh b/linefeeder/linefeeder.sh new file mode 100755 index 0000000..086eef9 --- /dev/null +++ b/linefeeder/linefeeder.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# define parseline() in the sourcing script + +linefeeder() { + for arg in "$@"; do + case "$1" in + -) + # treat - as input stream + break;; + -*) + # Ignore options for now + shift;; + esac + done + + local file= + local line= + local args=("$@") + + # Use standard input if no files are present + [ $# -eq 0 ] && args[0]="/dev/stdin" + + for file in "${args[@]}"; do + [ "$file" == "-" ] && file="/dev/stdin" + while IFS='' read -r line; do + parseline "$line" + done <"${file}" + # If last line in a file has no newline, read ends but still populates $line + [[ $line ]] && parseline "$line" + done +}