make/scripts/pandoc-troff-manpage-filter.js
author chegar
Thu, 17 Oct 2019 20:27:44 +0100
branchdatagramsocketimpl-branch
changeset 58677 13588c901957
parent 52940 26e2cfebcfba
permissions -rw-r--r--
Initial changes for datagramsocketimpl-branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52714
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     1
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     2
// Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     3
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     4
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     5
// This code is free software; you can redistribute it and/or modify it
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     6
// under the terms of the GNU General Public License version 2 only, as
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     7
// published by the Free Software Foundation.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     8
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
     9
// This code is distributed in the hope that it will be useful, but WITHOUT
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    10
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    11
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    12
// version 2 for more details (a copy is included in the LICENSE file that
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    13
// accompanied this code).
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    14
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    15
// You should have received a copy of the GNU General Public License version
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    16
// 2 along with this work; if not, write to the Free Software Foundation,
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    17
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    18
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    19
// Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    20
// or visit www.oracle.com if you need additional information or have any
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    21
// questions.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    22
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    23
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    24
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    25
// Traverse a tree of pandoc format objects, calling callback on each
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    26
// element, and replacing it if callback returns a new object.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    27
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    28
// Inspired by the walk method in
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    29
// https://github.com/jgm/pandocfilters/blob/master/pandocfilters.py
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    30
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    31
function traverse(obj, callback) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    32
    if (Array.isArray(obj)) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    33
        var processed_array = [];
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    34
        obj.forEach(function(elem) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    35
            if (elem === Object(elem) && elem.t) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    36
                var replacement = callback(elem.t, elem.c || []);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    37
                if (!replacement) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    38
                    // no replacement object returned, use original
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    39
                    processed_array.push(traverse(elem, callback));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    40
                } else if (Array.isArray(replacement)) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    41
                    // array of objects returned, splice all elements into array
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    42
                    replacement.forEach(function(repl_elem) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    43
                        processed_array.push(traverse(repl_elem, callback));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    44
                    })
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    45
                } else {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    46
                    // replacement object given, traverse it
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    47
                    processed_array.push(traverse(replacement, callback));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    48
                }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    49
            } else {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    50
                processed_array.push(traverse(elem, callback));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    51
            }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    52
        })
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    53
        return processed_array;
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    54
    } else if (obj === Object(obj)) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    55
        var processed_obj = {};
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    56
        Object.keys(obj).forEach(function(key) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    57
            processed_obj[key] = traverse(obj[key], callback);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    58
        })
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    59
        return processed_obj;
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    60
    } else {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    61
        return obj;
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    62
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    63
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    64
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    65
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    66
// Helper constructors to create pandoc format objects
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    67
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    68
function Space() {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    69
    return { 't': 'Space', 'c': [] };
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    70
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    71
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    72
function Str(value) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    73
    return { 't': 'Str', 'c': value };
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    74
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    75
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    76
function Strong(value) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    77
    return { 't': 'Strong', 'c': value };
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    78
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    79
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    80
function Header(value) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    81
    return { 't': 'Header', 'c': value };
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    82
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    83
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    84
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    85
// Callback to change all Str texts to upper case
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    86
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    87
function uppercase(type, value) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    88
    if (type === 'Str') {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    89
        return Str(value.toUpperCase());
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    90
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    91
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    92
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    93
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    94
// Main callback function that performs our man page AST rewrites
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    95
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    96
function manpage_filter(type, value) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    97
    // If it is a header, decrease the heading level by one, and
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    98
    // if it is a level 1 header, convert it to upper case.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
    99
    if (type === 'Header') {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   100
        value[0] = Math.max(1, value[0] - 1);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   101
        if (value[0] == 1) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   102
            return Header(traverse(value, uppercase));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   103
        }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   104
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   105
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   106
    // Man pages does not have superscript. We use it for footnotes, so
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   107
    // enclose in [...] for best representation.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   108
    if (type === 'Superscript') {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   109
        return [ Str('['), value[0], Str(']') ];
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   110
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   111
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   112
    // If it is a link, put the link name in bold. If it is an external
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   113
    // link, put it in brackets. Otherwise, it is either an internal link
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   114
    // (like "#next-heading"), or a relative link to another man page
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   115
    // (like "java.html"), so remove it for man pages.
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   116
    if (type === 'Link') {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   117
        var target = value[2][0];
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   118
        if (target.match(/^http[s]?:/)) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   119
            return [ Strong(value[1]), Space(), Str('[' + target + ']') ];
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   120
        } else {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   121
            return Strong(value[1]);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   122
        }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   123
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   124
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   125
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   126
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   127
// Main function
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   128
//
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   129
function main() {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   130
    var input = "";
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   131
    while (line = readLine()) {
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   132
        input = input.concat(line);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   133
    }
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   134
    var json = JSON.parse(input);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   135
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   136
    var transformed_json = traverse(json, manpage_filter);
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   137
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   138
    print(JSON.stringify(transformed_json));
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   139
}
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   140
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   141
// ... and execute it
2e52aa822c57 8178317: Create man pages using pandoc from markdown sources
ihse
parents:
diff changeset
   142
main();