From ad45de11c483216c1a280ef5cbbe16fe81e02a56 Mon Sep 17 00:00:00 2001 From: Alain Reguera Delgado Date: Oct 08 2010 03:45:07 +0000 Subject: Update html_updateHeadings.sh - In this commit the table of content is updated using headings but nesting isn't as expected yet. We need to change the way to parse, instead of making a full search for al h1, then h2, later h3 and so on, we need to look for all h1 and headings inside it but until finding the next heading of the same level. The same thing repeats for h2, h3, h4, and h5 recursively. We consider h6 the last possibility so don't look for h7. For example: inside h1 can be: h2, h3, h4, h5, h6 inside h2 can be: h3, h4, h5, h6 inside h3 can be: h4, h5, h6 inside h4 can be: h5, h6 inside h5 can be: h6 --- diff --git a/Scripts/Bash/Functions/Html/html_updateHeadings.sh b/Scripts/Bash/Functions/Html/html_updateHeadings.sh index a5230a1..2622a36 100644 --- a/Scripts/Bash/Functions/Html/html_updateHeadings.sh +++ b/Scripts/Bash/Functions/Html/html_updateHeadings.sh @@ -1,33 +1,34 @@ #!/bin/bash # # html_updateHeadings.sh -- This function transforms html headings to -# create the page table of content headings as reference. Multiple -# heading levels are supported using nested lists. Use this function -# over html files inside the repository to standardize their headings. +# to make them accessible (e.g., through a table of contents). # # - In order for this function to work, you need to put headings in -# just one line and they must have the following format: +# just one line and they must have the following formats: # -#

Title

+#

Title

+#

Title

+#

Title

# -# Here h1 alternates between 1 and 6. Closing tag must be present -# and match the one opening. The value of option is -# the md5sum of page location, the 'head-' string plus heading -# title. If heading title or page location changes, does the option value changes too. This idea is similar to that -# one used by MoinMoin wiki. +# In the above examples, h1 alternates from h1 to h6. Closing tag +# must be present and match the one opening. The value of and options are the md5sum of page +# location, plus the 'head-' string, plus the heading string. If +# heading title or page location changes, the values of and options will change too. # -# - This function looks for
...
specification -# inside your page and, if present, replace the content inside -# with the link list o headinds. +# - 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 created. +# 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 replace everything in-between +# closing div in your html code and replaces everything in-between # with the table of content. # # Copyright (C) 2009-2010 Alain Reguera Delgado @@ -56,12 +57,14 @@ function html_updateHeadings { # Define variables as local to avoid conflicts outside. local FILES='' local LEVEL=1 + local COUNT=0 local HEADING='' local PATTERN='' local -a FIRST - local -a NAME local -a FINAL local -a TITLE + local -a MD5SM + local -a OPTNS # Define list of html files to process using option value as # reference. @@ -105,6 +108,9 @@ function html_updateHeadings { 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 @@ -132,6 +138,15 @@ function html_updateHeadings { done + # Define list structure using list entries for each + # heading level. + if [[ ${#LIENTRIES[*]} -gt 0 ]];then + LISTS[$(($LEVEL-1))]="${LIENTRIES[*]}" + fi + + # Clean up list entries of heading. + unset LIENTRIES + # Reset heading counter. COUNT=0 @@ -140,8 +155,39 @@ function html_updateHeadings { done + # 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 table of contents html structure. + egrep '
    ' $FILE > /dev/null \ + && sed -i -r '\%
    %,\%
    %c'"$TOC" $FILE + + # Give format to table of contents html structure. + sed -i -r '/
    /{ + s!

    !\n

    !; + s!

    !\n!; + s!!\n!g; + s!
      !
        \n!g; + s!
    !\n
    !; + }' $FILE + # Reset level counter. - LEVEL=0 + LEVEL=1 + + # Clean up level specific list entries. + unset LISTS done