#!/usr/bin/ksh93 ################################################################ function usagemsg_txt2html_k93 { print " Program: txt2html_k93 Convert preformatted text into HTML. Similar in function to the "Grutatxt" Perl module. txt2html_k93 is a shell script function to process text documents in a special markup format (also called Grutatxt), very similar to plain ASCII text. These documents can be easily converted to HTML. Usage: ${1##*/} [-?] [-a left|center|right] [-A top|middle|bottom] [-b color] [-B color] [-d color] [-D color] [-o AI1ai] [-w #] [-C] [-H] [-vV] [file...] Where: Default -a align = Table data cell alignment (left) -A vertAlign = Table data cell vertical alignment (middle) -b color = Table data cell background color (white) -B color = Table header cell background color (lightgrey) -d color = Table data cell font color (black) -D color = Table header cell font color (black) -w # = Table border width (0-99) (1) -o list = Ordered list numbering scheme (1aiAI) -C = Center all tables on the page (left) -H = Mark the first row of all tables as a header row -v = Verbose Mode -V = Very Verbose Mode Author: Dana French (dfrench@mtxia.com) Copyright 2006, All Rights Reserved \"AutoContent\" enabled " return 0 } ################################################################ #### #### Description: #### #### Convert preformatted text into HTML. Similar in #### function to the "Grutatxt" Perl module. txt2html_k93 is #### a shell script function to process text documents in a #### special markup format (also called Grutatxt), very #### similar to plain ASCII text. These documents can be #### easily converted to HTML. #### #### The markup is designed to be fairly intuitive and #### straightforward and can include headings, bold and #### italic text effects, bulleted, numbered and definition #### lists, URLs, function and variable names, preformatted #### text, horizontal separators and tables. Special marks #### can be inserted in the text and a heading-based #### structural index can be obtained from it. #### #### Assumptions: #### #### Dependencies: #### #### Products: #### #### Configured Usage: #### #### Details: #### ################################################################ function txt2html_k93 { typeset VERSION="1.0" typeset TRUE="0" typeset FALSE="1" typeset VERBOSE="${FALSE}" typeset VERYVERB="${FALSE}" typeset ULFLAG="${FALSE}" typeset DLFLAG="${FALSE}" typeset PREFLAG="${FALSE}" typeset CITEFLAG="${FALSE}" typeset CODEFLAG="${FALSE}" typeset HTMLFLAG="${FALSE}" typeset SIZ_TBLBDR="1" typeset TBLHDRROW="${FALSE}" typeset TBLCENTER="${FALSE}" typeset OLSCHEME="1aiAI" typeset -l CLR_TDBG="white" typeset -l CLR_THBG="lightgrey" typeset -l CLR_TDFG="black" typeset -l CLR_THFG="darkblue" typeset -l TBLALIGN="left" typeset -l TBLVALIGN="middle" typeset SPACES=$'\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\ \x20\x20\x20\x20\x20\x20\x20\x20\x20\x20' while getopts ":vVa:A:b:B:d:D:o:w#CH" OPTION do case "${OPTION}" in 'a') typeset -l TBLALIGN="${OPTARG}";; 'A') typeset -l TBLVALIGN="${OPTARG}";; 'b') typeset -l CLR_TDBG="${OPTARG}";; 'B') typeset -l CLR_THBG="${OPTARG}";; 'd') typeset -l CLR_TDFG="${OPTARG}";; 'D') typeset -l CLR_THFG="${OPTARG}";; 'w') SIZ_TBLBDR="${OPTARG}";; 'o') OLSCHEME="${OPTARG}";; 'C') TBLCENTER="${TRUE}";; 'H') TBLHDRROW="${TRUE}";; 'v') VERBOSE="${TRUE}";; 'V') VERYVERB="${TRUE}";; [?:#]) usagemsg_txt2html_k93 "${0}" && return 1 ;; esac done shift $(( ${OPTIND} - 1 )) (( VERYVERB == TRUE )) && set -x ################################################################ typeset OLTYPE="0" typeset ICNT=0 for (( ICNT=0; ICNT<=10; ++ICNT )) do OLTYPE="${OLTYPE}${OLSCHEME}" done ################################################################ #### Checking command line options and arguments for improper values. trap "usagemsg_txt2html_k93 ${0}" EXIT if [[ "${TBLALIGN}" != @(left|center|right) ]] then print -u 2 -- "# ERROR: Invalid table cell alignment value: ${TBLALIGN}" return 2 fi if [[ "${TBLVALIGN}" != @(top|middle|bottom) ]] then print -u 2 -- "# ERROR: Invalid table cell verticle alignment value: ${TBLVALIGN}" return 3 fi if [[ "_${SIZ_TBLBDR}" == "_" ]] || (( SIZ_TBLBDR < 0 )) || (( SIZ_TBLBDR > 99 )) then print -u 2 -- "# ERROR: Invalid table border width value: ${SIZ_TBLBDR}" return 4 fi trap "-" EXIT ################################################################ typeset STDIN="${1:+${FALSE}}" [[ "_${1}" == '_-' ]] && STDIN="${TRUE}" STDIN="${STDIN:-${TRUE}}" #### Read in the data either from STDIN or one or more files if (( STDIN == TRUE )) then set -- - fi #### Loop through each file name specified on the command line #### or standard input if no file names for FNAME in "${@}" do if (( STDIN == FALSE )) && [[ ! -e "${FNAME}" ]] then print -u 2 "${0##*/}: ${FNAME}: No such file or directory" continue fi #### Loop through each line of the current input stream or file and #### determine the HTML type to assign to each line. The HTML type #### will be used later to insert the appropriate HTML tags for #### rendering the line. unset TXTARRY LCNT="0" cat_k93 -- "${FNAME}" | while IFS="" read -r -- LINE do #### Save each line of the input stream to an array. This array will #### be used to display the original text and to associate various #### parameters with each array element position. TXTARRY[LCNT]="${LINE}" #### If the line is blank, mark the line as a "paragraph" separator line if [[ "_${LINE}" == "_" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Paragraph" ULFLAG="${FALSE}" TAGTYPE[LCNT]="P" fi #### If the line begins with 4 or more hash (#) characters, mark it #### as a horizontal rule separator line if [[ "_${LINE}" == _\#\#\#\#*(\#) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Horizontal Rule" TAGTYPE[LCNT]="HR" fi #### If the first non-blank character of the line is an equal sign, dash, #### or tilde, mark the previous line (if non-blank) as a header line. if (( LCNT > 0 )) then if [[ "_${LINE}" == _*([[:blank:]])+([=]) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Header 1" TAGTYPE[LCNT-1]="H1" fi if [[ "_${LINE}" == _*([[:blank:]])+([-]) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Header 2" TAGTYPE[LCNT-1]="H2" fi if [[ "_${LINE}" == _*([[:blank:]])+([\~]) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Header 3" TAGTYPE[LCNT-1]="H3" fi if [[ "_${LINE}" == _*([[:blank:]])+([=\~-]) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# DELETE" TAGTYPE[LCNT]="DELETE" fi fi #### #### Imbedded HTML #### if (( HTMLFLAG == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "# Imbedded HTML" TAGTYPE[LCNT]="HTML" fi if [[ "_${LINE}" == _*@('<<'|'>>')* ]] then if [[ "_${LINE}" == _*'<<'* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Begin Imbedded HTML" HTMLFLAG="${TRUE}" TAGTYPE[LCNT]="HTML" fi if [[ "_${LINE}" == _*'>>'* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# End Imbedded HTML" HTMLFLAG="${FALSE}" TAGTYPE[LCNT]="HTML" fi fi #### #### Preformatted Text segments #### if (( ULFLAG == FALSE )) && (( DLFLAG == FALSE )) && [[ "_${LINE}" == _+([[:blank:]])[!*\#\"]* ]] then TAGTYPE[LCNT]="PRE" if [[ "${TAGTYPE[LCNT-1]}" == "CITE" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# CITE" TAGTYPE[LCNT]="CITE" else (( VERBOSE == TRUE )) && print -u 2 -- "# Preformatted" fi fi #### #### Cite Text segments #### if [[ "_${LINE}" == _+([[:blank:]])\"* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Begin CITE" TAGTYPE[LCNT]="CITE" fi #### #### Programming Code segments #### if [[ "_${LINE}" == _*([[:blank:]])\$* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# CODE " TAGTYPE[LCNT]="CODE" fi #### #### Unordered/Ordered Lists #### if (( ULFLAG == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "# List line" TAGTYPE[LCNT]="LI" LSTTYPE[LCNT]="${TAGTYPE[LCNT-1]}" ULSPACE[LCNT]="${ULSPACE[LCNT-1]:-0}" ULLEVEL[LCNT]="${ULLEVEL[LCNT-1]:-0}" fi if [[ "_${LINE}" == _*([[:blank:]])[*\#][!-]* ]] then ULFLAG="${TRUE}" #### *([[:blank:]])\*[!\*]* = Zero or more blank characters, #### followed by literal asterisk (*), #### followed by any single non-asterisk(*) character, #### followed by zero or more characters if [[ "_${LINE}" == _*([[:blank:]])[*][!*]* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Unordered list" TAGTYPE[LCNT]="UL" fi if [[ "_${LINE}" == _*([[:blank:]])\#[!\#]* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Ordered list" TAGTYPE[LCNT]="OL" fi TMPNBR="${LINE%%[!$' \t\n']*}" ULSPACE[LCNT]="${#TMPNBR}" ULDELTA="0" (( ${ULSPACE[LCNT]} > ${ULSPACE[LCNT-1]:-0} )) && ULDELTA="+1" (( ${ULSPACE[LCNT]} < ${ULSPACE[LCNT-1]:-0} )) && ULDELTA="-1" ULLEVEL[LCNT]="$(( ${ULLEVEL[LCNT-1]:-0} + ${ULDELTA} ))" LSTTYPE[LCNT]="${TAGTYPE[LCNT]}" fi #### #### Definition Lists #### if (( DLFLAG == TRUE )) && [[ "${TAGTYPE[LCNT-1]}" == @(DL|DD) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Definition list data" TAGTYPE[LCNT]="DD" fi if [[ "_${LINE}" == _*([[:blank:]])[*]*:* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Begin Definition list" TAGTYPE[LCNT]="DL" DLFLAG="${TRUE}" ULFLAG="${FALSE}" LSTTYPE[LCNT]="" ULSPACE[LCNT]="" ULLEVEL[LCNT]="" fi if (( DLFLAG == TRUE )) && [[ "${TAGTYPE[LCNT]}" != @(DL|DD) ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# End Definition list" DLFLAG="${FALSE}" fi #### #### Tables and rows #### if [[ "_${LINE}" == _*([[:blank:]])[+]-* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Table data cell border" TAGTYPE[LCNT]="TABLETD" fi if [[ "_${LINE}" == _*([[:blank:]])[*]-* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Table header cell border" TAGTYPE[LCNT]="TABLETH" fi if [[ "_${LINE}" == _*([[:blank:]])\|* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Table data cell" TAGTYPE[LCNT]="ROW" if [[ "${TAGTYPE[LCNT-1]}" == @(TABLETH|HEADER) ]] then TAGTYPE[LCNT]="HEADER" fi fi if [[ "_${LINE}" == _*([[:blank:]])\|\!* ]] then (( VERBOSE == TRUE )) && print -u 2 -- "# Table header cell" TAGTYPE[LCNT]="HEADER" fi #### Array element counter for lines read in (( ++LCNT )) done ################################################################ TABLEFLAG="${FALSE}" print -- "
" for IDX in "${!TXTARRY[@]}" do TEXTLINE="${TXTARRY[IDX]}" #### #### Imbedded HTML #### if [[ "${TAGTYPE[IDX]}" == "HTML" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "#01 ${TAGTYPE[IDX]}" TEXTLINE="${TEXTLINE//'<<'/}" TEXTLINE="${TEXTLINE//'>>'/}" fi #### #### CODE HTML TAG output #### if [[ "${TAGTYPE[IDX]}" == "CODE" ]] && (( CODEFLAG == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#02 ${TAGTYPE[IDX]}" print -- "\n
<${TAGTYPE[IDX]}>" CODEFLAG="${FALSE}" fi #### #### CITE Text HTML TAG output #### if [[ "${TAGTYPE[IDX]}" == "CITE" ]] && (( CITEFLAG == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#05 ${TAGTYPE[IDX]}" print -- "" CITEFLAG="${TRUE}" fi if [[ "${TAGTYPE[IDX]}" != "CITE" ]] && (( CITEFLAG == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#06 ${TAGTYPE[IDX]}" print -- "" CITEFLAG="${FALSE}" fi #### #### Preformatted Text HTML TAG output #### if [[ "${TAGTYPE[IDX]}" == "PRE" ]] && (( PREFLAG == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#07 ${TAGTYPE[IDX]}" print -- "" CODEFLAG="${TRUE}" fi if [[ "${TAGTYPE[IDX]}" == "CODE" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "#03 ${TAGTYPE[IDX]}" TEXTLINE="${TEXTLINE/#*([[:blank:]])\$*([[:blank:]])}" fi if [[ "${TAGTYPE[IDX]}" != "CODE" ]] && (( CODEFLAG == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#04 ${TAGTYPE[IDX]}" print -- "${TAGTYPE[IDX]}>
" PREFLAG="${TRUE}" fi if [[ "${TAGTYPE[IDX]}" != "PRE" ]] && (( PREFLAG == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#08 ${TAGTYPE[IDX]}" print -- "" PREFLAG="${FALSE}" fi #### #### Horizontal Rule HTML TAG output #### if [[ "${TAGTYPE[IDX]}" == "HR" ]] then (( VERBOSE == TRUE )) && print -u 2 -- "#09 ${TAGTYPE[IDX]}" print -- "<${TAGTYPE[IDX]}>" continue fi #### #### Table and Row HTML TAG output #### if [[ "${TAGTYPE[IDX]}" == @(TABLETH|TABLETD) ]] then if (( TABLEFLAG == FALSE )) then if (( TBLCENTER == TRUE )) then (( VERBOSE == TRUE )) && print -u 2 -- "#10 ${TAGTYPE[IDX]}" print -- "