Translators/GoogleTranslate: add "Did you mean" hint

####################################################################
# GoogleTranslate.awk                                              #
####################################################################
#
# Last Updated: 18 Dec 2015
# https://translate.google.com/translate/releases/twsfe_w_20151214_RC03/r/js/desktop_module_main.js
BEGIN { provides("google") }

function genRL(a, x,
               ####
               b, c, d, i, y) {
    tokenize(y, x)
    parseList(b, y)
    i = SUBSEP 0
    for (c = 0; c < length(b[i]) - 2; c += 3) {
        d = b[i][c + 2]
        d = d >= 97 ? d - 87 :
            d - 48 # convert to number
        d = b[i][c + 1] == 43 ? rshift(a, d) : lshift(a, d)
        a = b[i][c] == 43 ? and(a + d, 4294967295) : xor(a, d)
    }
    return a
}

function genTK(text,
               ####
               a, d, dLen, e, tkk, ub, vb) {
    if (TK[text]) return TK[text]

    tkk = systime() / 3600
    ub = "[43,45,51,94,43,98,43,45,102]"
    vb = "[43,45,97,94,43,54]"

    dLen = dump(text, d) # convert to byte array
    a = tkk
    for (e = 1; e <= dLen; e++)
        a = genRL(a + d[e], vb)
    a = genRL(a, ub)
    0 > a && (a = and(a, 2147483647) + 2147483648)
    a %= 1e6
    TK[text] = a "." xor(a, tkk)

    l(text, "text")
    l(tkk, "tkk")
    l(TK[text], "tk")
    return TK[text]
}

function googleInit() {
    HttpProtocol = "http://"
    HttpHost = "translate.googleapis.com"
    HttpPort = 80
}

function googleRequestUrl(text, sl, tl, hl,    qc) {
    qc = Option["no-autocorrect"] ? "qc" : "qca";
    return HttpPathPrefix "/translate_a/single?client=gtx"              \
        "&ie=UTF-8&oe=UTF-8"                                            \
        "&dt=bd&dt=ex&dt=ld&dt=md&dt=rw&dt=rm&dt=ss&dt=t&dt=at"         \
        "&dt=" qc "&sl=" sl "&tl=" tl "&hl=" hl                         \
        "&q=" preprocess(text)
}

function googleTTSUrl(text, tl) {
    return HttpProtocol HttpHost "/translate_tts?ie=UTF-8&client=gtx"	\
        "&tl=" tl "&q=" preprocess(text)
}

function googleWebTranslateUrl(uri, sl, tl, hl) {
    return "https://translate.google.com/translate?"    \
        "hl=" hl "&sl=" sl "&tl=" tl "&u=" uri
}

