Skip to content

Commit cd651b9

Browse files
committed
8246435: Replace Javascript implementation of pandoc filters with Java
Reviewed-by: erikj
1 parent 71c926c commit cd651b9

23 files changed

Lines changed: 1510 additions & 308 deletions

make/CompileToolsJdk.gmk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ ifeq ($(ENABLE_PANDOC), true)
103103
SOURCE_FILES := $(TOPDIR)/make/scripts/pandoc-troff-manpage-filter.sh.template, \
104104
OUTPUT_FILE := $(PANDOC_TROFF_MANPAGE_FILTER), \
105105
REPLACEMENTS := \
106-
@@JJS@@ => $(JJS) ; \
107-
@@TOPDIR@@ => $(TOPDIR) ; \
106+
@@JAVA_SMALL@@ => $(JAVA_SMALL) ; \
107+
@@BUILDTOOLS_OUTPUTDIR@@ => $(BUILDTOOLS_OUTPUTDIR) ; \
108108
))
109109

110110
# Created script must be made executable
@@ -126,8 +126,8 @@ ifeq ($(ENABLE_PANDOC), true)
126126
SOURCE_FILES := $(TOPDIR)/make/scripts/pandoc-html-manpage-filter.sh.template, \
127127
OUTPUT_FILE := $(PANDOC_HTML_MANPAGE_FILTER), \
128128
REPLACEMENTS := \
129-
@@JJS@@ => $(JJS) ; \
130-
@@TOPDIR@@ => $(TOPDIR) ; \
129+
@@JAVA_SMALL@@ => $(JAVA_SMALL) ; \
130+
@@BUILDTOOLS_OUTPUTDIR@@ => $(BUILDTOOLS_OUTPUTDIR) ; \
131131
))
132132

133133
# Created script must be made executable

make/Docs.gmk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,9 @@ ifeq ($(ENABLE_PANDOC), true)
610610
# PANDOC_HTML_MANPAGE_FILTER, a wrapper around
611611
# PANDOC_HTML_MANPAGE_FILTER_JAVASCRIPT. This is created by buildtools-jdk.
612612

613-
# We should also depend on the source javascript filter
614-
PANDOC_HTML_MANPAGE_FILTER_JAVASCRIPT := \
615-
$(TOPDIR)/make/scripts/pandoc-html-manpage-filter.js
613+
# We should also depend on the source code for the filter
614+
PANDOC_HTML_MANPAGE_FILTER_SOURCE := $(call FindFiles, \
615+
$(TOPDIR)/make/jdk/src/classes/build/tools/pandocfilter)
616616

617617
$(foreach m, $(ALL_MODULES), \
618618
$(eval MAN_$m := $(call FindModuleManDirs, $m)) \
@@ -632,7 +632,7 @@ ifeq ($(ENABLE_PANDOC), true)
632632
OPTIONS := --toc -V include-before='$(SPECS_TOP)' -V include-after='$(SPECS_BOTTOM_1)', \
633633
POST_PROCESS := $(TOOL_FIXUPPANDOC), \
634634
EXTRA_DEPS := $(PANDOC_HTML_MANPAGE_FILTER) \
635-
$(PANDOC_HTML_MANPAGE_FILTER_JAVASCRIPT), \
635+
$(PANDOC_HTML_MANPAGE_FILTER_SOURCE), \
636636
)) \
637637
$(eval JDK_SPECS_TARGETS += $($($m_$f_NAME))) \
638638
) \

make/autoconf/boot-jdk.m4

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,6 @@ AC_DEFUN_ONCE([BOOTJDK_SETUP_BOOT_JDK],
381381
BOOTJDK_USE_LOCAL_CDS=false
382382
AC_MSG_RESULT([no, -XX:SharedArchiveFile not supported])
383383
fi
384-
385-
# Check for jjs in bootjdk
386-
UTIL_SETUP_TOOL(JJS,
387-
[
388-
AC_MSG_CHECKING([for jjs in Boot JDK])
389-
JJS=$BOOT_JDK/bin/jjs
390-
if test ! -x $JJS; then
391-
AC_MSG_RESULT(not found)
392-
JJS=""
393-
AC_MSG_NOTICE([Cannot use pandoc without jjs])
394-
ENABLE_PANDOC=false
395-
else
396-
AC_MSG_RESULT(ok)
397-
fi
398-
AC_SUBST(JJS)
399-
])
400384
])
401385

