make/scripts/pandoc-html-manpage-filter.js
author herrick
Fri, 08 Nov 2019 15:27:05 -0500
branchJDK-8200758-branch
changeset 58995 de1413ae214c
parent 53228 e7738fd1c974
permissions -rw-r--r--
resources missed in 8233636 push
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52940
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     1
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     2
// Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     3
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     4
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     5
// This code is free software; you can redistribute it and/or modify it
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     6
// under the terms of the GNU General Public License version 2 only, as
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     7
// published by the Free Software Foundation.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     8
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
     9
// This code is distributed in the hope that it will be useful, but WITHOUT
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    10
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    11
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    12
// version 2 for more details (a copy is included in the LICENSE file that
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    13
// accompanied this code).
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    14
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    15
// You should have received a copy of the GNU General Public License version
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    16
// 2 along with this work; if not, write to the Free Software Foundation,
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    17
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    18
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    19
// Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    20
// or visit www.oracle.com if you need additional information or have any
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    21
// questions.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    22
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    23
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    24
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    25
// Traverse a tree of pandoc format objects, calling callback on each
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    26
// element, and replacing it if callback returns a new object.
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    27
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    28
// Inspired by the walk method in
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    29
// https://github.com/jgm/pandocfilters/blob/master/pandocfilters.py
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    30
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    31
function traverse(obj, callback) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    32
    if (Array.isArray(obj)) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    33
        var processed_array = [];
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    34
        obj.forEach(function(elem) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    35
            if (elem === Object(elem) && elem.t) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    36
                var replacement = callback(elem.t, elem.c || []);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    37
                if (!replacement) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    38
                    // no replacement object returned, use original
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    39
                    processed_array.push(traverse(elem, callback));
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    40
                } else if (Array.isArray(replacement)) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    41
                    // array of objects returned, splice all elements into array
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    42
                    replacement.forEach(function(repl_elem) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    43
                        processed_array.push(traverse(repl_elem, callback));
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    44
                    })
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    45
                } else {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    46
                    // replacement object given, traverse it
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    47
                    processed_array.push(traverse(replacement, callback));
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    48
                }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    49
            } else {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    50
                processed_array.push(traverse(elem, callback));
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    51
            }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    52
        })
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    53
        return processed_array;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    54
    } else if (obj === Object(obj)) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    55
        if (obj.t) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    56
            var replacement = callback(obj.t, obj.c || []);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    57
            if (replacement) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    58
                return replacement;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    59
            }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    60
        }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    61
        var processed_obj = {};
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    62
        Object.keys(obj).forEach(function(key) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    63
            processed_obj[key] = traverse(obj[key], callback);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    64
        })
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    65
        return processed_obj;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    66
    } else {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    67
        return obj;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    68
    }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    69
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    70
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    71
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    72
// Helper constructors to create pandoc format objects
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    73
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    74
function Space() {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    75
    return { 't': 'Space' };
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    76
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    77
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    78
function Str(value) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    79
    return { 't': 'Str', 'c': value };
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    80
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    81
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    82
function MetaInlines(value) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    83
    return { 't': 'MetaInlines', 'c': value };
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    84
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    85
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    86
function change_title(type, value) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    87
    if (type === 'MetaInlines') {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    88
        if (value[0].t === 'Str') {
53228
e7738fd1c974 8215308: pandoc-html-manpage-filter.js does not work for [un]pack200
jjg
parents: 52940
diff changeset
    89
            var match = value[0].c.match(/^([A-Z0-9]+)\([0-9]+\)$/);
52940
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    90
            if (match) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    91
                return MetaInlines([
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    92
                        Str("The"), Space(),
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    93
			Str(match[1].toLowerCase()),
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    94
			Space(), Str("Command")
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    95
		    ]);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    96
            }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    97
        }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    98
    }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
    99
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   100
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   101
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   102
// Main function
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   103
//
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   104
function main() {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   105
    var input = "";
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   106
    while (line = readLine()) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   107
        input = input.concat(line);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   108
    }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   109
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   110
    var json = JSON.parse(input);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   111
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   112
    var meta = json.meta;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   113
    if (meta) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   114
        meta.date = undefined;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   115
        var title = meta.title;
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   116
        if (meta.title) {
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   117
            meta.title = traverse(meta.title, change_title);
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   118
        }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   119
    }
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   120
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   121
    print(JSON.stringify(json));
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   122
}
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   123
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   124
// ... and execute it
26e2cfebcfba 8214720: Add pandoc filter to improve html man page output
ihse
parents:
diff changeset
   125
main();