# Get the translation of a string.
function googleTranslate(text, sl, tl, hl,
                         isVerbose, toSpeech, returnPlaylist, returnIl,
                         ####
                         r,
                         content, tokens, ast,
                         _sl, _tl, _hl, il, ils, isPhonetic,
                         article, example, explanation, ref, word,
                         translation, translations, phonetics,
                         wordClasses, words, segments, altTranslations,
                         original, oPhonetics, oWordClasses, oWords,
                         oRefs, oSynonymClasses, oSynonyms,
                         oExamples, oSeeAlso,
                         wShowOriginal, wShowOriginalPhonetics,
                         wShowTranslation, wShowTranslationPhonetics,
                         wShowPromptMessage, wShowLanguages,
                         wShowOriginalDictionary, wShowDictionary,
                         wShowAlternatives,
                         hasWordClasses, hasAltTranslations,
                         i, j, k, group, temp, saveSortedIn) {
    isPhonetic = match(tl, /^@/)
    tl = substr(tl, 1 + isPhonetic)

    if (!getCode(tl)) {
        # Check if target language is supported
        w("[WARNING] Unknown target language code: " tl)
    } else if (isRTL(tl)) {
        # Check if target language is R-to-L
        if (!FriBidi)
            w("[WARNING] " getName(tl) " is a right-to-left language, but FriBidi is not found.")
    }

    # Convert codes or aliases to standard codes used by Google Translate
    # If the code or alias cannot be found, use as it is
    _sl = getCode(sl); if (!_sl) _sl = sl
    _tl = getCode(tl); if (!_tl) _tl = tl
    _hl = getCode(hl); if (!_hl) _hl = hl
    content = getResponse(text, _sl, _tl, _hl)
    tokenize(tokens, content)
    parseJsonArray(ast, tokens)

    l(content, "content", 1, 1)
    l(tokens, "tokens", 1, 0, 1)
    l(ast, "ast")
    if (!isarray(ast) || !anything(ast)) {
        e("[ERROR] Oops! Something went wrong and I can't translate it for you :(")
        ExitCode = 1
        return
    }

    saveSortedIn = PROCINFO["sorted_in"]
    PROCINFO["sorted_in"] = "@ind_num_asc"
    for (i in ast) {
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "0$")
            append(translations, postprocess(literal(ast[i])))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "1$")
            append(original, literal(ast[i]))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "2$")
            append(phonetics, literal(ast[i]))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "3$")
            append(oPhonetics, literal(ast[i]))

        # 1 - word classes and explanations
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            wordClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "([[:digit:]]+)$", group))
            words[group[1]][group[2]][group[3]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)$", group))
            words[group[1]][group[2]]["1"][group[3]] = literal(ast[i])

        # 5 - alternative translations
        if (match(i, "^0" SUBSEP "5" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group)) {
            segments[group[1]] = literal(ast[i])
            altTranslations[group[1]][0] = ""
        }
        if (match(i, "^0" SUBSEP "5" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            altTranslations[group[1]][group[2]] = postprocess(literal(ast[i]))

        # 7 - autocorrection
        if (i ~ "^0" SUBSEP "7" SUBSEP "5$") {
            if (ast[i] == "true")
                w("Showing translation for:  (use -no-auto to disable autocorrect)")
            else
                w("Did you mean: "                                      \
                  ansi("bold", unparameterize(ast["0" SUBSEP "7" SUBSEP "1"])))
        }

        # 8 - identified source languages
        if (i ~ "^0" SUBSEP "8" SUBSEP "0" SUBSEP "[[:digit:]]+$" ||
            i ~ "^0" SUBSEP "2$")
            append(ils, literal(ast[i]))

        # 11 - (original) word classes and synonyms
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oSynonymClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "1$", group))
            if (ast[i]) {
                oRefs[literal(ast[i])][1] = group[1]
                oRefs[literal(ast[i])][2] = group[2]
            }
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0" SUBSEP "([[:digit:]]+)$", group))
            oSynonyms[group[1]][group[2]][group[3]] = literal(ast[i])

        # 12 - (original) word classes and explanations
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oWordClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oWords[group[1]][group[2]][0] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "1$", group))
            oWords[group[1]][group[2]][1] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2$", group))
            oWords[group[1]][group[2]][2] = postprocess(literal(ast[i]))

        # 13 - (original) examples
        if (match(i, "^0" SUBSEP "13" SUBSEP "0" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oExamples[group[1]] = postprocess(literal(ast[i]))

        # 14 - (original) see also
        if (match(i, "^0" SUBSEP "14" SUBSEP "0" SUBSEP "([[:digit:]]+)$", group))
            oSeeAlso[group[1]] = literal(ast[i])
    }
    PROCINFO["sorted_in"] = saveSortedIn

    translation = join(translations)

    returnIl[0] = il = !anything(ils) || belongsTo(sl, ils) ? sl : ils[0]
    if (Option["verbose"] < 0)
        return getList(il)

    # Generate output
    if (!isVerbose) {
        # Brief mode

        r = isPhonetic && anything(phonetics) ?
            prettify("brief-translation-phonetics", join(phonetics)) :
            prettify("brief-translation", s(translation, tl))

        if (toSpeech) {
            returnPlaylist[0]["text"] = translation
            returnPlaylist[0]["tl"] = tl
        }

    } else {
        # Verbose mode

        wShowOriginal = Option["show-original"]
        wShowOriginalPhonetics = Option["show-original-phonetics"]
        wShowTranslation = Option["show-translation"]
        wShowTranslationPhonetics = Option["show-translation-phonetics"]
        wShowPromptMessage = Option["show-prompt-message"]
        wShowLanguages = Option["show-languages"]
        wShowOriginalDictionary = Option["show-original-dictionary"]
        wShowDictionary = Option["show-dictionary"]
        wShowAlternatives = Option["show-alternatives"]

        if (!anything(oPhonetics)) wShowOriginalPhonetics = 0
        if (!anything(phonetics)) wShowTranslationPhonetics = 0
        if (getCode(il) == getCode(tl) &&                               \
            (isarray(oWordClasses) || isarray(oSynonymClasses) ||       \
             isarray(oExamples) || isarray(oSeeAlso))) {
            wShowOriginalDictionary = 1
            wShowTranslation = 0
        }
        hasWordClasses = exists(wordClasses)
        hasAltTranslations = exists(altTranslations[0])
        if (!hasWordClasses && !hasAltTranslations)
            wShowPromptMessage = wShowLanguages = 0
        if (!hasWordClasses) wShowDictionary = 0
        if (hasWordClasses || !hasAltTranslations) wShowAlternatives = 0

        if (wShowOriginal) {
            # Display: original text & phonetics
            if (r) r = r RS RS
            r = r m("-- display original text & phonetics")
            r = r prettify("original", s(join(original), il))
            if (wShowOriginalPhonetics)
                r = r RS prettify("original-phonetics", showPhonetics(join(oPhonetics), il))
        }

        if (wShowTranslation) {
            # Display: major translation & phonetics
            if (r) r = r RS RS
            r = r m("-- display major translation & phonetics")
            r = r prettify("translation", s(translation, tl))
            if (wShowTranslationPhonetics)
                r = r RS prettify("translation-phonetics", showPhonetics(join(phonetics), tl))
        }

        if (wShowPromptMessage || wShowLanguages)
            if (r) r = r RS
        if (wShowPromptMessage) {
            if (hasWordClasses) {
                # Display: prompt message (Definitions of ...)
                if (r) r = r RS
                r = r m("-- display prompt message (Definitions of ...)")
                if (isRTL(hl)) # home language is R-to-L
                    r = r prettify("prompt-message", s(showDefinitionsOf(hl, join(original))))
                else { # home language is L-to-R
                    split(showDefinitionsOf(hl, "\0%s\0"), group, "\0")
                    for (i = 1; i <= length(group); i++) {
                        if (group[i] == "%s")
                            r = r prettify("prompt-message-original", show(join(original), il))
                        else
                            r = r prettify("prompt-message", group[i])
                    }
                }
            } else if (hasAltTranslations) {
                # Display: prompt message (Translations of ...)
                if (r) r = r RS
                r = r m("-- display prompt message (Translations of ...)")
                if (isRTL(hl)) # home language is R-to-L
                    r = r prettify("prompt-message", s(showTranslationsOf(hl, join(original))))
                else { # home language is L-to-R
                    split(showTranslationsOf(hl, "\0%s\0"), group, "\0")
                    for (i = 1; i <= length(group); i++) {
                        if (group[i] == "%s")
                            r = r prettify("prompt-message-original", show(join(original), il))
                        else
                            r = r prettify("prompt-message", group[i])
                    }
                }
            }
        }
        if (wShowLanguages) {
            # Display: source language -> target language
            if (r) r = r RS
            r = r m("-- display source language -> target language")
            temp = Option["fmt-languages"]
            if (!temp) temp = "[ %s -> %t ]"
            split(temp, group, /(%s|%S|%t|%T)/)
            r = r prettify("languages", group[1])
            if (temp ~ /%s/)
                r = r prettify("languages-sl", getDisplay(il))
            if (temp ~ /%S/)
                r = r prettify("languages-sl", getName(il))
            r = r prettify("languages", group[2])
            if (temp ~ /%t/)
                r = r prettify("languages-tl", getDisplay(tl))
            if (temp ~ /%T/)
                r = r prettify("languages-tl", getName(tl))
            r = r prettify("languages", group[3])
        }

        if (wShowOriginalDictionary) {
            # Display: original dictionary
            if (exists(oWordClasses)) {
                # Detailed explanations
                if (r) r = r RS
                r = r m("-- display original dictionary (detailed explanations)")
                for (i = 0; i < length(oWordClasses); i++) {
                    r = (i > 0 ? r RS : r) RS prettify("original-dictionary-detailed-word-class", s(oWordClasses[i], hl))
                    for (j = 0; j < length(oWords[i]); j++) {
                        explanation = oWords[i][j][0]
                        ref = oWords[i][j][1]
                        example = oWords[i][j][2]

                        r = (j > 0 ? r RS : r) RS prettify("original-dictionary-detailed-explanation", ins(1, explanation, il))
                        if (example)
                            r = r RS prettify("original-dictionary-detailed-example", ins(2, "- \"" example "\"", il))
                        if (ref && isarray(oRefs[ref])) {
                            temp = prettify("original-dictionary-detailed-synonyms", ins(1, show(showSynonyms(hl), hl) ": "))
                            temp = temp prettify("original-dictionary-detailed-synonyms-item", show(oSynonyms[oRefs[ref][1]][oRefs[ref][2]][0], il))
                            for (k = 1; k < length(oSynonyms[oRefs[ref][1]][oRefs[ref][2]]); k++)
                                temp = temp prettify("original-dictionary-detailed-synonyms", ", ") \
                                    prettify("original-dictionary-detailed-synonyms-item", show(oSynonyms[oRefs[ref][1]][oRefs[ref][2]][k], il))
                            r = r RS temp
                        }
                    }
                }
            }
            if (exists(oSynonymClasses)) {
                # Synonyms
                r = r RS RS
                r = r m("-- display original dictionary (synonyms)")
                r = r prettify("original-dictionary-synonyms", s(showSynonyms(hl), hl))
                for (i = 0; i < length(oSynonymClasses); i++) {
                    r = (i > 0 ? r RS : r) RS prettify("original-dictionary-synonyms-word-class", ins(1, oSynonymClasses[i], hl))
                    for (j = 0; j < length(oSynonyms[i]); j++) {
                        temp = prettify("original-dictionary-synonyms-synonyms", ins(2, "- "))
                        temp = temp prettify("original-dictionary-synonyms-synonyms-item", show(oSynonyms[i][j][0], il))
                        for (k = 1; k < length(oSynonyms[i][j]); k++)
                            temp = temp prettify("original-dictionary-synonyms-synonyms", ", ") \
                                prettify("original-dictionary-synonyms-synonyms-item", show(oSynonyms[i][j][k], il))
                        r = r RS temp
                    }
                }
            }
            if (exists(oExamples)) {
                # Examples
                r = r RS RS
                r = r m("-- display original dictionary (examples)")
                r = r prettify("original-dictionary-examples", s(showExamples(hl), hl))
                for (i = 0; i < length(oExamples); i++) {
                    example = oExamples[i]

                    temp = prettify("original-dictionary-examples-example", ins(1, "- "))
                    split(example, group, /(\u003cb\u003e|\u003c\/b\u003e)/)
                    if (isRTL(il)) # target language is R-to-L
                        temp = temp show(group[1] group[2] group[3], il)
                    else # target language is L-to-R
                        temp = temp prettify("original-dictionary-examples-example", group[1]) \
                            prettify("original-dictionary-examples-original", group[2]) \
                            prettify("original-dictionary-examples-example", group[3])
                    r = (i > 0 ? r RS : r) RS temp
                }
            }
            if (exists(oSeeAlso)) {
                # See also
                r = r RS RS
                r = r m("-- display original dictionary (see also)")
                r = r prettify("original-dictionary-see-also", s(showSeeAlso(hl), hl))
                temp = ins(1, prettify("original-dictionary-see-also-phrases-item", show(oSeeAlso[0], il)))
                for (k = 1; k < length(oSeeAlso); k++)
                    temp = temp prettify("original-dictionary-see-also-phrases", ", ") \
                        prettify("original-dictionary-see-also-phrases-item", show(oSeeAlso[k], il))
                r = r RS temp
            }
        }

        if (wShowDictionary) {
            # Display: dictionary entries
            if (r) r = r RS
            r = r m("-- display dictionary entries")
            for (i = 0; i < length(wordClasses); i++) {
                r = (i > 0 ? r RS : r) RS prettify("dictionary-word-class", s(wordClasses[i], hl))
                for (j = 0; j < length(words[i]); j++) {
                    word = words[i][j][0]
                    article = words[i][j][4]
                    if (isRTL(il))
                        explanation = join(words[i][j][1], ", ")
                    else {
                        explanation = prettify("dictionary-explanation-item", words[i][j][1][0])
                        for (k = 1; k < length(words[i][j][1]); k++)
                            explanation = explanation prettify("dictionary-explanation", ", ") \
                                prettify("dictionary-explanation-item", words[i][j][1][k])
                    }

                    r = r RS prettify("dictionary-word", ins(1, (article ? "(" article ") " : "") word, tl))
                    if (isRTL(il))
                        r = r RS prettify("dictionary-explanation-item", ins(2, explanation, il))
                    else
                        r = r RS ins(2, explanation)
                }
            }
        }

        if (wShowAlternatives) {
            # Display: alternative translations
            if (r) r = r RS RS
            r = r m("-- display alternative translations")
            for (i = 0; i < length(altTranslations); i++) {
                r = (i > 0 ? r RS : r) prettify("alternatives-original", show(segments[i], il))
                if (isRTL(tl)) {
                    temp = join(altTranslations[i], ", ")
                    r = r RS prettify("alternatives-translations-item", ins(1, temp, tl))
                } else {
                    temp = prettify("alternatives-translations-item", altTranslations[i][0])
                    for (j = 1; j < length(altTranslations[i]); j++)
                        temp = temp prettify("alternatives-translations", ", ") \
                            prettify("alternatives-translations-item", altTranslations[i][j])
                    r = r RS ins(1, temp)
                }
            }
        }

        if (toSpeech) {
            if (index(showTranslationsOf(hl, "%s"), "%s") > 2) {
                returnPlaylist[0]["text"] = showTranslationsOf(hl)
                returnPlaylist[0]["tl"] = hl
                returnPlaylist[1]["text"] = join(original)
                returnPlaylist[1]["tl"] = il
            } else {
                returnPlaylist[0]["text"] = join(original)
                returnPlaylist[0]["tl"] = il
                returnPlaylist[1]["text"] = showTranslationsOf(hl)
                returnPlaylist[1]["tl"] = hl
            }
            returnPlaylist[2]["text"] = translation
            returnPlaylist[2]["tl"] = tl
        }
    }

    return r
}

####################################################################
# GoogleTranslate.awk                                              #
####################################################################
#
# Last Updated: 18 Dec 2015
# https://translate.google.com/translate/releases/twsfe_w_20151214_RC03/r/js/desktop_module_main.js
BEGIN { provides("google") }

function genRL(a, x,
               ####
               b, c, d, i, y) {
    tokenize(y, x)
    parseList(b, y)
    i = SUBSEP 0
    for (c = 0; c < length(b[i]) - 2; c += 3) {
        d = b[i][c + 2]
        d = d >= 97 ? d - 87 :
            d - 48 # convert to number
        d = b[i][c + 1] == 43 ? rshift(a, d) : lshift(a, d)
        a = b[i][c] == 43 ? and(a + d, 4294967295) : xor(a, d)
    }
    return a
}

function genTK(text,
               ####
               a, d, dLen, e, tkk, ub, vb) {
    if (TK[text]) return TK[text]

    tkk = systime() / 3600
    ub = "[43,45,51,94,43,98,43,45,102]"
    vb = "[43,45,97,94,43,54]"

    dLen = dump(text, d) # convert to byte array
    a = tkk
    for (e = 1; e <= dLen; e++)
        a = genRL(a + d[e], vb)
    a = genRL(a, ub)
    0 > a && (a = and(a, 2147483647) + 2147483648)
    a %= 1e6
    TK[text] = a "." xor(a, tkk)

    l(text, "text")
    l(tkk, "tkk")
    l(TK[text], "tk")
    return TK[text]
}