402386
AC_DEFUN_ONCE([BOOTJDK_SETUP_BOOT_JDK_ARGUMENTS],

make/autoconf/spec.gmk.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ JAR_CMD:=@JAR@
625625
JLINK_CMD := @JLINK@
626626
JMOD_CMD := @JMOD@
627627
JARSIGNER_CMD:=@JARSIGNER@
628-
JJS_CMD:=@JJS@
629628
# These variables are meant to be used. They are defined with = instead of := to make
630629
# it possible to override only the *_CMD variables.
631630
JAVA=@FIXPATH@ $(JAVA_CMD) $(JAVA_FLAGS_BIG) $(JAVA_FLAGS)
@@ -637,7 +636,6 @@ JAR=@FIXPATH@ $(JAR_CMD)
637636
JLINK = @FIXPATH@ $(JLINK_CMD)
638637
JMOD = @FIXPATH@ $(JMOD_CMD) $(JAVA_TOOL_FLAGS_SMALL)
639638
JARSIGNER=@FIXPATH@ $(JARSIGNER_CMD)
640-
JJS=@FIXPATH@ $(JJS_CMD) $(JAVA_TOOL_FLAGS_SMALL)
641639

642640
BUILD_JAVA_FLAGS := @BOOTCYCLE_JVM_ARGS_BIG@
643641
BUILD_JAVA=@FIXPATH@ $(BUILD_JDK)/bin/java $(BUILD_JAVA_FLAGS)

make/common/modules/LauncherCommon.gmk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ ifeq ($(call isTargetOsType, unix), true)
199199
# PANDOC_TROFF_MANPAGE_FILTER, a wrapper around
200200
# PANDOC_TROFF_MANPAGE_FILTER_JAVASCRIPT. This is created by buildtools-jdk.
201201

202-
# We should also depend on the source javascript filter
203-
PANDOC_TROFF_MANPAGE_FILTER_JAVASCRIPT := \
204-
$(TOPDIR)/make/scripts/pandoc-troff-manpage-filter.js
202+
# We should also depend on the source code for the filter
203+
PANDOC_TROFF_MANPAGE_FILTER_SOURCE := $(call FindFiles, \
204+
$(TOPDIR)/make/jdk/src/classes/build/tools/pandocfilter)
205205

206206
# The norm in man pages is to display code literals as bold, but pandoc
207207
# "correctly" converts these constructs (encoded in markdown using `...`
@@ -231,7 +231,7 @@ ifeq ($(call isTargetOsType, unix), true)
231231
@@VERSION_SHORT@@ => $(VERSION_SHORT) ; \
232232
@@VERSION_SPECIFICATION@@ => $(VERSION_SPECIFICATION), \
233233
EXTRA_DEPS := $(PANDOC_TROFF_MANPAGE_FILTER) \
234-
$(PANDOC_TROFF_MANPAGE_FILTER_JAVASCRIPT), \
234+
$(PANDOC_TROFF_MANPAGE_FILTER_SOURCE), \
235235
))
236236

237237
TARGETS += $(BUILD_MAN_PAGES)

