# templateDokuJob creates a documentation for the templates in an Infopark CMS Fiona (NPS).
# The script was written by Karina Taubert (kt@jetaido.com) and 
# is copyright of Jetaido GmbH. You may use the script as you see fit.
# If you want to change things in this script, feel free to do so but please let
# us know.
# Only released versions of templates are considered.
# You may want to start you template with a comment text of the form
#    <npsgui><!-- here goes the explanation what the template does --></npsgui>
# parameters:
#    path of a file that is used as the starting point of template calculation
#    and name of your mastertemplate, usually that would be 'mastertemplate'
# return value
#    string containing an html documentation, you best put that into a file

proc templateDokuJob {file mt} {
    # looking for file's valid mastertemplate
    set fileType [obj withPath $file get objType]
    switch -- $fileType {
        publication {
        }
        document {
            set file [obj withPath $file get parent.path]
        }
        default {
            return "The sample file must be a folder or a standard document. Please correct the configuration!"
        }
    }

    # assembling template structure and generating documentation
    set content "<p>To update this documentation please run the job that creates it (templateDokuJob).<br />\n"
    append content "Please note that only layouts directly or indirectly used by the mastertemplate are shown. Main Content Layouts and unused layouts may be missing.</p>\n"
    array set templatesA {}
    array set templatesA [_parseTemplates $mt $file]
    set list [array names templatesA]
    set list [lsort $list]
    foreach l $list {
        append content "<a name=\"$l\"></a><h3>$l</h3>\n"
        foreach {desc templs} $templatesA($l) {}
        append content "<p>$desc<br />"
        if {[llength $templs] > 0} {
            append content "It calls the following templates:</p>\n<ul>\n"
            foreach j $templs {
                append content "<li><a href=\"external:#$j\">$j</a></li>\n"
            }
            append content "</ul>\n"
        }
    }
    return $content
}

safeInterp alias templateDokuJob templateDokuJob


proc _searchTemplate {tname file} {
    set list [obj withPath $file get children]
    foreach i $list {
        foreach {objType name isReleased} [obj withId $i mget objType name isReleased] {}
        if {($objType == "template") &&
            ($name == $tname) && ($isReleased == "1")} {
                return [obj withId $i get path]
        }
    }
    if {$file != "/"} {
        return [_searchTemplate $tname [obj withPath $file get parent.path]]
    } else {
        return false
    }
}

proc _parseTemplates {tname file} {
    # returns the comment of a template and 
    # a list of the templates that it uses
    upvar templatesA templatesA

    set tfile [_searchTemplate $tname $file]
    set desc "$tfile<br />"
    set tblob [obj withPath $tfile releasedContent get blob]
    while {[regexp {<npsgui><!--[^>]*--></npsgui>} $tblob]} {
        append desc "<strong>[lindex [regexp -inline {<npsgui><!--([^>]*)--></npsgui>} $tblob] 1]</strong>"
        regsub {<npsgui><!--[^>]*--></npsgui>} $tblob "" tblob
    }
    set templs [list]
    while {[regexp {insertvalue="template"} $tblob]} {
        set insTempl "[lindex [regexp -inline {(<npsobj[^>]* insertvalue="template" [^>]*>)} $tblob] 1]"
        lappend templs "[lindex [regexp -inline {name="(\w*)"} $insTempl] 1]"
        regsub {insertvalue="template"} $tblob "" tblob
    }
    set templs [lsort -unique $templs]   
    set templatesA($tname) [list $desc $templs]
    foreach t $templs {
        if {![info exists templatesA($t)]} {
            _parseTemplates $t $file
        }
    }
    return [array get templatesA] 
}
