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