function googleInit() {
    HttpProtocol = "http://"
    HttpHost = "translate.googleapis.com"
    HttpPort = 80
}

function googleRequestUrl(text, sl, tl, hl,    qc) {
    qc = Option["no-autocorrect"] ? "qc" : "qca";
    return HttpPathPrefix "/translate_a/single?client=gtx"              \
        "&ie=UTF-8&oe=UTF-8"                                            \
        "&dt=bd&dt=ex&dt=ld&dt=md&dt=rw&dt=rm&dt=ss&dt=t&dt=at"         \
        "&dt=" qc "&sl=" sl "&tl=" tl "&hl=" hl                         \
        "&q=" preprocess(text)
}

function googleTTSUrl(text, tl) {
    return HttpProtocol HttpHost "/translate_tts?ie=UTF-8&client=gtx"	\
        "&tl=" tl "&q=" preprocess(text)
}

function googleWebTranslateUrl(uri, sl, tl, hl) {
    return "https://translate.google.com/translate?"    \
        "hl=" hl "&sl=" sl "&tl=" tl "&u=" uri
}

# Get the translation of a string.
function googleTranslate(text, sl, tl, hl,
                         isVerbose, toSpeech, returnPlaylist, returnIl,
                         ####
                         r,
                         content, tokens, ast,
                         _sl, _tl, _hl, il, ils, isPhonetic,
                         article, example, explanation, ref, word,
                         translation, translations, phonetics,
                         wordClasses, words, segments, altTranslations,
                         original, oPhonetics, oWordClasses, oWords,
                         oRefs, oSynonymClasses, oSynonyms,
                         oExamples, oSeeAlso,
                         wShowOriginal, wShowOriginalPhonetics,
                         wShowTranslation, wShowTranslationPhonetics,
                         wShowPromptMessage, wShowLanguages,
                         wShowOriginalDictionary, wShowDictionary,
                         wShowAlternatives,
                         hasWordClasses, hasAltTranslations,
                         i, j, k, group, temp, saveSortedIn) {
    isPhonetic = match(tl, /^@/)
    tl = substr(tl, 1 + isPhonetic)

    if (!getCode(tl)) {
        # Check if target language is supported
        w("[WARNING] Unknown target language code: " tl)
    } else if (isRTL(tl)) {
        # Check if target language is R-to-L
        if (!FriBidi)
            w("[WARNING] " getName(tl) " is a right-to-left language, but FriBidi is not found.")
    }

    # Convert codes or aliases to standard codes used by Google Translate
    # If the code or alias cannot be found, use as it is
    _sl = getCode(sl); if (!_sl) _sl = sl
    _tl = getCode(tl); if (!_tl) _tl = tl
    _hl = getCode(hl); if (!_hl) _hl = hl
    content = getResponse(text, _sl, _tl, _hl)
    tokenize(tokens, content)
    parseJsonArray(ast, tokens)

    l(content, "content", 1, 1)
    l(tokens, "tokens", 1, 0, 1)
    l(ast, "ast")
    if (!isarray(ast) || !anything(ast)) {
        e("[ERROR] Oops! Something went wrong and I can't translate it for you :(")
        ExitCode = 1
        return
    }

    saveSortedIn = PROCINFO["sorted_in"]
    PROCINFO["sorted_in"] = "@ind_num_asc"
    for (i in ast) {
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "0$")
            append(translations, postprocess(literal(ast[i])))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "1$")
            append(original, literal(ast[i]))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "2$")
            append(phonetics, literal(ast[i]))
        if (i ~ "^0" SUBSEP "0" SUBSEP "[[:digit:]]+" SUBSEP "3$")
            append(oPhonetics, literal(ast[i]))

        # 1 - word classes and explanations
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            wordClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "([[:digit:]]+)$", group))
            words[group[1]][group[2]][group[3]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)$", group))
            words[group[1]][group[2]]["1"][group[3]] = literal(ast[i])

        # 5 - alternative translations
        if (match(i, "^0" SUBSEP "5" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group)) {
            segments[group[1]] = literal(ast[i])
            altTranslations[group[1]][0] = ""
        }
        if (match(i, "^0" SUBSEP "5" SUBSEP "([[:digit:]]+)" SUBSEP "2" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            altTranslations[group[1]][group[2]] = postprocess(literal(ast[i]))

        # 7 - autocorrection
        if (i ~ "^0" SUBSEP "7" SUBSEP "5$")
            w("Showing translation for:  (use -no-auto to disable autocorrect)")

        # 8 - identified source languages
        if (i ~ "^0" SUBSEP "8" SUBSEP "0" SUBSEP "[[:digit:]]+$" ||
            i ~ "^0" SUBSEP "2$")
            append(ils, literal(ast[i]))

        # 11 - (original) word classes and synonyms
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oSynonymClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "1$", group))
            if (ast[i]) {
                oRefs[literal(ast[i])][1] = group[1]
                oRefs[literal(ast[i])][2] = group[2]
            }
        if (match(i, "^0" SUBSEP "11" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0" SUBSEP "([[:digit:]]+)$", group))
            oSynonyms[group[1]][group[2]][group[3]] = literal(ast[i])

        # 12 - (original) word classes and explanations
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oWordClasses[group[1]] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oWords[group[1]][group[2]][0] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "1$", group))
            oWords[group[1]][group[2]][1] = literal(ast[i])
        if (match(i, "^0" SUBSEP "12" SUBSEP "([[:digit:]]+)" SUBSEP "1" SUBSEP "([[:digit:]]+)" SUBSEP "2$", group))
            oWords[group[1]][group[2]][2] = postprocess(literal(ast[i]))

        # 13 - (original) examples
        if (match(i, "^0" SUBSEP "13" SUBSEP "0" SUBSEP "([[:digit:]]+)" SUBSEP "0$", group))
            oExamples[group[1]] = postprocess(literal(ast[i]))

        # 14 - (original) see also
        if (match(i, "^0" SUBSEP "14" SUBSEP "0" SUBSEP "([[:digit:]]+)$", group))
            oSeeAlso[group[1]] = literal(ast[i])
    }
    PROCINFO["sorted_in"] = saveSortedIn

    translation = join(translations)

    returnIl[0] = il = !anything(ils) || belongsTo(sl, ils) ? sl : ils[0]
    if (Option["verbose"] < 0)
        return getList(il)

    # Generate output
    if (!isVerbose) {
        # Brief mode

        r = isPhonetic && anything(phonetics) ?
            prettify("brief-translation-phonetics", join(phonetics)) :
            prettify("brief-translation", s(translation, tl))

        if (toSpeech) {
            returnPlaylist[0]["text"] = translation
            returnPlaylist[0]["tl"] = tl
        }

    } else {
        # Verbose mode

        wShowOriginal = Option["show-original"]
        wShowOriginalPhonetics = Option["show-original-phonetics"]
        wShowTranslation = Option["show-translation"]
        wShowTranslationPhonetics = Option["show-translation-phonetics"]
        wShowPromptMessage = Option["show-prompt-message"]
        wShowLanguages = Option["show-languages"]
        wShowOriginalDictionary = Option["show-original-dictionary"]
        wShowDictionary = Option["show-dictionary"]
        wShowAlternatives = Option["show-alternatives"]

        if (!anything(oPhonetics)) wShowOriginalPhonetics = 0
        if (!anything(phonetics)) wShowTranslationPhonetics = 0
        if (getCode(il) == getCode(tl) &&                               \
            (isarray(oWordClasses) || isarray(oSynonymClasses) ||       \
             isarray(oExamples) || isarray(oSeeAlso))) {
            wShowOriginalDictionary = 1
            wShowTranslation = 0
        }
        hasWordClasses = exists(wordClasses)
        hasAltTranslations = exists(altTranslations[0])
        if (!hasWordClasses && !hasAltTranslations)
            wShowPromptMessage = wShowLanguages = 0
        if (!hasWordClasses) wShowDictionary = 0
        if (hasWordClasses || !hasAltTranslations) wShowAlternatives = 0

        if (wShowOriginal) {
            # Display: original text & phonetics
            if (r) r = r RS RS
            r = r m("-- display original text & phonetics")
            r = r prettify("original", s(join(original), il))
            if (wShowOriginalPhonetics)
                r = r RS prettify("original-phonetics", showPhonetics(join(oPhonetics), il))
        }

        if (wShowTranslation) {
            # Display: major translation & phonetics
            if (r) r = r RS RS
            r = r m("-- display major translation & phonetics")
            r = r prettify("translation", s(translation, tl))
            if (wShowTranslationPhonetics)
                r = r RS prettify("translation-phonetics", showPhonetics(join(phonetics), tl))
        }

        if (wShowPromptMessage || wShowLanguages)
            if (r) r = r RS
        if (wShowPromptMessage) {
            if (hasWordClasses) {
                # Display: prompt message (Definitions of ...)
                if (r) r = r RS
                r = r m("-- display prompt message (Definitions of ...)")
                if (isRTL(hl)) # home language is R-to-L
                    r = r prettify("prompt-message", s(showDefinitionsOf(hl, join(original))))
                else { # home language is L-to-R
                    split(showDefinitionsOf(hl, "\0%s\0"), group, "\0")
                    for (i = 1; i <= length(group); i++) {
                        if (group[i] == "%s")
                            r = r prettify("prompt-message-original", show(join(original), il))
                        else
                            r = r prettify("prompt-message", group[i])
                    }
                }
            } else if (hasAltTranslations) {
                # Display: prompt message (Translations of ...)
                if (r) r = r RS
                r = r m("-- display prompt message (Translations of ...)")
                if (isRTL(hl)) # home language is R-to-L
                    r = r prettify("prompt-message", s(showTranslationsOf(hl, join(original))))
                else { # home language is L-to-R
                    split(showTranslationsOf(hl, "\0%s\0"), group, "\0")
                    for (i = 1; i <= length(group); i++) {
                        if (group[i] == "%s")
                            r = r prettify("prompt-message-original", show(join(original), il))
                        else
                            r = r prettify("prompt-message", group[i])
                    }
                }
            }
        }
        if (wShowLanguages) {
            # Display: source language -> target language
            if (r) r = r RS
            r = r m("-- display source language -> target language")
            temp = Option["fmt-languages"]
            if (!temp) temp = "[ %s -> %t ]"
            split(temp, group, /(%s|%S|%t|%T)/)
            r = r prettify("languages", group[1])
            if (temp ~ /%s/)
                r = r prettify("languages-sl", getDisplay(il))
            if (temp ~ /%S/)
                r = r prettify("languages-sl", getName(il))
            r = r prettify("languages", group[2])
            if (temp ~ /%t/)
                r = r prettify("languages-tl", getDisplay(tl))
            if (temp ~ /%T/)
                r = r prettify("languages-tl", getName(tl))
            r = r prettify("languages", group[3])
        }

        if (wShowOriginalDictionary) {
            # Display: original dictionary
            if (exists(oWordClasses)) {
                # Detailed explanations
                if (r) r = r RS
                r = r m("-- display original dictionary (detailed explanations)")
                for (i = 0; i < length(oWordClasses); i++) {
                    r = (i > 0 ? r RS : r) RS prettify("original-dictionary-detailed-word-class", s(oWordClasses[i], hl))
                    for (j = 0; j < length(oWords[i]); j++) {
                        explanation = oWords[i][j][0]
                        ref = oWords[i][j][1]
                        example = oWords[i][j][2]

                        r = (j > 0 ? r RS : r) RS prettify("original-dictionary-detailed-explanation", ins(1, explanation, il))
                        if (example)
                            r = r RS prettify("original-dictionary-detailed-example", ins(2, "- \"" example "\"", il))
                        if (ref && isarray(oRefs[ref])) {
                            temp = prettify("original-dictionary-detailed-synonyms", ins(1, show(showSynonyms(hl), hl) ": "))
                            temp = temp prettify("original-dictionary-detailed-synonyms-item", show(oSynonyms[oRefs[ref][1]][oRefs[ref][2]][0], il))
                            for (k = 1; k < length(oSynonyms[oRefs[ref][1]][oRefs[ref][2]]); k++)
                                temp = temp prettify("original-dictionary-detailed-synonyms", ", ") \
                                    prettify("original-dictionary-detailed-synonyms-item", show(oSynonyms[oRefs[ref][1]][oRefs[ref][2]][k], il))
                            r = r RS temp
                        }
                    }
                }
            }
            if (exists(oSynonymClasses)) {
                # Synonyms
                r = r RS RS
                r = r m("-- display original dictionary (synonyms)")
                r = r prettify("original-dictionary-synonyms", s(showSynonyms(hl), hl))
                for (i = 0; i < length(oSynonymClasses); i++) {
                    r = (i > 0 ? r RS : r) RS prettify("original-dictionary-synonyms-word-class", ins(1, oSynonymClasses[i], hl))
                    for (j = 0; j < length(oSynonyms[i]); j++) {
                        temp = prettify("original-dictionary-synonyms-synonyms", ins(2, "- "))
                        temp = temp prettify("original-dictionary-synonyms-synonyms-item", show(oSynonyms[i][j][0], il))
                        for (k = 1; k < length(oSynonyms[i][j]); k++)
                            temp = temp prettify("original-dictionary-synonyms-synonyms", ", ") \
                                prettify("original-dictionary-synonyms-synonyms-item", show(oSynonyms[i][j][k], il))
                        r = r RS temp
                    }
                }
            }
            if (exists(oExamples)) {
                # Examples
                r = r RS RS
                r = r m("-- display original dictionary (examples)")
                r = r prettify("original-dictionary-examples", s(showExamples(hl), hl))
                for (i = 0; i < length(oExamples); i++) {
                    example = oExamples[i]

                    temp = prettify("original-dictionary-examples-example", ins(1, "- "))
                    split(example, group, /(\u003cb\u003e|\u003c\/b\u003e)/)
                    if (isRTL(il)) # target language is R-to-L
                        temp = temp show(group[1] group[2] group[3], il)
                    else # target language is L-to-R
                        temp = temp prettify("original-dictionary-examples-example", group[1]) \
                            prettify("original-dictionary-examples-original", group[2]) \
                            prettify("original-dictionary-examples-example", group[3])
                    r = (i > 0 ? r RS : r) RS temp
                }
            }
            if (exists(oSeeAlso)) {
                # See also
                r = r RS RS
                r = r m("-- display original dictionary (see also)")
                r = r prettify("original-dictionary-see-also", s(showSeeAlso(hl), hl))
                temp = ins(1, prettify("original-dictionary-see-also-phrases-item", show(oSeeAlso[0], il)))
                for (k = 1; k < length(oSeeAlso); k++)
                    temp = temp prettify("original-dictionary-see-also-phrases", ", ") \
                        prettify("original-dictionary-see-also-phrases-item", show(oSeeAlso[k], il))
                r = r RS temp
            }
        }

        if (wShowDictionary) {
            # Display: dictionary entries
            if (r) r = r RS
            r = r m("-- display dictionary entries")
            for (i = 0; i < length(wordClasses); i++) {
                r = (i > 0 ? r RS : r) RS prettify("dictionary-word-class", s(wordClasses[i], hl))
                for (j = 0; j < length(words[i]); j++) {
                    word = words[i][j][0]
                    article = words[i][j][4]
                    if (isRTL(il))
                        explanation = join(words[i][j][1], ", ")
                    else {
                        explanation = prettify("dictionary-explanation-item", words[i][j][1][0])
                        for (k = 1; k < length(words[i][j][1]); k++)
                            explanation = explanation prettify("dictionary-explanation", ", ") \
                                prettify("dictionary-explanation-item", words[i][j][1][k])
                    }

                    r = r RS prettify("dictionary-word", ins(1, (article ? "(" article ") " : "") word, tl))
                    if (isRTL(il))
                        r = r RS prettify("dictionary-explanation-item", ins(2, explanation, il))
                    else
                        r = r RS ins(2, explanation)
                }
            }
        }

        if (wShowAlternatives) {
            # Display: alternative translations
            if (r) r = r RS RS
            r = r m("-- display alternative translations")
            for (i = 0; i < length(altTranslations); i++) {
                r = (i > 0 ? r RS : r) prettify("alternatives-original", show(segments[i], il))
                if (isRTL(tl)) {
                    temp = join(altTranslations[i], ", ")
                    r = r RS prettify("alternatives-translations-item", ins(1, temp, tl))
                } else {
                    temp = prettify("alternatives-translations-item", altTranslations[i][0])
                    for (j = 1; j < length(altTranslations[i]); j++)
                        temp = temp prettify("alternatives-translations", ", ") \
                            prettify("alternatives-translations-item", altTranslations[i][j])
                    r = r RS ins(1, temp)
                }
            }
        }

        if (toSpeech) {
            if (index(showTranslationsOf(hl, "%s"), "%s") > 2) {
                returnPlaylist[0]["text"] = showTranslationsOf(hl)
                returnPlaylist[0]["tl"] = hl
                returnPlaylist[1]["text"] = join(original)
                returnPlaylist[1]["tl"] = il
            } else {
                returnPlaylist[0]["text"] = join(original)
                returnPlaylist[0]["tl"] = il
                returnPlaylist[1]["text"] = showTranslationsOf(hl)
                returnPlaylist[1]["tl"] = hl
            }
            returnPlaylist[2]["text"] = translation
            returnPlaylist[2]["tl"] = tl
        }
    }

    return r
}


