/*
    doc.bit -- Appweb documentation build file
 */

Bit.load({

    targets: {
        patch: {
            action: "
                trace('Patch', 'Patch version string ${settings.version}')
                let path = Path('product/version.html')
                let settings = bit.settings
                path.write(path.readString().replace(/<body>.*<.body>/, '<body>' + settings.version + '</body>'))
                path = Path('dsi/head.html')
                path.write(path.readString().replace(/<title>.*<.title>/, 
                    '<title>' + settings.title + ' ' + settings.version + ' Documentation</title>'))

                trace('Patch', 'Expand DSI')
                let path = Path('dsi/head.html')
                path.write(path.readString().replace(/version\">.*</, 'version\">' + 
                    settings.title + ' ' + settings.version + '<'))
                for each (path in Path('.').files('**.html', {exclude: /Bare.html|dsi/})) {
                    vtrace('Patch', path)
                    Cmd([bit.packs.dsi.path, '-v', '-I ', path])
                }
            "
        },

        api: {
            action: "
                apidoc('api/appweb.dox', '${SRC}/src/appweb.h', 'Appweb API', 'api/*.tags')
                apidoc('api/esp.dox', ['${SRC}/src/esp/edi.h', '${SRC}/src/esp/mdb.h', '${SRC}/src/esp/esp.h'], 
                    'ESP API', ls('api/*.tags'))
                for each (let f: Path in ['api/appweb.html', 'api/http.html', 'api/mpr.html', 'api/esp.html']) {
                    trace('Generate', f)
                    apiwrap(f)
                    f.write(f.readString().replace(/mprBare.html/g, 'mpr.html').
                                           replace(/httpBare.html/g, 'http.html').
                                           replace(/external=\"mpr.dtags\"/g, '').
                                           replace(/external=\"http.dtags\"/g, ''))
                }
            ",
        }, 

        tidy: {
            action: "
                for each (path in Path('.').files('**.html', { exclude: /\\/$|dsi|Base|Bare/ })) {
                    trace('Tidy', path)
                    Cmd('tidy -q -config tidy.conf ' + path)
                }
            ",
        },

        'validate-doc': {
            action: "
                let vdir = bit.dir.cfg.join('validate')
                vdir.removeAll()
                for each (file in Path('.').files('**', { exclude: /\\/$|dsi|Base|Bare/ })) {
                    let out = vdir.join(file)
                    out.dirname.makeDir()
                    if (file.extension == 'html') {
                        out.write(file.readString().replace(/<pre>((?!pre>)(?:.|\\s))*pre>/mg, '<pre>DELETED</pre>'))
                    } else {
                        out.write(file.readString())
                    }
                }
                trace('Prepare', vdir)
            ",
        },

        'clean-doc': {
            type: 'clean',
            action: "
                rmdir(['../doc/api/html', '../doc/api/xml'])
            "
        },
    
    },
})
