| #!/bin/bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function xhtml_doToc { |
| |
| |
| local COUNT=0 |
| local PREVCOUNT=0 |
| local -a FINAL |
| local -a TITLE |
| local -a MD5SM |
| local -a OPTNS |
| local -a CLASS |
| local -a LEVEL |
| local -a PARENT |
| local -a TOCENTRIES |
| local -a LINK |
| |
| |
| |
| local TOC_CONFIG=${TUNEUP_BACKEND_CONFIG}/toc.awk |
| |
| |
| cli_checkFiles ${TOC_CONFIG} |
| |
| |
| |
| local PATTERN='<h([1-6])(.*)>(<a.*[^\>]>)(.*[^<])</a></h[1-6]>' |
| |
| |
| |
| if [[ ! $(file --brief $FILE) =~ '^(XHTML|HTML|XML)' ]];then |
| continue |
| fi |
| |
| |
| |
| |
| |
| |
| |
| for HEADING in $(egrep "$PATTERN" $FILE \ |
| | sed -r -e 's!^[[:space:]]+!!' -e "s! !\\\040!g");do |
| |
| |
| |
| if [[ $COUNT -ne 0 ]];then |
| PREVCOUNT=$(($COUNT-1)) |
| fi |
| |
| |
| FIRST[$COUNT]=$(echo $HEADING | sed -r "s!\\\040! !g") |
| TITLE[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\4!") |
| MD5SM[$COUNT]=$(echo "${FILE}${FIRST[$COUNT]}" | md5sum | sed -r 's![[:space:]]+-$!!') |
| OPTNS[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\3!") |
| CLASS[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\2!") |
| LEVEL[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\1!") |
| PARENT[$COUNT]=${LEVEL[$PREVCOUNT]} |
| |
| |
| |
| if [[ ${OPTNS[$COUNT]} =~ '^<a (href|name)="(.*)" (href|name)="(.*)">$' ]];then |
| OPTNS[$COUNT]='<a name="head-'${MD5SM[$COUNT]}'" href="#head-'${MD5SM[$COUNT]}'">' |
| elif [[ ${OPTNS[$COUNT]} =~ '^<a name="(.*)">$' ]];then |
| OPTNS[$COUNT]='<a name="head-'${MD5SM[$COUNT]}'">' |
| elif [[ ${OPTNS[$COUNT]} =~ '^<a href="(.*)">$' ]];then |
| OPTNS[$COUNT]='<a href="#head-'${MD5SM[$COUNT]}'">' |
| fi |
| |
| |
| FINAL[$COUNT]='<h'${LEVEL[$COUNT]}${CLASS[$COUNT]}'>'${OPTNS[$COUNT]}${TITLE[$COUNT]}'</a></h'${LEVEL[$COUNT]}'>' |
| |
| |
| |
| LINK[$COUNT]='<a href="#head-'${MD5SM[$COUNT]}'">'${TITLE[$COUNT]}'</a>' |
| |
| |
| |
| |
| |
| TOCENTRIES[$COUNT]="$COUNT:${LEVEL[$COUNT]}:${PARENT[$COUNT]}:${LINK[$COUNT]}" |
| |
| |
| |
| sed -i -r "s!${FIRST[$COUNT]}!${FINAL[$COUNT]}!" $FILE |
| |
| |
| COUNT=$(($COUNT + 1)) |
| |
| done |
| |
| |
| |
| |
| |
| |
| |
| TOC=$(echo '<div class="toc">' |
| echo "<p>`gettext "Table of contents"`</p>" |
| for TOCENTRY in "${TOCENTRIES[@]}";do |
| echo $TOCENTRY |
| done \ |
| | awk -f ${TOC_CONFIG}) |
| |
| |
| |
| sed -i -r '/<div class="toc">[^<\/div].*<\/div>/c'"$(echo -e $TOC)" $FILE |
| |
| |
| unset FINAL |
| unset TITLE |
| unset MD5SM |
| unset OPTNS |
| unset CLASS |
| unset LEVEL |
| unset PARENT |
| unset TOCENTRIES |
| unset LINK |
| |
| } |