correct color asignment

correct color asignment

#! /usr/bin/awk -f
# deltadot.awk <encumbant.gv> <candidate.gv>
# Use to illuminate the commonality and differences between dot files created
# by 'ntriples2dot.awk' but might work for other dot files
#
# To ignore counts
# deltadot.awk -v"NOCOUNTS=1" <encumbant.gv> <candidate.gv>
#
# expects two graphviz dot digraph files;
# by convention "old"  then "new"
# for color to mean;  orange lost in "new" and blue gained in "new"
# with simple node names ('C-lang' variable naming conventions)
# edge's unadorned predicate label
# concludes with a space then count in parens
#
# For example:
# NODE1 -> NODE2 [label=<curie:term ! label (42)>];
#
# where the " ! label " is optional, but the lable _could_ have parens
#
# filter for edges in each file associate the count with the edge
# if an edge is common between the files
# then change its "count" to the difference. (second minus first)
# otherwise leave count as is
# UNLESS there is a parameter NOCOUNTS not equal to 0
# in which case set all counts the same
# this is for use when the counts have no meaning
# such as with random samples

# associate the count with an edge
function parse(str, arr){
    # can't count on label being paren-free anymore
    n = match($0, / [(][[:digit:]]*[)]>];$/)
    if(n>0)
        arr[substr($0,1,n)] = substr($0, n+2, length($0)-n-5)
    else
        arr[$1 " " $2 " " $3 " "]++
}

# strip metadata from filename
function de_path_ext(pth){
    split(pth, a, "/")
    return substr(a[length(a)], 1, index(a[length(a)], ".")-1)
}

BEGIN {
    NOCOUNTS=0
    DFLT="black"
    GAIN="blue"
    DROP="orange"
    LESS="pink"
}

# collect the edges from both files
NR==FNR && /^[^ ]+ -> [^ ]+ / {
    label1 = FILENAME;
    parse($0, edge1)
}
NR!=FNR && /^[^ ]+ -> [^ ]+ / {
    label2 = FILENAME;
    parse($0, edge2)
}

