diff --git a/Scripts/Bash/Functions/Html/html_updateHeadings.sh b/Scripts/Bash/Functions/Html/html_updateHeadings.sh index 2622a36..c773215 100644 --- a/Scripts/Bash/Functions/Html/html_updateHeadings.sh +++ b/Scripts/Bash/Functions/Html/html_updateHeadings.sh @@ -17,20 +17,6 @@ # heading title or page location changes, the values of and options will change too. # -# - When creating the table of contents, this function looks for -#
...
specification inside your page and, -# if present, replaces the content inside with the list of heading -# links. -# -# - If
...
specification is present on the -# page it is updated with headings links. Otherwise only heading -# links are updated. -# -# - If
...
specification is malformed (e.g., -# you forgot the closing tag), this function will look the next -# closing div in your html code and replaces everything in-between -# with the table of content. -# # Copyright (C) 2009-2010 Alain Reguera Delgado # # This program is free software; you can redistribute it and/or modify @@ -56,15 +42,15 @@ function html_updateHeadings { # Define variables as local to avoid conflicts outside. local FILES='' - local LEVEL=1 local COUNT=0 - local HEADING='' + local PREVCOUNT=0 local PATTERN='' - local -a FIRST local -a FINAL local -a TITLE local -a MD5SM local -a OPTNS + local -a LEVEL + local -a PARENT # Define list of html files to process using option value as # reference. @@ -83,111 +69,73 @@ function html_updateHeadings { fi # Output action message. - cli_printMessage $FILE 'AsUpdatingLine' - - # Define how many heading levels this function works with. - until [[ $LEVEL -eq 7 ]]; do - - # Define translation pattern. Use parenthisis to save - # html option name, option value, and heading title. - PATTERN="(]>)(.*[^<])
" - - # Define list of headings to process. When building the - # heading, it is required to change spaces characters from - # its current output form to something different (e.g., - # its \x040 octal alternative). This is required because - # the space character is used as egrep default field - # separator and spaces can be present inside heading - # strings we don't want to separate. - for HEADING in $(egrep "$PATTERN" $FILE \ - | sed -r -e 's!^[[:space:]]+!!' -e "s! !\x040!g");do - - # Define initial heading information. - FIRST[$COUNT]=$(echo $HEADING | sed -r "s!\x040! !g") - TITLE[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\2!") - MD5SM[$COUNT]=$(echo "${FILE}${TITLE[$COUNT]}" | md5sum | sed -r 's![[:space:]]+-$!!') - OPTNS[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\1!") - - # Define entry list used to show table of contents. - LIENTRIES[$COUNT]='
  • '${TITLE[$COUNT]}'
  • ' - - # Transform heading information using initial heading - # information as reference. - if [[ ${OPTNS[$COUNT]} =~ '^$' ]];then - - OPTNS[$COUNT]='' - - elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then - - OPTNS[$COUNT]='' - - elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then - - OPTNS[$COUNT]='' - - fi - - FINAL[$COUNT]=''${OPTNS[$COUNT]}${TITLE[$COUNT]}'' - - # Update heading information using transformed heading - # information. - sed -i -r "s!${FIRST[$COUNT]}!${FINAL[$COUNT]}!" $FILE - - # Increase heading counter. - COUNT=$(($COUNT + 1)) - - done + #cli_printMessage $FILE 'AsUpdatingLine' + + # Define html heading regular expression. Use parenthisis to save + # html option name, option value, and heading title. + PATTERN="(]>)(.*[^<])" + + # Define list of headings to process. When building the + # heading, it is required to change spaces characters from its + # current output form to something different (e.g., its \x040 + # octal alternative). This is required because the space + # character is used as egrep default field separator and + # spaces can be present inside heading strings we don't want + # to separate. + for HEADING in $(egrep "$PATTERN" $FILE \ + | sed -r -e 's!^[[:space:]]+!!' -e "s! !\x040!g");do + + # Define previous counter value using current counter + # value as reference. + if [[ $COUNT -ne 0 ]];then + PREVCOUNT=$(($COUNT-1)) + fi - # Define list structure using list entries for each - # heading level. - if [[ ${#LIENTRIES[*]} -gt 0 ]];then - LISTS[$(($LEVEL-1))]="${LIENTRIES[*]}" + # Define initial heading information. + FIRST[$COUNT]=$(echo $HEADING | sed -r "s!\x040! !g") + TITLE[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\3!") + MD5SM[$COUNT]=$(echo "${FILE}${FIRST[$COUNT]}" | md5sum | sed -r 's![[:space:]]+-$!!') + OPTNS[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\2!") + LEVEL[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\1!") + PARENT[$COUNT]=${LEVEL[$PREVCOUNT]} + + # Transform heading information using initial heading + # information as reference. + if [[ ${OPTNS[$COUNT]} =~ '^$' ]];then + OPTNS[$COUNT]='' + elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then + OPTNS[$COUNT]='' + elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then + OPTNS[$COUNT]='' fi - - # Clean up list entries of heading. - unset LIENTRIES - # Reset heading counter. - COUNT=0 + # Build final html heading structure. + FINAL[$COUNT]=''${OPTNS[$COUNT]}${TITLE[$COUNT]}'' - # Increase level counter. - LEVEL=$(($LEVEL + 1)) + # Build html heading link structure. + LINK[$COUNT]=''${TITLE[$COUNT]}'' - done + # Build table of contents entry with numerical + # identifications. + TOCENTRIES[$COUNT]="$COUNT:${LEVEL[$COUNT]}:${PARENT[$COUNT]}:${LINK[$COUNT]}" - # Define table of contents using specific list entries. - TOC=$(for LIST in "${LISTS[@]}";do - if [[ $LIST != '' ]];then - echo $LIST | sed -r 's!$!' - fi - done) - if [[ $TOC != '' ]];then - TOC=$(echo -e '

    Table of contents

      '${TOC}'
    ') - fi + # Update heading information using the first and last + # heading structures. + #sed -i -r "s!${FIRST[$COUNT]}!${FINAL[$COUNT]}!" $FILE - # Update table of contents html structure. - egrep '
    ' $FILE > /dev/null \ - && sed -i -r '\%
    %,\%
    %c'"$TOC" $FILE + # Increase heading counter. + COUNT=$(($COUNT + 1)) - # Give format to table of contents html structure. - sed -i -r '/
    /{ - s!

    !\n

    !; - s!

    !\n!; - s!!\n!g; - s!
      !
        \n!g; - s!
    !\n
    !; - }' $FILE + done - # Reset level counter. - LEVEL=1 + # Reset heading counter. + COUNT=0 - # Clean up level specific list entries. - unset LISTS + # Use awk to build the table of content. + for TOC in "${TOCENTRIES[@]}";do + echo $TOC + done \ + | awk 'BEGIN{FS=":"}{printf "%s\n", $4}' done