Add ability to source catmd.sh itself (providing catmd() also as function)
Outsourced line reading function to linefeeder
This commit is contained in:
61
catmd.sh
61
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
|
||||
|
32
linefeeder/linefeeder.sh
Executable file
32
linefeeder/linefeeder.sh
Executable file
@@ -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
|
||||
}
|
Reference in New Issue
Block a user