END{
    for(e in edge1){
        if(!(e in edge2)) edges[e "(-" edge1[e] ")>, color=\"" DROP "\"];"];
        else {
            # edge is in both graphs
            if(NOCOUNTS)
                edges[e "()>, color=\"" DELT "\"];"]
            else {
                diff = edge2[e] - edge1[e];
                if(diff < 0)
                    # diff = "" diff "" #  breaks svg
                    edges[e "(" diff ")>, color=\"" LESS "\"];"]
                else
                    edges[e "(" diff ")>, color=\"" DFLT "\"];"]
            }
            delete edge2[e]
        }
    }
    # only edges not in first file
    for(e in edge2)
        edges[e "(" edge2[e] ")>, color=\"" GAIN "\"];"];  # expects label=<tag> to close

    print "digraph {"
    print "rankdir=LR;"
    print "charset=\"utf-8\";"
    print "LITERAL [shape=record];"
    print "labelloc=\"t\";"
    print "label=\"" de_path_ext(label1) " <=> " de_path_ext(label2) "\""

    n = asorti(edges,output)
    for(i=1;i<=n;i++ ){print output[i]}

    # include a Legend  rank [min,max,sink]
    print "\n\tnode[shape=plaintext];\n\tsubgraph cluster_key{rank=max;label=\"Color Legend\";"
    print "\tdflt [label=< Typical Case >];"
    print "\tgain [label=< New Edge >];"
    print "\tdrop [label=< Lost Edge >];"
    print "\tless [label=< Diminished Count >];"
    print "\tdflt -> dflt [color=\"" DFLT "\"];"
    print "\tgain -> gain [color=\"" GAIN "\"];"
    print "\tdrop -> drop [color=\"" DROP "\"];"
    print "\tless -> less [color=\"" LESS "\"];"

    print "\t}\n}\n"
}

#! /usr/bin/awk -f
# deltadot.awk <encumbant.gv> <candidate.gv>
# Use to illuminate the commonality and differences between dot files created
# by 'ntriples2dot.awk' but might work for other dot files
#
# To ignore counts
# deltadot.awk -v"NOCOUNTS=1" <encumbant.gv> <candidate.gv>
#
# expects two graphviz dot digraph files;
# by convention "old"  then "new"
# for color to mean;  orange lost in "new" and blue gained in "new"
# with simple node names ('C-lang' variable naming conventions)
# edge's unadorned predicate label
# concludes with a space then count in parens
#
# For example:
# NODE1 -> NODE2 [label=<curie:term ! label (42)>];
#
# where the " ! label " is optional, but the lable _could_ have parens
#
# filter for edges in each file associate the count with the edge
# if an edge is common between the files
# then change its "count" to the difference. (second minus first)
# otherwise leave count as is
# UNLESS there is a parameter NOCOUNTS not equal to 0
# in which case set all counts the same
# this is for use when the counts have no meaning
# such as with random samples

# associate the count with an edge
function parse(str, arr){
    # can't count on label being paren-free anymore
    n = match($0, / [(][[:digit:]]*[)]>];$/)
    if(n>0)
        arr[substr($0,1,n)] = substr($0, n+2, length($0)-n-5)
    else
        arr[$1 " " $2 " " $3 " "]++
}

# strip metadata from filename
function de_path_ext(pth){
    split(pth, a, "/")
    return substr(a[length(a)], 1, index(a[length(a)], ".")-1)
}

BEGIN {
    NOCOUNTS=0
    DFLT="black"
    GAIN="blue"
    DROP="orange"
    LESS="pink"
}

# collect the edges from both files
NR==FNR && /^[^ ]+ -> [^ ]+ / {
    label1 = FILENAME;
    parse($0, edge1)
}
NR!=FNR && /^[^ ]+ -> [^ ]+ / {
    label2 = FILENAME;
    parse($0, edge2)
}

END{
    for(e in edge1){
        if(!(e in edge2)) edges[e "(-" edge1[e] ")>, color=\"" DROP "\"];"];
        else {
            # edge is in both graphs
            if(NOCOUNTS)
                edges[e "()>, color=\"" DELT "\"];"]
            else {
                diff = edge2[e] - edge1[e];
                if(diff < 0)
                    # diff = "" diff "" #  breaks svg
                    edges[e "(" diff ")>, color=\"" DROP "\"];"]
                else
                    edges[e "(" diff ")>, color=\"" DFLT "\"];"]
            }
            delete edge2[e]
        }
    }
    # only edges not in first file
    for(e in edge2)
        edges[e "(" edge2[e] ")>, color=\"" GAIN "\"];"];  # expects label=<tag> to close

    print "digraph {"
    print "rankdir=LR;"
    print "charset=\"utf-8\";"
    print "LITERAL [shape=record];"
    print "labelloc=\"t\";"
    print "label=\"" de_path_ext(label1) " <=> " de_path_ext(label2) "\""

    n = asorti(edges,output)
    for(i=1;i<=n;i++ ){print output[i]}

    # include a Legend  rank [min,max,sink]
    print "\n\tnode[shape=plaintext];\n\tsubgraph cluster_key{rank=max;label=\"Color Legend\";"
    print "\tdflt [label=< Typical Case >];"
    print "\tgain [label=< New Edge >];"
    print "\tdrop [label=< Lost Edge >];"
    print "\tless [label=< Diminished Count >];"
    print "\tdflt -> dflt [color=\"" DFLT "\"];"
    print "\tgain -> gain [color=\"" GAIN "\"];"
    print "\tdrop -> drop [color=\"" DROP "\"];"
    print "\tless -> less [color=\"" LESS "\"];"

    print "\t}\n}\n"
}


More cleansing

More cleansing


Reassign publishers.

Reassign publishers.

git-svn-id: 293778eaa97c8c94097d610b1bd5133a8f478f36@30387 4f837ed2-42f5-46e7-a7a5-fa17313484d4

#!/usr/bin/awk -f
# cat *.tdb | tdbout -t publisher,plugin,publisher:info[tester],status,year

BEGIN {
  FS="\t"
  pn = 0
}

{
  # add a loop to add line only if either status is _not_ manifest or ending year is gt or eq to contract year
  current_year = 2012
  end_year = 0
  incontract = 0
  test_year = ""
  test_year = 2005
  if (length($5) > 3) {
    end_year = substr($5,length($5)-3,4)
  }
  #printf "%s\n", $0
  if (($4 != "manifest") || ((end_year >= test_year) && (end_year <= current_year))) {
    incontract = 1
  }

  if (incontract == 1) {
    nn = split($2,na,/\./)
    lp2 = na[nn]
    if (!(($1,lp2) in b)) {
      p[pn] = $1
      n[pn] = lp2
  #    n[pn] = $2
      r[pn] = $3
      if (r[pn] == "") {
        if ((substr(p[pn],1,2) <= "American H")) {
          r[pn] = "5"
        } else if ((substr(p[pn],1,2) >= "AMerican I") && (substr(p[pn],1,2) <= "Bz")) {
          r[pn] = "4"
        } else if ((substr(p[pn],1,2) >= "CA") && (substr(p[pn],1,2) <= "Nz")) {
          r[pn] = "5"
        } else if (substr(p[pn],1,1) >= "O") {
          r[pn] = "8"
        }
      }
      pn++
    }
    b[$1,lp2]++
    c[$1,lp2,$4]++
    x[$4]++
    tt++
  }
}

END {
  s[0] = "expected"
  s[1] = "exists"
  s[2] = "manifest"
  s[3] = "wanted"
  s[4] = "crawling"
  s[5] = "testing"
  s[6] = "notReady"
  s[7] = "released"
  s[8] = "down"
  s[9] = "superseded"
  s[10] = "zapped"
  sn = 11

  sc[0] = "expe"
  sc[1] = "exis"
  sc[2] = "mani"
  sc[3] = "want"
  sc[4] = "craw"
  sc[5] = "test"
  sc[6] = "notR"
  sc[7] = "rele"
  sc[8] = "down"
  sc[9] = "supe"
  sc[10] = "zapp"
  scn = 11

  #print out header
  printf "Publisher\tPlugin\tT\tTotal"
  for (j = 0 ; j < scn ; j++) {
    if (x[s[j]] > 0) {
    printf "\t%s", sc[j]
    }
  }
  printf "\n"

  #print out publisher, plugin, tester, total aus
  for (i = 0 ; i < pn ; i++) {
    printf "%s\t%s\t%s\t%d", p[i], n[i], r[i], b[p[i],n[i]]
    for (j = 0 ; j < sn ; j++) {
      if (x[s[j]] > 0){
      if (c[p[i],n[i],s[j]] == 0) {
      printf "\t.."
    } else {
        printf "\t%d", c[p[i],n[i],s[j]]
      }
    }
    }
    printf "\n"
  }
    #print out bottom line sums
    printf "Publisher\tPlugin\tT\t%d", tt
    for (j = 0 ; j < sn ; j++) {
      if (x[s[j]] > 0) {
        printf "\t%d", x[s[j]]
      }
    }

  printf "\n"
}

#!/usr/bin/awk -f
# cat *.tdb | tdbout -t publisher,plugin,publisher:info[tester],status,year

BEGIN {
  FS="\t"
  pn = 0
}

{
  # add a loop to add line only if either status is _not_ manifest or ending year is gt or eq to contract year
  current_year = 2012
  end_year = 0
  incontract = 0
  test_year = ""
  test_year = 2005
  if (length($5) > 3) {
    end_year = substr($5,length($5)-3,4)
  }
  #printf "%s\n", $0
  if (($4 != "manifest") || ((end_year >= test_year) && (end_year <= current_year))) {
    incontract = 1
  }

  if (incontract == 1) {
    nn = split($2,na,/\./)
    lp2 = na[nn]
    if (!(($1,lp2) in b)) {
      p[pn] = $1
      n[pn] = lp2
  #    n[pn] = $2
      r[pn] = $3
      if (r[pn] == "") {
        if ((substr(p[pn],1,2) <= "Am")) {
          r[pn] = "7"
        } else if ((substr(p[pn],1,2) >= "An") && (substr(p[pn],1,2) <= "Bz")) {
          r[pn] = "4"
        } else if ((substr(p[pn],1,2) >= "CA") && (substr(p[pn],1,2) <= "Nz")) {
          r[pn] = "5"
        } else if (substr(p[pn],1,1) >= "O") {
          r[pn] = "8"
        }
      }
      pn++
    }
    b[$1,lp2]++
    c[$1,lp2,$4]++
    x[$4]++
    tt++
  }
}

