Updated 2013-12-31 14:02:09 by WJG

WJG <27/06/12> In a previous post I included an example of how to create formatted title strings using the selection in a Tk text widget. The following example shows how to create formatted titles using arbitrary strings.
#---------------
# test-capitalize.tcl
#---------------
#!/bin/sh
#\
exec tclsh "$0" "$@"

#---------------
# capitalize str1, excepting those words given in str2
#---------------
# args:
#        str1        string to format
#        str2        exceptions list
# returns:
#        Reformatted string

set exceptions "a an and are as at be for had has have have if in is of on or that the this to with you your" 

proc capitalize {str1 {str2 {}} } {
        
        # parse each word in str1 
        foreach word  $str1 {
                # is it found in the exception list str2, if not capitalize
                if { [lsearch -exact $str2 $word] == -1 }  {
                        set word [string totitle $word]
                        } 
                lappend text $word
                }
                
        # make sure initial is always capital
        return [string totitle $text 0 0]
}



# demo --------
puts [capitalize "the development of perfection -the interiorization of buddhist ritual in the eighth and ninth centuries" $exceptions]
puts [capitalize "vasubandhu's philosophical critique of the vatsiputriya's theory of persons (II)" $exceptions]

# the (II) will not be capitalized, so add this to the exceptions list
lappend exceptions (II)
puts [capitalize "vasubandhu's philosophical critique of the vatsiputriya's theory of persons (II)" $exceptions]

# capitalize the first letter of all words
puts [capitalize "vasubandhu's philosophical critique of the vatsiputriya's theory of persons (II)"]