make/conf/jib-profiles.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,6 @@ var getJibProfilesProfiles = function (input, common, data) {
604604
dependencies: [ name + ".jdk" ],
605605
configure_args: [
606606
"--with-boot-jdk=" + input.get(name + ".jdk", "home_path"),
607-
// Full docs do not currently work with bootcycle build
608-
// since Nashorn was removed. This negates the
609-
// --enable-full-docs from the main profile.
610-
"--enable-full-docs=auto",
611607
]
612608
}
613609
profiles[bootcyclePrebuiltName] = concatObjects(profiles[name],
@@ -765,7 +761,7 @@ var getJibProfilesProfiles = function (input, common, data) {
765761
profiles[cmpBaselineName].make_args = [ "COMPARE_BUILD=CONF=" ];
766762
profiles[cmpBaselineName].configure_args = concat(
767763
profiles[cmpBaselineName].configure_args,
768-
"--with-hotspot-build-time=n/a",
764+
"--with-hotspot-build-time=n/a",
769765
"--disable-precompiled-headers");
770766
// Do not inherit artifact definitions from base profile
771767
delete profiles[cmpBaselineName].artifacts;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package build.tools.pandocfilter;
2+
3+
import build.tools.pandocfilter.json.JSON;
4+
import build.tools.pandocfilter.json.JSONArray;
5+
import build.tools.pandocfilter.json.JSONObject;
6+
import build.tools.pandocfilter.json.JSONString;
7+
import build.tools.pandocfilter.json.JSONValue;
8+
9+
import java.io.BufferedReader;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileReader;
12+
import java.io.InputStreamReader;
13+
import java.util.Map;
14+
15+
public class PandocFilter {
16+
/**
17+
* Traverse a tree of pandoc format objects, calling callback on each
18+
* element, and replacing it if callback returns a new object.
19+
* <p>
20+
* Inspired by the walk method in
21+
* https://github.com/jgm/pandocfilters/blob/master/pandocfilters.py
22+
*/
23+
public JSONValue traverse(JSONValue obj, Callback callback, boolean deep) {
24+
if (obj instanceof JSONArray) {
25+
JSONArray array = (JSONArray) obj;
26+
27+
JSONArray processed_array = new JSONArray();
28+
for (JSONValue elem : array) {
29+
if (elem instanceof JSONObject && elem.contains("t")) {
30+
JSONValue replacement = callback.invoke(elem.get("t").asString(), elem.contains("c") ? elem.get("c") : new JSONArray());
31+
if (replacement == null) {
32+
// no replacement object returned, use original
33+
processed_array.add(traverse(elem, callback, deep));
34+
} else if (replacement instanceof JSONArray) {
35+
// array of objects returned, splice all elements into array
36+
JSONArray replacement_array = (JSONArray) replacement;
37+
for (JSONValue repl_elem : replacement_array) {
38+
processed_array.add(traverse(repl_elem, callback, deep));
39+
}
40+
} else {
41+
// replacement object given, traverse it
42+
processed_array.add(traverse(replacement, callback, deep));
43+
}
44+
} else {
45+
processed_array.add(traverse(elem, callback, deep));
46+
}
47+
}
48+
return processed_array;
49+
} else if (obj instanceof JSONObject) {
50+
if (deep && obj.contains("t")) {
51+
JSONValue replacement = callback.invoke(obj.get("t").asString(), obj.contains("c") ? obj.get("c") : new JSONArray());
52+
if (replacement != null) {
53+
return replacement;
54+
}
55+
} JSONObject obj_obj = (JSONObject) obj;
56+
var processed_obj = new JSONObject();
57+
for (String key : obj_obj.keys()) {
58+
processed_obj.put(key, traverse(obj_obj.get(key), callback, deep));
59+
}
60+
return processed_obj;
61+
} else {
62+
return obj;
63+
}
64+
}
65+
66+
public JSONValue createPandocNode(String type, JSONValue content) {
67+
if (content == null) {
68+
return new JSONObject(Map.of(
69+
"t", new JSONString(type)));
70+
} else {
71+
return new JSONObject(Map.of(
72+
"t", new JSONString(type),
73+
"c", content));
74+
}
75+
}
76+
77+
public JSONValue createPandocNode(String type) {
78+
return createPandocNode(type, null);
79+
}
80+
81+
/*
82+
* Helper constructors to create pandoc format objects
83+
*/
84+
public JSONValue createSpace() {
85+
return createPandocNode("Space");
86+
}
87+
88+
public JSONValue createStr(String string) {
89+
return createPandocNode("Str", new JSONString(string));
90+
}
91+
92+
public static JSONValue loadJson(String[] args) throws FileNotFoundException {
93+
StringBuffer input = new StringBuffer();
94+
InputStreamReader reader;
95+
if (args.length > 0)
96+
reader = new FileReader(args[0]);
97+
else {
98+
reader = new InputStreamReader(System.in);
99+
}
100+
new BufferedReader(reader).lines().forEach(line -> input.append(line));
101+
102+
return JSON.parse(input.toString());
103+
}
104+
105+
public interface Callback {
106+
JSONValue invoke(String type, JSONValue value);
107+
}
108+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2018, 2020, 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+
package build.tools.pandocfilter;
25+
26+
import build.tools.pandocfilter.json.JSONArray;
27+
import build.tools.pandocfilter.json.JSONObject;
28+
import build.tools.pandocfilter.json.JSONValue;
29+
30+
import java.io.FileNotFoundException;
31+
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
33+
34+
public class PandocManPageHtmlFilter extends PandocFilter {
35+
36+
private JSONValue MetaInlines(JSONValue value) {
37+
return createPandocNode("MetaInlines", value);
38+
}
39+
40+
private JSONValue changeTitle(String type, JSONValue value) {
41+
if (type.equals("MetaInlines")) {
42+
String subType = value.get(0).get("t").asString();
43+
String subContent = value.get(0).get("c").asString();
44+
if (subType.equals("Str")) {
45+
Pattern pattern = Pattern.compile("^([A-Z0-9]+)\\([0-9]+\\)$");
46+
Matcher matcher = pattern.matcher(subContent);
47+
if (matcher.find()) {
48+
String commandName = matcher.group(1).toLowerCase();
49+
return MetaInlines(new JSONArray(
50+
createStr("The"), createSpace(),
51+
createStr(commandName),
52+
createSpace(), createStr("Command")));
53+
}
54+
}
55+
}
56+
return null;
57+
}
58+
59+
/**
60+
* Main function
61+
*/
62+
public static void main(String[] args) throws FileNotFoundException {
63+
JSONValue json = loadJson(args);
64+
65+
PandocManPageHtmlFilter filter = new PandocManPageHtmlFilter();
66+
67+
JSONValue meta = json.get("meta");
68+
if (meta != null && meta instanceof JSONObject) {
69+
JSONObject metaobj = (JSONObject) meta;
70+
metaobj.remove("date");
71+
JSONValue title = meta.get("title");
72+
if (title != null) {
73+
metaobj.put("title", filter.traverse(title, filter::changeTitle, true));
74+
}
75+
}
76+
77+
System.out.println(json);
78+
}
79+
}

0 commit comments

Comments
 (0)