END {
  s[0] = "expected"
  s[1] = "exists"
  s[2] = "manifest"
  s[3] = "wanted"
  s[4] = "crawling"
  s[5] = "testing"
  s[6] = "notReady"
  s[7] = "released"
  s[8] = "down"
  s[9] = "superseded"
  s[10] = "zapped"
  sn = 11

  sc[0] = "expe"
  sc[1] = "exis"
  sc[2] = "mani"
  sc[3] = "want"
  sc[4] = "craw"
  sc[5] = "test"
  sc[6] = "notR"
  sc[7] = "rele"
  sc[8] = "down"
  sc[9] = "supe"
  sc[10] = "zapp"
  scn = 11

  #print out header
  printf "Publisher\tPlugin\tT\tTotal"
  for (j = 0 ; j < scn ; j++) {
    if (x[s[j]] > 0) {
    printf "\t%s", sc[j]
    }
  }
  printf "\n"

  #print out publisher, plugin, tester, total aus
  for (i = 0 ; i < pn ; i++) {
    printf "%s\t%s\t%s\t%d", p[i], n[i], r[i], b[p[i],n[i]]
    for (j = 0 ; j < sn ; j++) {
      if (x[s[j]] > 0){
      if (c[p[i],n[i],s[j]] == 0) {
      printf "\t.."
    } else {
        printf "\t%d", c[p[i],n[i],s[j]]
      }
    }
    }
    printf "\n"
  }
    #print out bottom line sums
    printf "Publisher\tPlugin\tT\t%d", tt
    for (j = 0 ; j < sn ; j++) {
      if (x[s[j]] > 0) {
        printf "\t%d", x[s[j]]
      }
    }

  printf "\n"
}


accomadate some exceptions, shorten predicate when an anchor is used

#! /usr/bin/awk -f

# remove first and last chars
function trim(str){
	return substr(str,2,length(str)-2)
}

# remove final element in slashed path
function stripid(uri){
	l = match(uri,/.*\//)  # side effect is RLENGTH
	if(l<=0)
		return uri
	else
		return substr(uri,1,RLENGTH -1)
}

# keep underscore, letters & numbers
# change the rest to (a single) underscore sans leading & trailing
function simplify(str){
	gsub(/[^[:alpha:][:digit:]_]+/,"_",str)
	gsub(/^_+|_+$/, "",str)
	gsub(/__*/,"_",str)
	return str
}

function contract(uri){
	if(uri in prefix)
		return prefix[uri]
	else
		for(ex in exception){
			start = match(uri, ex)
			if(0 < start)
				return exception[substr(uri, start, RLENGTH)]
		}
	# else
		return simplify(uri)
}

# get the final (identifier) portion of a slashed path
function final(uri){
	split(trim(uri),b,"/")
	p = b[length(b)]
	anchor = match(p, "#")
	if(anchor > 0)
		p = substr(p, anchor+1)
	return
}

BEGIN{
	# Monarchinitiave's dipper ingest files have cases
	# which escape the general rules
	# in everything
	exception["http://www.w3.org/2002/07"]="OWL_"
	# in go,mgi,wormbase
	exception["http://dx.doi.org"]="DOI"
	# in mgi
	exception["https://www.mousephenotype.org"]="IMPC_"
	# in panther
	exception["http://identifiers.org/wormbase"]="WormBase"
	# just until skolemized bnodes get in the curie map?
	exception["https://monarchinitiave.org/.well-known/genid"]="BNODE"
}

# main loop
# parse and stash the curie file (first file)
(FNR == NR) && /^'[^']*' *: 'http[^']*'.*/ {
	split($0,a,"'")
	prefix[stripid(a[4])]=a[2]
}
# process the ntriple file(s)  which are not the first file
### case when subject predicate and object are all uri
(FNR != NR) && /^<[^>]*> <[^>]*> <[^>]*> \.$/ {
	### Subject (uri)
	s = contract(stripid(trim($1)))
	### Predicate (uri)
	p = final($2)
	### Object (like subject)
	o = contract(stripid(trim($3)))
	edgelist[s " -> " o " [label=\"" p ]++
}
### case when the object is a literal
(FNR != NR) && /^<[^>]*> <[^>]*> "[^"]*" \.$/ {
	### Subject (uri)
	s = contract(stripid(trim($1)))
	### Predicate (uri)
	p = final($2)
	### not a uri
	o = "LITERAL"
	edgelist[s " -> " o " [label=\"" p ]++
	nodelist[o " [shape=record];"]++
}

END{
	print "digraph {"
	print "rankdir=LR;"
	print "charset=\"utf-8\";"
	for(edge in edgelist){
		print edge " (" edgelist[edge] ")\"];"
	}
	for(node in nodelist){
		print node
	}
	print "}"
}

#! /usr/bin/awk -f

# remove first and last chars
function trim(str){
	return substr(str,2,length(str)-2)
}

# remove final element in slashed path
function stripid(uri){
	l = match(uri,/.*\//)  # side effect is RLENGTH
	if(l<=0)
		return uri
	else
		return substr(uri,1,RLENGTH -1)
}

function contract(uri){
	if(uri in prefix)
		return prefix[uri]
	else {
		gsub(/[:./-]+/,"_",uri)
		return uri
	}
}

# parse and stash the curie file (first file)
(FNR == NR) && /^'[^']*' *: 'http[^']*'.*/ {
	split($0,a,"'")
	prefix[stripid(a[4])]=a[2]
}

# process the ntriple file(s)  which are not the first file
### all are uri
(FNR != NR) && /^<[^>]*> <[^>]*> <[^>]*> \.$/ {
	### Subject (uri)
	s = contract(stripid(trim($1)))
	### Predicate (uri)
	split(trim($2),b,"/")
	p = b[length(b)]
	### Object (like subject)
	o = contract(stripid(trim($3)))
	edgelist[s " -> " o " [label=\"" p "\"];"]++
}
### object is a literal
(FNR != NR) && /^<[^>]*> <[^>]*> "[^"]*" \.$/ {
	### Subject (uri)
	s = contract(stripid(trim($1)))
	### Predicate (uri)
	split(trim($2),b,"/")
	p = b[length(b)]
	### not a uri
	o = "LITERAL"
	edgelist[s " -> " o " [label=\"" p "\"];"]++
	nodelist[o " [shape=record];"]++
}

END{
	print "digraph {"
	print "rankdir=LR;"
	print "charset=\"utf-8\";"
	for(edge in edgelist){
		print edge
	}
	for(node in nodelist){
		print node
	}
	print "}"
}


Bugfix change manual->dhcp

Bugfix change manual->dhcp

function writeStatic(addr, nw, nm, gw, dns, device) {

    if (length(addr))
        print "    address ", addr

    if (length(nw))
        print "    network ", nw

    if (length(nm))
        print "    netmask ", nm

    if (length(gw))
        print "    gateway ", gw

    if (length(dns))
        print "    dns-nameservers ", dns

    if (length(br))
    {
        print "    bridge_ports", device
        print "    bridge_fd 0"
        print "    bridge_maxwait 0"
    }
}

function usage() {
        print "awk -f changeInterfaces.awk <interfaces file> dev=<eth device> \n" \
            "       [address=<ip addr>] [gateway=<ip addr>] [netmask=<ip addr>]\n" \
            "       [network=<ip addr>] [mode=add|dhcp|static|remove|manual] [dns=<ip addr [ip addr ...]>] [arg=debug]\n" \
            "       [bridge=<bridge device>]"
}

BEGIN { start = 0;
    dnsVal = "";

    if (ARGC < 3 || ARGC > 10) {
        usage();
        exit 1;
    }

    for (i = 2; i < ARGC; i++) {
        split(ARGV[i], pair, "=");
        if (pair[1] == "address")
            address = pair[2];
        else if (pair[1] == "gateway")
            gateway = pair[2];
        else if (pair[1] == "network")
            network = pair[2];
        else if (pair[1] == "netmask")
            netmask = pair[2];
        else if (pair[1] == "brigde")
            brigde = pair[2];
        else if (pair[1] == "dev")
            device = pair[2];
        else if (pair[1] == "dns") {
            if (!length(pair[2])) {
                dnsVal = "clear";
            } else {
                dnsVal = pair[2];
            }
        }
        else if (pair[1] == "arg" && pair[2] == "debug")
            debug = 1;
        else if (pair[1] == "mode" && pair[2] == "dhcp")
            dhcp = 1;
        else if (pair[1] == "mode" && pair[2] == "static")
            static = 1;
        else if (pair[1] == "mode" && pair[2] == "remove")
            remove = 1;
        else if (pair[1] == "mode" && pair[2] == "manual")
            manual = 1;
        else if (pair[1] == "mode" && pair[2] == "add")
            add = 1;
        else {
            usage();
            exit 1;
        }
    }

    # Sort out the logic of argument
    if (dhcp && (length(network) || length(gateway) || length(address) || length(netmask))) {
        print "Both DHCP and static properties are defined";
        usage();
        exit 1;
    }

}

