#!/usr/bin/env bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2026 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. # # * Redistribution and use in source and binary forms, # * with or without modification, are permitted provided # * that the following conditions are met: # * # * Redistributions of source code must retain the above # * copyright notice, this list of conditions and the # * following disclaimer. # * # * Redistributions in binary form must reproduce the above # * copyright notice, this list of conditions and the following # * disclaimer in the documentation and/or other materials # * provided with the distribution. # * # * Neither the name of the copyright holder nor the names # * of any other contributors may be used to endorse or # * promote products derived from this software without # * specific prior written permission. # * # * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND # * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, # * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Color definitions set_colors() { # Standard 16 colors display declaration export DEFAULTFG='\e[0;39m' export DEFAULTBG='\e[0;49m' export DEFAULTCOL="${DEFAULTBG}${DEFAULTFG}" export RESETCOL=$'\e[0m' # Regular Colors export Black='\e[0;30m' export Red='\e[0;31m' export Green='\e[0;32m' export Yellow='\e[0;33m' export Blue='\e[0;34m' export Purple='\e[0;35m' export Cyan='\e[0;36m' export White='\e[0;37m' # Bold export BBlack='\e[1;30m' export BRed='\e[1;31m' export BGreen='\e[1;32m' export BYellow='\e[1;33m' export BBlue='\e[1;34m' export BPurple='\e[1;35m' export BCyan='\e[1;36m' export BWhite='\e[1;37m' # Underline export UBlack='\e[4;30m' export URed='\e[4;31m' export UGreen='\e[4;32m' export UYellow='\e[4;33m' export UBlue='\e[4;34m' export UPurple='\e[4;35m' export UCyan='\e[4;36m' export UWhite='\e[4;37m' # Background export On_Black='\e[40m' export On_Red='\e[41m' export On_Green='\e[42m' export On_Yellow='\e[43m' export On_Blue='\e[44m' export On_Purple='\e[45m' export On_Cyan='\e[46m' export On_White='\e[47m' # High Intensity export IBlack='\e[0;90m' export IRed='\e[0;91m' export IGreen='\e[0;92m' export IYellow='\e[0;93m' export IBlue='\e[0;94m' export IPurple='\e[0;95m' export ICyan='\e[0;96m' export IWhite='\e[0;97m' # Bold High Intensity export BIBlack='\e[1;90m' export BIRed='\e[1;91m' export BIGreen='\e[1;92m' export BIYellow='\e[1;93m' export BIBlue='\e[1;94m' export BIPurple='\e[1;95m' export BICyan='\e[1;96m' export BIWhite='\e[1;97m' # High Intensity backgrounds export On_IBlack='\e[0;100m' export On_IRed='\e[0;101m' export On_IGreen='\e[0;102m' export On_IYellow='\e[0;103m' export On_IBlue='\e[0;104m' export On_IPurple='\e[0;105m' export On_ICyan='\e[0;106m' export On_IWhite='\e[0;107m' } export -f set_colors # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Display a message # Usage: disp # Types: # I : info (green) # W : warning (yellow) # E : error (red) # D : debug (cyan) disp() { _disp_print_wrapped() { local prefix="$1" local prefix_len="$2" local target_fd="$3" shift 3 local message="$*" local cols="${COLUMNS:-}" if [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]]; then cols=$(tput cols 2>/dev/null) fi [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]] && cols=80 local indent_len=0 [[ "$prefix_len" =~ ^[0-9]+$ && "$prefix_len" -gt 0 ]] && indent_len=$((prefix_len + 1)) local width=$((cols - indent_len)) (( width < 10 )) && width=10 local wrapped wrapped=$(printf "%s" "$message" | fold -s -w "$width") local first_line=1 local line while IFS= read -r line || [[ -n "$line" ]]; do if (( first_line )); then if [[ -n "$prefix" ]]; then if [[ "$target_fd" -eq 2 ]]; then printf "%b\n" "${prefix} ${line}${RESETCOL}" >&2 else printf "%b\n" "${prefix} ${line}${RESETCOL}" fi else if [[ "$target_fd" -eq 2 ]]; then printf "%b\n" "${line}${RESETCOL}" >&2 else printf "%b\n" "${line}${RESETCOL}" fi fi first_line=0 else if [[ "$target_fd" -eq 2 ]]; then printf "%*s%b\n" "$indent_len" "" "${line}${RESETCOL}" >&2 else printf "%*s%b\n" "$indent_len" "" "${line}${RESETCOL}" fi fi done <<< "$wrapped" } # Handle NO_COLOR: disable colors if set local color_enabled=1 [[ -n $NO_COLOR ]] && color_enabled=0 case ${1^^} in "I") local heads_plain="[ info ]" if [[ $color_enabled -eq 1 ]]; then local heads="[ ${IGreen}info${DEFAULTFG} ]" else local heads="$heads_plain" fi shift [[ -z $QUIET || $QUIET -ne 1 ]] && \ _disp_print_wrapped "$heads" "${#heads_plain}" 1 "$*" ;; "W") local heads_plain="[ Warning ]" if [[ $color_enabled -eq 1 ]]; then local heads="[ ${IYellow}Warning${DEFAULTFG} ]" else local heads="$heads_plain" fi shift _disp_print_wrapped "$heads" "${#heads_plain}" 2 "$*" ;; "E") local heads_plain="[ ERROR ]" if [[ $color_enabled -eq 1 ]]; then local heads="[ ${IRed}ERROR${DEFAULTFG} ]" else local heads="$heads_plain" fi shift _disp_print_wrapped "$heads" "${#heads_plain}" 2 "$*" ;; "D") local heads_plain="[ debug ]" if [[ $color_enabled -eq 1 ]]; then local heads="[ ${ICyan}debug${DEFAULTFG} ]" else local heads="$heads_plain" fi shift [[ -n $DEBUG && $DEBUG -gt 1 ]] && \ _disp_print_wrapped "$heads" "${#heads_plain}" 1 "$*" ;; * ) [[ -z $QUIET || $QUIET -ne 1 ]] && \ _disp_print_wrapped "" 0 1 "$*" ;; esac } export -f disp # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Render Markdown files with terminal formatting # Usage: mdcat [file] mdcat() { _mdcat_style_inline() { local text="$1" local bold_on="" italic_on="" code_on="" style_off="" local link_text_on="" link_url_on="" if [[ -z $NO_COLOR ]]; then bold_on=$'\e[1m' italic_on=$'\e[3m' code_on="${On_IBlack}${BIWhite}" link_text_on=$'\e[4;96m' link_url_on="$IBlack" style_off="$RESETCOL" fi # Apply inline transforms in a safe order: code, links, then emphasis. # This prevents emphasis parsing inside code spans or link URLs. while [[ "$text" =~ \[([^][]+)\]\(([^()]+)\) ]]; do local match="${BASH_REMATCH[0]}" local label="${BASH_REMATCH[1]}" local url="${BASH_REMATCH[2]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" if [[ "$label" == "$url" ]]; then if [[ -z $NO_COLOR ]]; then text="${before}${link_text_on}${label}${style_off}${after}" else text="${before}${label}${after}" fi elif [[ -z $NO_COLOR ]]; then text="${before}${link_text_on}${label}${style_off} ${link_url_on}(${url})${style_off}${after}" else text="${before}${label} (${url})${after}" fi done # Style bare URLs without re-matching the same URL forever. # The prefix capture keeps progress monotonic and prevents freeze loops. # Skip this transformation in NO_COLOR mode. if [[ -z $NO_COLOR ]]; then while [[ "$text" =~ (^|[[:space:]\(])(https?://[^[:space:]\)]+) ]]; do local match="${BASH_REMATCH[0]}" local pre="${BASH_REMATCH[1]}" local url="${BASH_REMATCH[2]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${pre}${link_text_on}${url}${style_off}${after}" done fi while [[ "$text" =~ \`([^\`]+)\` ]]; do local match="${BASH_REMATCH[0]}" local val="${BASH_REMATCH[1]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${code_on}${val}${style_off}${after}" done while [[ "$text" =~ \*\*([^*]+)\*\* ]]; do local match="${BASH_REMATCH[0]}" local val="${BASH_REMATCH[1]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${bold_on}${val}${style_off}${after}" done while [[ "$text" =~ __([^_]+)__ ]]; do local match="${BASH_REMATCH[0]}" local val="${BASH_REMATCH[1]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${bold_on}${val}${style_off}${after}" done while [[ "$text" =~ \*([^*]+)\* ]]; do local match="${BASH_REMATCH[0]}" local val="${BASH_REMATCH[1]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${italic_on}${val}${style_off}${after}" done # Match _italic_ only when underscores are outside word-like identifiers. while [[ "$text" =~ (^|[[:space:][:punct:]])_([^[:space:]_][^_]*[^[:space:]_])_([[:space:][:punct:]]|$) ]]; do local match="${BASH_REMATCH[0]}" local pre="${BASH_REMATCH[1]}" local val="${BASH_REMATCH[2]}" local post="${BASH_REMATCH[3]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${pre}${italic_on}${val}${style_off}${post}${after}" done # Unescape Markdown punctuation escapes, such as \<, \>, \_, and \*. while [[ "$text" =~ \\([[:punct:]]) ]]; do local match="${BASH_REMATCH[0]}" local val="${BASH_REMATCH[1]}" local before="${text%%"$match"*}" local after="${text#*"$match"}" text="${before}${val}${after}" done printf "%s\n" "$text" } _mdcat_print_hr() { local cols="${COLUMNS:-}" if [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]]; then cols=$(tput cols 2>/dev/null) fi [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]] && cols=80 local hr printf -v hr "%*s" "$cols" "" hr="${hr// /-}" if [[ -z $NO_COLOR ]]; then printf "%b%s%b\n" "$IBlack" "$hr" "$RESETCOL" else printf "%s\n" "$hr" fi } _mdcat_print_code_block() { local lang="$1" shift local -a lines=("$@") local cols="${COLUMNS:-}" if [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]]; then cols=$(tput cols 2>/dev/null) fi [[ -z "$cols" || ! "$cols" =~ ^[0-9]+$ || "$cols" -lt 20 ]] && cols=80 local max_inner=$((cols - 4)) (( max_inner < 16 )) && max_inner=16 local width=16 line for line in "${lines[@]}"; do local line_len=${#line} (( line_len > width )) && width=$line_len done (( width > max_inner )) && width=$max_inner local border printf -v border "+-%*s-+" "$width" "" border="${border// /-}" local frame_on="" code_on="" off="" if [[ -z $NO_COLOR ]]; then frame_on="$IBlack" code_on="$BIWhite" off="$RESETCOL" fi printf "%b%s%b\n" "$frame_on" "$border" "$off" if [[ -n "$lang" ]]; then local tag="language: $lang" local wrapped_tag wrapped_tag=$(printf "%s" "$tag" | fold -s -w "$width") while IFS= read -r line || [[ -n "$line" ]]; do printf "%b| %b%-*s%b |%b\n" "$frame_on" "$code_on" "$width" "$line" "$frame_on" "$off" done <<< "$wrapped_tag" printf "%b%s%b\n" "$frame_on" "$border" "$off" fi if [[ ${#lines[@]} -eq 0 ]]; then printf "%b| %b%-*s%b |%b\n" "$frame_on" "$code_on" "$width" "" "$frame_on" "$off" else for line in "${lines[@]}"; do local wrapped wrapped=$(printf "%s" "$line" | fold -s -w "$width") while IFS= read -r wline || [[ -n "$wline" ]]; do printf "%b| %b%-*s%b |%b\n" "$frame_on" "$code_on" "$width" "$wline" "$frame_on" "$off" done <<< "$wrapped" done fi printf "%b%s%b\n" "$frame_on" "$border" "$off" } _mdcat_print_table() { local -a lines=("$@") local -a table_rows=() local -a col_widths=() local i j ncols=0 local sep=$'\x1f' _mdcat_parse_table_row() { local input="$1" local line line=$(printf '%s' "$input" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//') line="${line#|}" line="${line%|}" # Parse Markdown cells using only '|' separators and preserve spaces inside cells. local -a cells=() local cell rest="$line" while :; do if [[ "$rest" == *'|'* ]]; then cell="${rest%%|*}" rest="${rest#*|}" else cell="$rest" rest="" fi cell=$(printf '%s' "$cell" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//') cells+=("$cell") [[ -z "$rest" ]] && break done if [[ ${#cells[@]} -eq 0 ]]; then printf '%s' "" return fi local joined="${cells[0]}" for ((j=1; j<${#cells[@]}; ++j)); do joined+="$sep${cells[j]}" done printf '%s' "$joined" } # Parse header and data rows, skipping the Markdown separator row. # Width is computed from visible text length with ANSI escapes stripped. for ((i=0; i<${#lines[@]}; ++i)); do (( i == 1 )) && continue local parsed parsed=$(_mdcat_parse_table_row "${lines[i]}") table_rows+=("$parsed") local -a row=() IFS="$sep" read -r -a row <<< "$parsed" (( ncols < ${#row[@]} )) && ncols=${#row[@]} for ((j=0; j<${#row[@]}; ++j)); do local vis vis=$(_mdcat_style_inline "${row[j]}") vis=$(printf '%b' "$vis" | sed -E 's/\x1B\[[0-9;]*[mK]//g') local cell_len=${#vis} [[ -z "${col_widths[j]}" ]] && col_widths[j]=0 (( col_widths[j] < cell_len )) && col_widths[j]=$cell_len done done # Ensure all width slots are initialized before drawing borders. for ((j=0; j[[:space:]]?(.*)$ ]]; then local quote="${BASH_REMATCH[2]}" quote=$(_mdcat_style_inline "$quote") if [[ -z $NO_COLOR ]]; then printf "%s%b|%b %b\n" "${BASH_REMATCH[1]}" "$ICyan" "$RESETCOL" "$quote" else printf "%s| %b\n" "${BASH_REMATCH[1]}" "$quote" fi continue fi if [[ "$line" =~ ^([[:space:]]*)[-+*][[:space:]]+(.*)$ ]]; then local item="${BASH_REMATCH[2]}" item=$(_mdcat_style_inline "$item") if [[ -z $NO_COLOR ]]; then printf "%s%b*%b %b\n" "${BASH_REMATCH[1]}" "$IGreen" "$RESETCOL" "$item" else printf "%s* %b\n" "${BASH_REMATCH[1]}" "$item" fi continue fi if [[ "$line" =~ ^([[:space:]]*)([0-9]+)\.[[:space:]]+(.*)$ ]]; then local nitem="${BASH_REMATCH[3]}" nitem=$(_mdcat_style_inline "$nitem") if [[ -z $NO_COLOR ]]; then printf "%s%b%s.%b %b\n" "${BASH_REMATCH[1]}" "$IGreen" "${BASH_REMATCH[2]}" "$RESETCOL" "$nitem" else printf "%s%s. %b\n" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "$nitem" fi continue fi printf "%b\n" "$(_mdcat_style_inline "$line")" done < "${input_file:-/dev/stdin}" if [[ $in_code -eq 1 ]]; then _mdcat_print_code_block "$code_lang" "${code_lines[@]}" fi } export -f mdcat # ------------------------------------------------------------------------------ # Load disp section variables load_conf disp set_colors # EOF