Compare commits

8 Commits
main ... dev

Author SHA1 Message Date
090c7780d9 Use . for sourcing (POSIX standard) 2022-03-12 16:02:41 +01:00
1e007d436e Option to detect newlines at line endings
More function documentation
2022-03-10 13:57:40 +01:00
c073aa13b9 Fix WDIR evaluation when called via symlink 2022-03-09 00:53:58 +01:00
ebb7a3d10a Add ability to source catmd.sh itself (providing catmd() also as function)
Outsourced line reading function to linefeeder
2022-03-09 00:22:03 +01:00
39b184e40e Refine multi line codeblock formating
Some shells set the line background for complete terminal width instead of only the available characters to print
2022-03-07 00:18:20 +01:00
a9ac8a5d1e Support for paragraph and code block indentation inside list elements 2022-03-06 21:27:56 +01:00
dd426b16d5 Handle multiple input file arguments as well as - for standard input 2022-03-05 16:59:12 +01:00
4fbb7ec04d WIP on lists and correct indentation 2022-03-05 01:10:43 +01:00
2 changed files with 464 additions and 0 deletions

407
catmd.sh Executable file
View File

@@ -0,0 +1,407 @@
#!/bin/bash
# Script to beautify markdown syntax on command line
DEBUG=:
#DEBUG=echo
#INDENT_SPACE_SHORT=2
#INDENT_SPACE=4
#INDENT_TAB=2
#rex_nonemptyline_indent='^([ ]{'$INDENT_SPACE_SHORT'}|[ ]{'$INDENT_SPACE'}|[\ ]{'$INDENT_TAB'})[[:graph:]]+' # for $INDENT_SPACE spaces or $INDENT_TAB tabs
rex_emptyline='^$'
rex_nonemptyline='^([[:blank:]]*)([[:graph:]]+.*)'
rex_header='^[[:blank:]]*(#+)[[:blank:]]*(.*)'
rex_list_start='^([[:blank:]]{0,1})([*+-]{1} +|[[:digit:]]+\.)(.*)'; flag_list=0; indent_list=
rex_list_level='^([[:blank:]]{2,})([*+-]{1} +|[[:digit:]]+\.)(.*)'
rex_list="$rex_list_start"
rex_codeblock='^[[:blank:]]*(```)(.*)'
rex_codeinline_start='([^`]*)(`{1,2})(.*)'
rex_codeinline_space='(^[[:blank:]]{4})([[:blank:]]*)(.*)'
rex_codeinline1='([^`]*)(`)(.*)'
rex_codeinline2='(``)(.*)$' # first part of $line: ${line%"${BASH_REMATCH[0]}"}
rex_codeinline="$rex_codeinline_start"
rex_quote='^([[:blank:]]*>)(.*)'; flag_quote=0
rex_emphasis_start='([^*_]*)([*_]{1,2})([^ ].*)'
rex_emphasis_end='([^*_]*)([*_]{1,2})(.*)'
rex_emphasis="$rex_emphasis_start"
rex_link='([^[]*\[+)([^]]*)(\]+ *[\([])([^]\)]*)([]\)].*)'
rex_autolink='([^<]*<)([^>]*)(>)(.*)'
TEXT_INDENT=0
LIST_INDENT_LAST=
LINE_INDENT=
LINE_INDENT_N=
LINE_EMPTY_N=
PARSE_STATE=0
PARSE_FIX=0
FIX_STATE=0
UNFIX_STATE=0
# Next parsted line is directed directly to the fixed state
fix() {
PARSE_FIX=1
FIX_STATE=$PARSE_STATE
[ ! -z "$1" ] && PARSE_STATE=$1
}
isfix() { [ $PARSE_FIX -ne 0 ] && [ $FIX_STATE -eq $PARSE_STATE ]; return $?; }
# Unfix and/or stop parsing for this line
unfix() {
UNFIX_STATE=1
PARSE_FIX=0
FIX_STATE=0
}
isunfix() { [ $UNFIX_STATE -ne 0 ]; return $?; }
# Decides when to got the next parser stop
# Called usually like: fall || break
fall() {
if isunfix; then
$DEBUG -n "U]"
UNFIX_STATE=0
PARSE_STATE=0
return 1
fi
# Fall to next PARSE_STATE if not fixed
if [ $PARSE_FIX -eq 0 ]; then
((PARSE_STATE++))
return 0
fi
if [ $PARSE_STATE -ne $FIX_STATE ]; then
PARSE_STATE=$FIX_STATE
return 0
else
$DEBUG -n "B]"
return 1
fi
}
parseline() {
local line="$1"
local tocheck="$1"
local format_prefix=
local format_suffix=
[ $PARSE_FIX -eq 0 ] && PARSE_STATE=0
while : ; do
$DEBUG -n "$PARSE_STATE]"
case "$PARSE_STATE" in
0) ## Empty line ##
if [[ "$tocheck" =~ $rex_emptyline ]]; then
((LINE_EMPTY_N++))
# End quote
[ $flag_quote -ne 0 ] && flag_quote=0 && unfix
# End text indent
[ $TEXT_INDENT -ne 0 ] && TEXT_INDENT=0
# TODO? Catch rouge formates with empty lines outside codeblocks
echo -ne "$FO_RESET"
format_prefix=""
format_suffix=""
fi
fall || break;;
1) ## Non empty line ##
if [[ "$tocheck" =~ $rex_nonemptyline ]]; then
LINE_INDENT_N=${#BASH_REMATCH[1]}
LINE_INDENT="${BASH_REMATCH[1]}"
LINE_EMPTY_N=0
# List ending
if [ $flag_list -ne 0 ] && [ $LINE_INDENT_N -le 1 ] && [ $TEXT_INDENT -eq 0 ]; then
rex_list="$rex_list_start"
flag_list=0
indent_list=
LIST_INDENT_LAST=
fi
fi
fall || break;;
2) ## List ##
if [[ "$tocheck" =~ $rex_list ]]; then
indent_list[$flag_list]="$LINE_INDENT"
LIST_INDENT_LAST="$LINE_INDENT"
if (($flag_list > 0 )); then
local level=$((${#LINE_INDENT} - ${#indent_list[$flag_list - 1]}))
#echo -n $level
#if (($level > 1 )); then
# echo -n ">]"
#elif (($level < -1 )); then
# echo -n "<]"
#else
# echo -n "=]"
#fi
fi
# Detect indent TODO detect levels of indent
#local tab=$(printf '\t')
#case "$LINE_INDENT" in
# "${tab}${tab}")
# LINE_INDENT="${BASH_REMATCH[1]}"
# $DEBUG -n "2t]";;
# ' ')
# LINE_INDENT="${BASH_REMATCH[1]}"
# $DEBUG -n "4s]";;
# ' ')
# LINE_INDENT="${BASH_REMATCH[1]}"
# $DEBUG -n "2s]";;
# *)
# LINE_INDENT=
#esac
((flag_list++))
rex_list="$rex_list_level"
local indent=
[ $flag_list -gt 1 ] && indent="$LINE_INDENT" #"${BASH_REMATCH[1]}"
printf "%s%b%s%b" "$indent" "$FO_LISTITEM" "${BASH_REMATCH[2]}" "$FO_RESET"
tocheck="${BASH_REMATCH[3]}"
line="$tocheck"
elif [[ "$tocheck" =~ $rex_list_start ]]; then
# Start of new list
flag_list=0
indent_list=
LIST_INDENT_LAST=
rex_list="$rex_list_start"
continue
elif [ $flag_list -ne 0 ] && [ $LINE_INDENT_N -le 1 ] && [ $TEXT_INDENT -ne 0 ]; then
printf "%-${TEXT_INDENT}s" " "
fi
fall || break
;;
3) ## Header ##
if [[ "$tocheck" =~ $rex_header ]]; then
format_prefix="${FO_HEADLINE}"
format_suffix="${FO_RESET}"
unfix
fi
fall || break;;
4) ## Quotes
if [[ "$tocheck" =~ $rex_quote ]] || isfix; then
# TODO continue parsing quoted string
fix 0
flag_quote=1
format_prefix="${FO_QUOTE}"
format_suffix="${FO_RESET}"
break
else
fall || break
fi
;;
5) ## Multiline code blocks ##
if [[ "$tocheck" =~ $rex_codeblock ]]; then
if isfix; then
unfix
else
fix
fi
format_prefix="${FO_CODEBLOCK}"
format_suffix="${FO_RESET}"
elif isfix; then
format_prefix="${FO_CODEBLOCK}"
format_suffix="${FO_RESET}"
fi
fall || break;;
6) ## Code block started with spaces
if [[ "$tocheck" =~ $rex_codeinline_space ]]; then
if [ $LINE_INDENT_N -eq 4 ] && [ $flag_list -ne 0 ]; then
# First line of text paragraph inside a list item
# signaling indentation for the following lines
TEXT_INDENT=$LINE_INDENT_N
else
# Code block started with 4 spaces TODO 2 tabs
if [ $flag_list -ne 0 ] && [ $LINE_INDENT_N -ge 8 ]; then
# Indent code line according to list level
printf "%-4s%s" " " "$LIST_INDENT_LAST"
# Remove 4 leading spaces
printf "%b%s" "$FO_CODEBLOCK" "${BASH_REMATCH[2]#' '}"
else
# Print code related spaces
printf "%b%s" "$FO_CODEBLOCK" "${BASH_REMATCH[2]}"
fi
format_suffix="$FO_RESET"
printf "%b%s" "$FO_CODEBLOCK" "${BASH_REMATCH[3]}"
line=
unfix
fi
fi
fall || break;;
7) ## Inline code blocks ##
if [[ "$tocheck" =~ $rex_codeinline ]]; then
if isfix; then
local lazym="${tocheck%"${BASH_REMATCH[0]}"}"
if [ -z "$lazym" ]; then
printf "%b%s%b" "$FO_CODEBLOCK" "${BASH_REMATCH[1]}${BASH_REMATCH[2]}" "${FO_RESET}"
tocheck="${BASH_REMATCH[3]}"
else
printf "%b%s%b" "$FO_CODEBLOCK" "${lazym}${BASH_REMATCH[1]}" "${FO_RESET}"
tocheck="${BASH_REMATCH[2]}"
fi
rex_codeinline="$rex_codeinline_start"
unfix
else
if [ '``' == "${BASH_REMATCH[2]}" ]; then
rex_codeinline="$rex_codeinline2"
else
rex_codeinline="$rex_codeinline1"
fi
printf "%s%b%s" "${BASH_REMATCH[1]}" "${FO_CODEBLOCK}" "${BASH_REMATCH[2]}"
tocheck="${BASH_REMATCH[3]}"
fix
fi
line="$tocheck"
elif isfix; then
# Open inline block at end of line
format_suffix="$FO_RESET"
fall || break
fi
fall;;
8) ## Inline emphasis start
if [[ "$tocheck" =~ $rex_emphasis ]]; then
local fo_emphasis=$FO_EMPHASIS1
if isfix; then
printf "%s%b" "${BASH_REMATCH[1]}${BASH_REMATCH[2]}" "${FO_RESET}"
rex_emphasis="$rex_emphasis_start"
unfix
else
if [ '**' == "${BASH_REMATCH[2]}" ] || [ '__' == "${BASH_REMATCH[2]}" ]; then
fo_emphasis=$FO_EMPHASIS2
fi
printf "%s%b%s" "${BASH_REMATCH[1]}" "${fo_emphasis}" "${BASH_REMATCH[2]}"
rex_emphasis="$rex_emphasis_end"
fix
fi
tocheck="${BASH_REMATCH[3]}"
line="$tocheck"
elif isfix; then
# Open inline block at end of line
fall || break
fi
fall;;
9) ## Links within one line ##
if [[ "$tocheck" =~ $rex_link ]]; then
printf "%s%b%s%b%s%b%s%b%s" "${BASH_REMATCH[1]}" "${FO_LINKTEXT}" "${BASH_REMATCH[2]}" "${FO_RESET}" "${BASH_REMATCH[3]}" "${FO_LINKURL}" \
"${BASH_REMATCH[4]}" "${FO_RESET}"
tocheck="${BASH_REMATCH[5]}"
line="$tocheck"
[ ! -z "$tocheck" ] && fix
elif isfix; then
unfix
fall || break
fi
fall;;
10) ## Auto-links within one line ##
if [[ "$tocheck" =~ $rex_autolink ]]; then
printf "%s%b%s%b%s" "${BASH_REMATCH[1]}" "${FO_LINKURL}" "${BASH_REMATCH[2]}" "${FO_RESET}" "${BASH_REMATCH[3]}"
tocheck="${BASH_REMATCH[4]}"
line="$tocheck"
[ ! -z "$tocheck" ] && fix
elif isfix; then
unfix
fall || break
fi
fall;;
*) ## Regular text
$DEBUG -n "e]$tocheck"
break;;
esac
done
printf "%b%s%b" "$format_prefix" "$line" "$format_suffix"
printf "\n"
}
## Font formating
FO_RESET=$(tput sgr0)
FO_BLINK=$(tput blink) # '\033[5m'
FO_BOLD=$(tput bold) # '\033[1m'
FO_SO=$(tput smso) # '\033[7m'
FO_SO_E=$(tput rmso) # '\033[27m'
FO_UL=$(tput smul) # '\033[4m'
FO_UL_E=$(tput smul) # '\033[24m'
FO_INVIS=$(tput invis) # '\033[8m'
## Rareley supported
FO_DIM=$(tput dim)
FO_REV=$(tput rev)
## Colorcodes
##
## '\033[Xm' - Basic 8 colors
## X = 30..37 foreground
## = 40..47 background
## = Black, Red, Green, Yellow, Blue, Magenta, Cyan, Light gray
## = 39 = default foreground
## = 49 = default background
## - Basic "high contrast" colors
## X = 90...97 foreground
## = 100..107 background
## = Dark grey, Light red, green, yellow, blue, magenta, cyan, white
## - Format
## X: 0 = reset, 1 = bold, 2 = dim,
## 4 = underline, 5 = slow blink,
## 7 = Reverse, 8 = hidden
## 20+X: Reset of formating
## e.g. 21 = reset bold
## '\033[38;5;Xm' - xterm-256 foreground colors X = 0..255
## '\033[48;5;Xm' - xterm-256 foreground colors X = 0..255
COB_BLACK='\033[40m'
COB_LIGHTGRAY='\033[100m'
COF_BLACK='\033[0;30m'
COF_DARKGRAY='\033[1;30m'
COF_RED='\033[0;31m'
COF_LIGHTRED='\033[1;31m'
COF_GREEN='\033[0;32m'
COF_LIGHTGREEN='\033[1;32m'
COF_ORANGE='\033[0;33m'
COF_YELLOW='\033[1;33m'
COF_BLUE='\033[0;34m'
COF_LIGHTBLUE='\033[1;34m'
COF_PURPLE='\033[0;35m'
COF_LIGHTPURPLE='\033[1;35m'
COF_CYAN='\033[0;36m'
COF_LIGHTCYAN='\033[1;36m'
COF_LIGHTGRAY='\033[0;37m'
COF_WHITE='\033[1;37m'
FO_HEADLINE="$FO_BOLD$FO_SO"
FO_LISTITEM="$COF_ORANGE"
FO_EMPHASIS1="${COF_LIGHTGRAY}"
FO_EMPHASIS2="${COF_WHITE}"
FO_LINKTEXT="${COF_LIGHTGRAY}"
FO_LINKURL="${FO_UL}"
FO_CODEBLOCK="$COF_LIGHTGRAY$COB_LIGHTGRAY"
FO_QUOTE='\033[96;100m'
# Use linefeeder for input handling
WDIR="$(cd "$(dirname -- "$(realpath ${BASH_SOURCE[0]})")" >>/dev/null 2>&1 && pwd)"
. ${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 for all kinds of shells
### 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

57
linefeeder/linefeeder.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/bin/bash
## parseline [-n|-e] <LINE>
## This function must be defined in the sourcing script
##
## [-n|-e] : If linefeeder is called with -n every line is passed
## to parseline with either -n or -e as first parameter
## -n Line has a newline
## -e Line has no newline
## linefeeder [OPTION] [FILE...]
## Read input line by line and behaves parameter-wise like cat
##
## [OPTION]
## -n : Provide information to parseline if current line
## has a newline at the end (parseline -n "$line")
## or no newline at the end (parseline -e "$line")
##
## [FILE]
## - : Is treadted as /dev/stdin
##
linefeeder() {
local newLine=
for arg in "$@"; do
case "$1" in
-) # treat - as input stream
break;;
--) # treat -- as end of parameter
shift
break;;
-n) # Newline detection
newLine="-n"
shift;;
-*) # Ignore other options
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"
newLine=${newLine/"-e"/"-n"}
while IFS= read -r line; do
parseline $newLine "$line"
done <"${file}"
# If last line in a file has no newline, read ends but still populates $line
[ -n "$line" ] && parseline ${newLine/"-n"/"-e"} "$line"
done
}