{
    # maybe remove auto if manual is selected??
    if (($1 == "auto" && remove) || ($1 == "auto" && manual) ) {
        if($2 != device)
            print;
        next;
    }

    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static
    if ($1 == "iface")  {

        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {

            if (debug)
                print $0;

            # If remove is defined, switch on delete mode
            if (remove) {
                definedRemove=1;
            }

            # It's a DHCP interface, if defined any static properties
            # change it to static
            if (match($0, / dhcp/)) {
                definedDhcp=1;
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
                    next;
                }
        # change to manual if defined
                if (manual) {
                    sub(/ dhcp/, " manual");
                    print $0;
                    print "\n"
                    next;
                }
            }

            else if (match ($0, / manual/)) {
                definedManual=1;
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
                    next;
                }
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ manual/, " dhcp");
                    print $0;
                    print "\n"
                    next;
                }
            }

            # It's a static network interface
            else if (match ($0, / static/)) {
                definedStatic=1;
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ static/, " dhcp");
                    print $0;
                    next;
                }
                if (manual) {
                    sub(/ static/, " manual");
                    print $0;
                    next;
                }
            }

        }
        # If it is other inteface line, switch it off
        else {
            if (definedStatic) {
                if (length(dnsVal) && dnsVal != "clear") {
                    if (debug) {
                        print "Detected new iface defined but dns hasn't been updated yet";
                    }
                    print "    dns-nameservers ", dnsVal;
                }
            }

            definedStatic = 0;
            definedDhcp = 0;
            definedRemove = 0;
            definedManual = 0;
        }

        if (!definedRemove) {
            print $0;
            next;
        }
    }

    # Reaches here - means non iface lines
    # Change the static content
    if (definedStatic) {

        # Already defined static, just changing the properties
        # Otherwise omit everything until the iface section is
        # finished
        if (!dhcp && !manual) {

            if (debug)
                print "static - ", $0, $1;

            if ($1 == "address" && length(address))
                print "    address ", address

            else if ($1 == "netmask" && length(netmask))
                print "    netmask ", netmask;

            else if ($1 == "gateway" && length(gateway))
                print "    gateway ", gateway;

            else if ($1 == "network" && length(network))
                print "    network ", network;

            else if ($1 == "dns-nameservers") {
                # Overwrite it if dns is defined.
                # Clear the dns entry if the parameter is empty string
                if (length(dnsVal) && dnsVal != "clear") {
                    print "    dns-nameservers ", dnsVal;
                    # Important - to reset the dns. So that we know whether
                    # dns has been updated to the interfaces file
                    dnsVal = "";
                } else if (remove) {
                    dnsVal = "";
                } else if (!length(dnsVal)) {
                    print $0;
                }
            }

            else if (!definedRemove) {
                print $0;
            }
        }

        if ($1 == "auto" || $1 == "allow-hotplug" || $1 == "")
        {
            print
        }

        next;
    }

    # If already defined dhcp, then dump the network properties
    if (definedDhcp) {
        writeStatic(address, network, netmask, gateway, dnsVal);
        definedDhcp = 0;
        next;
    } else if (definedManual) {
        writeStatic(address, network, netmask, gateway, dnsVal);
        definedManual = 0;
        next;
    }

    if (!definedRemove) {
        print $0;
    }
}

END {
    if (definedDhcp) {
        # This bit is useful at the condition when the last line is
        # iface dhcp
        writeStatic(address, network, netmask, gateway, dnsVal, device);
    }
    else if (definedManual) {
        # This bit is useful at the condition when the last line is
        # iface dhcp
        writeStatic(address, network, netmask, gateway, dnsVal, device);
    }
    else if (definedStatic) {
        # Condition for last line and adding dns entry
        if (length(dnsVal) && dnsVal != "clear") {
            if (debug) {
                print "Detected end of line but dns hasn't been updated yet";
            }
            print "    dns-nameservers ", dnsVal;
        }
    }

    if(add)
    {
        print "\nauto", device;
        print "iface",device,"inet manual";
    }
}

function writeStatic(addr, nw, nm, gw, dns, device) {

    if (length(addr))
        print "    address ", addr

    if (length(nw))
        print "    network ", nw

    if (length(nm))
        print "    netmask ", nm

    if (length(gw))
        print "    gateway ", gw

    if (length(dns))
        print "    dns-nameservers ", dns

    if (length(br))
    {
        print "    bridge_ports", device
        print "    bridge_fd 0"
        print "    bridge_maxwait 0"
    }
}

function usage() {
        print "awk -f changeInterfaces.awk <interfaces file> dev=<eth device> \n" \
            "       [address=<ip addr>] [gateway=<ip addr>] [netmask=<ip addr>]\n" \
            "       [network=<ip addr>] [mode=add|dhcp|static|remove|manual] [dns=<ip addr [ip addr ...]>] [arg=debug]\n" \
            "       [bridge=<bridge device>]"
}

BEGIN { start = 0;
    dnsVal = "";

    if (ARGC < 3 || ARGC > 10) {
        usage();
        exit 1;
    }

    for (i = 2; i < ARGC; i++) {
        split(ARGV[i], pair, "=");
        if (pair[1] == "address")
            address = pair[2];
        else if (pair[1] == "gateway")
            gateway = pair[2];
        else if (pair[1] == "network")
            network = pair[2];
        else if (pair[1] == "netmask")
            netmask = pair[2];
        else if (pair[1] == "brigde")
            brigde = pair[2];
        else if (pair[1] == "dev")
            device = pair[2];
        else if (pair[1] == "dns") {
            if (!length(pair[2])) {
                dnsVal = "clear";
            } else {
                dnsVal = pair[2];
            }
        }
        else if (pair[1] == "arg" && pair[2] == "debug")
            debug = 1;
        else if (pair[1] == "mode" && pair[2] == "dhcp")
            dhcp = 1;
        else if (pair[1] == "mode" && pair[2] == "static")
            static = 1;
        else if (pair[1] == "mode" && pair[2] == "remove")
            remove = 1;
        else if (pair[1] == "mode" && pair[2] == "manual")
            manual = 1;
        else if (pair[1] == "mode" && pair[2] == "add")
            add = 1;
        else {
            usage();
            exit 1;
        }
    }

    # Sort out the logic of argument
    if (dhcp && (length(network) || length(gateway) || length(address) || length(netmask))) {
        print "Both DHCP and static properties are defined";
        usage();
        exit 1;
    }

}

{
    # maybe remove auto if manual is selected??
    if ($1 == "auto" && remove ) {
        if($2 != device)
            print;
        next;
    }

    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static
    if ($1 == "iface")  {

        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {

            if (debug)
                print $0;

            # If remove is defined, switch on delete mode
            if (remove) {
                definedRemove=1;
            }

            # It's a DHCP interface, if defined any static properties
            # change it to static
            if (match($0, / dhcp/)) {
                definedDhcp=1;
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
                    next;
                }
            }

            else if (match ($0, / manual/)) {
                definedManual=1;
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
                    next;
                }
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ manual/, " dhcp");
                    print $0;
                    next;
                }
            }

            # It's a static network interface
            else if (match ($0, / static/)) {
                definedStatic=1;
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ static/, " dhcp");
                    print $0;
                    next;
                }
                if (manual) {
                    sub(/ static/, " manual");
                    print $0;
                    next;
                }
            }

        }
        # If it is other inteface line, switch it off
        else {
            if (definedStatic) {
                if (length(dnsVal) && dnsVal != "clear") {
                    if (debug) {
                        print "Detected new iface defined but dns hasn't been updated yet";
                    }
                    print "    dns-nameservers ", dnsVal;
                }
            }

            definedStatic = 0;
            definedDhcp = 0;
            definedRemove = 0;
            definedManual = 0;
        }

        if (!definedRemove) {
            print $0;
            next;
        }
    }

    # Reaches here - means non iface lines
    # Change the static content
    if (definedStatic) {

        # Already defined static, just changing the properties
        # Otherwise omit everything until the iface section is
        # finished
        if (!dhcp && !manual) {

            if (debug)
                print "static - ", $0, $1;

            if ($1 == "address" && length(address))
                print "    address ", address

            else if ($1 == "netmask" && length(netmask))
                print "    netmask ", netmask;

            else if ($1 == "gateway" && length(gateway))
                print "    gateway ", gateway;

            else if ($1 == "network" && length(network))
                print "    network ", network;

            else if ($1 == "dns-nameservers") {
                # Overwrite it if dns is defined.
                # Clear the dns entry if the parameter is empty string
                if (length(dnsVal) && dnsVal != "clear") {
                    print "    dns-nameservers ", dnsVal;
                    # Important - to reset the dns. So that we know whether
                    # dns has been updated to the interfaces file
                    dnsVal = "";
                } else if (remove) {
                    dnsVal = "";
                } else if (!length(dnsVal)) {
                    print $0;
                }
            }

            else if (!definedRemove) {
                print $0;
            }
        }

        if ($1 == "auto" || $1 == "allow-hotplug" || $1 == "")
        {
            print
        }

        next;
    }

    # If already defined dhcp, then dump the network properties
    if (definedDhcp) {
        writeStatic(address, network, netmask, gateway, dnsVal);
        definedDhcp = 0;
        next;
    } else if (definedManual) {
        writeStatic(address, network, netmask, gateway, dnsVal);
        definedManual = 0;
        next;
    }

    if (!definedRemove) {
        print $0;
    }
}

END {
    if (definedDhcp) {
        # This bit is useful at the condition when the last line is
        # iface dhcp
        writeStatic(address, network, netmask, gateway, dnsVal, device);
    }
    else if (definedManual) {
        # This bit is useful at the condition when the last line is
        # iface dhcp
        writeStatic(address, network, netmask, gateway, dnsVal, device);
    }
    else if (definedStatic) {
        # Condition for last line and adding dns entry
        if (length(dnsVal) && dnsVal != "clear") {
            if (debug) {
                print "Detected end of line but dns hasn't been updated yet";
            }
            print "    dns-nameservers ", dnsVal;
        }
    }

    if(add)
    {
        print "\nauto", device;
        print "iface",device,"inet manual";
    }
}


Fix invisible comments - CTR

Fix invisible comments - CTR

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

BEGIN {
  firstMatch=1
  styled=0
}

/Licensed to the Apache Software Foundation/ {
  isHeader=1
}

/<\/style>/ {
  if (!styled) {
    print ".invisible {color: rgba(0,0,0,0); font-size: 0;}"
    styled=1
  }
}

!// {
  if (firstMatch || !isHeader) {
    print gensub(/()\(([0-9]+)\)(<\/b>)/,
                 "//\\1\\2\\3", "g")
  }
}

// {
  if (firstMatch || !isHeader) {
    print gensub(/\/\/<\/span>()\(([0-9]+)\)(<\/b>)/,
                 "//\\1\\2\\3\\\\<\/span>", "g")
  }
}

/under the License\./ {
  firstMatch=0
  isHeader=0
}

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

BEGIN {
  firstMatch=1
  styled=0
}

/Licensed to the Apache Software Foundation/ {
  isHeader=1
}

/<\/style>/ {
  if (!styled) {
    print ".invisible {position: absolute; left: 9999px}"
    styled=1
  }
}

