Blame Scripts/Bash/Functions/Html/html_updateHeadings.sh

9f1608
#!/bin/bash
9f1608
#
9f1608
# html_updateHeadings.sh -- This function transforms html headings to
9f1608
# create the page table of content headings as reference. Multiple
9f1608
# heading levels are supported using nested lists. Use this function
9f1608
# over html files inside the repository to standardize their headings. 
9f1608
#
ffdd74
#   - In order for this function to work, you need to put headings in
ffdd74
#     just one line and they must have the following format:
ffdd74
#
ffdd74
#       

Title

ffdd74
#
ffdd74
#     Here h1 alternates between 1 and 6. Closing tag must be present
ffdd74
#     and match the one opening. The value of  option is
ffdd74
#     the md5sum of page location, the 'head-' string plus heading
ffdd74
#     title. If heading title or page location changes, does the 
ffdd74
#     name=""> option value changes too. This idea is similar to that
ffdd74
#     one used by MoinMoin wiki.
ffdd74
#
9f1608
#   - This function looks for 
...
specification
9f1608
#     inside your page and, if present, replace the content inside
9f1608
#     with the link list o headinds. 
9f1608
#
9f1608
#   - If 
...
specification is present on the
9f1608
#     page it is updated with headings links. Otherwise only heading
9f1608
#     links are created. 
9f1608
#
9f1608
#   - If 
...
specification is malformed (e.g.,
9f1608
#     you forgot the closing tag), this function will look the next
9f1608
#     closing div in your html code and replace everything in-between
9f1608
#     with the table of content.
9f1608
#
9f1608
# Copyright (C) 2009-2010 Alain Reguera Delgado
9f1608
# 
9f1608
# This program is free software; you can redistribute it and/or modify
9f1608
# it under the terms of the GNU General Public License as published by
9f1608
# the Free Software Foundation; either version 2 of the License, or
9f1608
# (at your option) any later version.
9f1608
# 
9f1608
# This program is distributed in the hope that it will be useful, but
9f1608
# WITHOUT ANY WARRANTY; without even the implied warranty of
9f1608
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9f1608
# General Public License for more details.
9f1608
#
9f1608
# You should have received a copy of the GNU General Public License
9f1608
# along with this program; if not, write to the Free Software
9f1608
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
9f1608
# USA.
9f1608
# 
9f1608
# ----------------------------------------------------------------------
9f1608
# $Id$
9f1608
# ----------------------------------------------------------------------
9f1608
9f1608
function html_updateHeadings {
9f1608
ffdd74
    # Define variables as local to avoid conflicts outside.
ffdd74
    local FILES=''
ffdd74
    local LEVEL=1
ffdd74
    local HEADING=''
ffdd74
    local PATTERN=''
ffdd74
    local -a FIRST
ffdd74
    local -a NAME
ffdd74
    local -a FINAL
ffdd74
    local -a TITLE
ffdd74
ffdd74
    # Define list of html files to process using option value as
ffdd74
    # reference. 
ffdd74
    if [[ -d $OPTIONVAL ]];then
ffdd74
        FILES=$(find $OPTIONVAL -regextype posix-egrep -type f -regex '.*/*.(html|htm)$')
ffdd74
    elif [[ -f $OPTIONVAL ]];then
ffdd74
        FILES=$OPTIONVAL
ffdd74
    fi
ffdd74
ffdd74
    for FILE in $FILES;do
ffdd74
ffdd74
        # Verify list of html files. Are they really html files? If
ffdd74
        # they don't, continue with the next one in the list.
ffdd74
        if [[ ! $(file --brief $FILE) =~ '^(XHTML|HTML|XML)' ]];then
ffdd74
            continue
ffdd74
        fi
ffdd74
ffdd74
        # Output action message.
ffdd74
        cli_printMessage $FILE 'AsUpdatingLine'
ffdd74
ffdd74
        # Define how many heading levels this function works with.
ffdd74
        until [[ $LEVEL -eq 7 ]]; do
ffdd74
ffdd74
            # Define translation pattern. Use parenthisis to save
ffdd74
            # html option name, option value, and heading title.
ffdd74
            PATTERN="<h$LEVEL>(<a.*[^\>]>)(.*[^<])</h$LEVEL>"
ffdd74
ffdd74
            # Define list of headings to process. When building the
ffdd74
            # heading, it is required to change spaces characters from
ffdd74
            # its current output form to something different (e.g.,
ffdd74
            # its \x040 octal alternative). This is required because
ffdd74
            # the space character is used as egrep default field
ffdd74
            # separator and spaces can be present inside heading
ffdd74
            # strings we don't want to separate.
ffdd74
            for HEADING in $(egrep "$PATTERN" $FILE \
ffdd74
                | sed -r -e 's!^[[:space:]]+!!' -e "s! !\x040!g");do
ffdd74
ffdd74
                # Define initial heading information.
ffdd74
                FIRST[$COUNT]=$(echo $HEADING | sed -r "s!\x040! !g")
ffdd74
                TITLE[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\2!")
ffdd74
                MD5SM[$COUNT]=$(echo "${FILE}${TITLE[$COUNT]}" | md5sum | sed -r 's![[:space:]]+-$!!')
ffdd74
                OPTNS[$COUNT]=$(echo ${FIRST[$COUNT]} | sed -r "s!$PATTERN!\1!")
ffdd74
ffdd74
                # Transform heading information using initial heading
ffdd74
                # information as reference.
ffdd74
                if [[ ${OPTNS[$COUNT]} =~ '^$' ]];then
ffdd74
ffdd74
                    OPTNS[$COUNT]=''
ffdd74
ffdd74
                elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then 
ffdd74
ffdd74
                    OPTNS[$COUNT]=''
ffdd74
ffdd74
                elif [[ ${OPTNS[$COUNT]} =~ '^$' ]];then
ffdd74
ffdd74
                    OPTNS[$COUNT]=''
ffdd74
ffdd74
                fi
ffdd74
ffdd74
                FINAL[$COUNT]='<h'$LEVEL'>'${OPTNS[$COUNT]}${TITLE[$COUNT]}'</h'$LEVEL'>'
ffdd74
ffdd74
                # Update heading information using transformed heading
ffdd74
                # information.
ffdd74
                sed -i -r "s!${FIRST[$COUNT]}!${FINAL[$COUNT]}!" $FILE
ffdd74
ffdd74
                # Increase heading counter.
ffdd74
                COUNT=$(($COUNT + 1))
ffdd74
ffdd74
            done
ffdd74
ffdd74
            # Reset heading counter.
ffdd74
            COUNT=0
ffdd74
ffdd74
            # Increase level counter. 
ffdd74
            LEVEL=$(($LEVEL + 1))
ffdd74
ffdd74
        done
ffdd74
ffdd74
        # Reset level counter.
ffdd74
        LEVEL=0
ffdd74
ffdd74
    done
ffdd74
9f1608
}