!// {
  if (firstMatch || !isHeader) {
    print gensub(/()\(([0-9]+)\)(<\/b>)/,
                 "//\\1\\2\\3", "g")
  }
}

// {
  if (firstMatch || !isHeader) {
    print gensub(/\/\/<\/span>()\(([0-9]+)\)(<\/b>)/,
                 "//\\1\\2\\3\\\\<\/span>", "g")
  }
}

/under the License\./ {
  firstMatch=0
  isHeader=0
}


Fix for line breaks in argument lists.

Fix for line breaks in argument lists.

# Qick hack to convert pydoc to something decent looking in html.
# Author: Thomas Moeller
# Usage: pydoc3.3 inventor | awk -f pydoc2html.awk > inventor.html
#
# Example for updating a man directory:
# for f in *.html; do pydoc3.3 ${f%%.html} | awk -f ../../PyInventor/utils/pydoc2html.awk > $f; done
#
# Copyright (C) the PyInventor contributors. All rights reserved.
# This file is part of PyInventor, distributed under the BSD 3-Clause
# License. For full terms see the included COPYING file.
#

BEGIN   {
          eatEmptyLines = 0;
          indendet = 0;
          dataDesc = 0;
          isBullet = 0;
          print "<!DOCTYPE html>";
          print "<meta name='description' content='PyInventor : 3D Graphics in Python with Open Inventor' />";
          print "<link rel='stylesheet' type='text/css' media='screen' href='../stylesheets/stylesheet.css'>";
          print "<title>PyInventor</title>";
          print "
";
          print "View on GitHub";
          print "
PyInventor

3D Graphics in Python with Open Inventor
";
          print "
";

          print "

";
        }

END     {
          print "
";
          print "
";
          print "
PyInventor maintained by TheHubbit
";
          print "
Published with GitHub Pages

";
          print "";
        }

        { sub(/^[ \t\|]+/, ""); gsub(/[\<\>]/, ""); }
/^Help on/ { getline; next; }
/ = class / { print "
" $1 "

" $3 " " $4 "
"; module = substr($1, 0, index($1, ".") - 1); next; }
/^[ ]*FILE$/ { getline; next; }
/^[ ]*NAME$/ { getline; print "
" $1 "

module " $0 "
"; module = $1; next; }
/^[ ]*DESCRIPTION$/ { next; }
/^[ ]*FUNCTIONS$/ { print "
Functions:
"; next; }
/^[ ]*[A-Z]+$/ { print "
" $0 "
"; next; }
/PACKAGE CONTENTS/ {
            print "
Package contents:

";
            while (!match($0, /^[ \|\t\n]*$/))
            {
              getline;
              if ((length($0) > 2) && !match($0, "__main__"))
              {
                print "
" $0 "";
              }
            }
            print "
";
            isBullet = 0; next;
          }
/Method resolution order:/ {
            print "
" $0 "

";
            while (!match($0, /^[ \|\t]*$/))
            {
              getline;
              if (match($2, /PySide\./)) { link = $2; gsub(/\./, "/", link); print "
" $2 ""; } else
              if (match($2, /FieldContainer/)) print "
" $2 ""; else
              if (length($2) > 0) print "
" $2;
            }
            print "
";
            isBullet = 0; next;
          }
/Methods inherited from PySide/ { exit; }
/^[ ]*[A-Z][a-z]+ [A-Za-z \.]+:$/ { header = "
 " $0 "
"; eatEmptyLines = 1; if (match($0, /[ ]*Data /)) dataDesc = 1; next; }
/__/    { getline; next; }
/^[ \t]*[a-zA-Z0-9_]+\([A-Za-z0-9_\., ]+\)$/ {
          sub(/^[ \t]*/, "");
          if (indendet)
          {
            indendet = 0;
            print "
";
          }
          if (length(header) > 0)
          {
            print header;
            header = "";
          }
          print "
" $0 "
"; next;
        }
/Args:|Returns:|Note:/ {
          if (indendet)
          {
            indendet = 0;
            print "
";
          }
          print "
" $0 "
"; print "
"; indendet = 1; next;
        }
/^[ ]*\- [a-zA-Z0-9_, ]+: / {
          if (!isBullet) print "
";
          target = substr($2, 0, length($2) -1);
          if (target == "inventor") print "
" $2 "";
          else print "
" $2 "";
          sub(/^[ ]*\- [a-zA-Z0-9_, ]+: /, "");
          print;
          isBullet = 1; next;
        }
/^[ -]*[a-zA-Z0-9_, ]+: / { sub(/^[ -]+/, ""); print "
" $0; isBullet = 1; next; }
/-----/ { print ""; dataDesc = 0; next; }
        {
          sub(/^[ \t]*/, "");
          if (dataDesc)
          {
            if (indendet)
            {
              indendet = 0;
              print "
";
            }
            if ((length(header) > 0) && (length($0) > 0))
            {
              print header;
              header = "";
            }
            if (match($0, /^[ \t]*[a-zA-Z0-9_]+$/))
            {
              print "
" $0 "
";
            }
            else if (length($0) > 0)
            {
              if (index($0, " = ") > 0)
              {
                print $0 "
";
              }
              else print $0;
            }
          }
          else if (!eatEmptyLines || length($0) > 0)
          {
            print;
          }
          if (length($0) > 0) eatEmptyLines = 0; else if (isBullet)
          {
            print "
";
            isBullet = 0;
          }
        }

# Qick hack to convert pydoc to something decent looking in html.
# Author: Thomas Moeller
# Usage: pydoc3.3 inventor | awk -f pydoc2html.awk > inventor.html
#
# Example for updating a man directory:
# for f in *.html; do pydoc3.3 ${f%%.html} | awk -f ../../PyInventor/utils/pydoc2html.awk > $f; done
#
# Copyright (C) the PyInventor contributors. All rights reserved.
# This file is part of PyInventor, distributed under the BSD 3-Clause
# License. For full terms see the included COPYING file.
#

BEGIN   {
          eatEmptyLines = 0;
          indendet = 0;
          dataDesc = 0;
          isBullet = 0;
          print "<!DOCTYPE html>";
          print "<meta name='description' content='PyInventor : 3D Graphics in Python with Open Inventor' />";
          print "<link rel='stylesheet' type='text/css' media='screen' href='../stylesheets/stylesheet.css'>";
          print "<title>PyInventor</title>";
          print "
";
          print "View on GitHub";
          print "
PyInventor

3D Graphics in Python with Open Inventor
";
          print "
";

          print "

";
        }

END     {
          print "
";
          print "
";
          print "
PyInventor maintained by TheHubbit
";
          print "
Published with GitHub Pages

";
          print "";
        }

        { sub(/^[ \t\|]+/, ""); gsub(/[\<\>]/, ""); }
/^Help on/ { getline; next; }
/ = class / { print "
" $1 "

" $3 " " $4 "
"; module = substr($1, 0, index($1, ".") - 1); next; }
/^[ ]*FILE$/ { getline; next; }
/^[ ]*NAME$/ { getline; print "
" $1 "

module " $0 "
"; module = $1; next; }
/^[ ]*DESCRIPTION$/ { next; }
/^[ ]*FUNCTIONS$/ { print "
Functions:
"; next; }
/^[ ]*[A-Z]+$/ { print "
" $0 "
"; next; }
/PACKAGE CONTENTS/ {
            print "
Package contents:

";
            while (!match($0, /^[ \|\t\n]*$/))
            {
              getline;
              if ((length($0) > 2) && !match($0, "__main__"))
              {
                print "
" $0 "";
              }
            }
            print "
";
            isBullet = 0; next;
          }
/Method resolution order:/ {
            print "
" $0 "

";
            while (!match($0, /^[ \|\t]*$/))
            {
              getline;
              if (match($2, /PySide\./)) { link = $2; gsub(/\./, "/", link); print "
" $2 ""; } else
              if (match($2, /FieldContainer/)) print "
" $2 ""; else
              if (length($2) > 0) print "
" $2;
            }
            print "
";
            isBullet = 0; next;
          }
/Methods inherited from PySide/ { exit; }
/^[ ]*[A-Z][a-z]+ [A-Za-z \.]+:$/ { header = "
 " $0 "
"; eatEmptyLines = 1; if (match($0, /[ ]*Data /)) dataDesc = 1; next; }
/__/    { getline; next; }
/^[ \t]*[a-zA-Z0-9_]+\([A-Za-z0-9_\., ]+\)$/ {
          sub(/^[ \t]*/, "");
          if (indendet)
          {
            indendet = 0;
            print "
";
          }
          if (length(header) > 0)
          {
            print header;
            header = "";
          }
          print "
" $0 "
"; next;
        }
/Args:|Returns:|Note:/ {
          if (indendet)
          {
            indendet = 0;
            print "
";
          }
          print "
" $0 "
"; print "
"; indendet = 1; next;
        }
/^[ ]*\- [a-zA-Z0-9_, ]+: / {
          if (!isBullet) print "
";
          target = substr($2, 0, length($2) -1);
          if (target == "inventor") print "
" $2 "";
          else print "
" $2 "";
          sub(/^[ ]*\- [a-zA-Z0-9_, ]+: /, "");
          print;
          isBullet = 1; next;
        }
/^[ -]+[a-zA-Z0-9_, ]+: / { sub(/^[ -]+/, ""); print "
" $0; isBullet = 1; next; }
/-----/ { print ""; dataDesc = 0; next; }
        {
          sub(/^[ \t]*/, "");
          if (dataDesc)
          {
            if (indendet)
            {
              indendet = 0;
              print "
";
            }
            if ((length(header) > 0) && (length($0) > 0))
            {
              print header;
              header = "";
            }
            if (match($0, /^[ \t]*[a-zA-Z0-9_]+$/))
            {
              print "
" $0 "
";
            }
            else if (length($0) > 0)
            {
              if (index($0, " = ") > 0)
              {
                print $0 "
";
              }
              else print $0;
            }
          }
          else if (!eatEmptyLines || length($0) > 0)
          {
            print;
          }
          if (length($0) > 0) eatEmptyLines = 0; else if (isBullet)
          {
            print "
";
            isBullet = 0;
          }
        }