Skip to content

8348282: Add option for syntax highlighting in javadoc snippets #24417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion make/Docs.gmk
Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ JAVA_WARNINGS_ARE_ERRORS ?= -Werror
JAVADOC_OPTIONS := -use -keywords -notimestamp \
-serialwarn -encoding ISO-8859-1 -docencoding UTF-8 -breakiterator \
-splitIndex --system none -javafx --expand-requires transitive \
--override-methods=summary
--override-methods=summary --syntax-highlight

# The reference options must stay stable to allow for comparisons across the
# development cycle.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -311,6 +311,12 @@ protected void generateOtherFiles(ClassTree classTree)
copyFontResources();
}

var syntaxHighlight = options.syntaxHighlight();
if (syntaxHighlight) {
copyResource(DocPaths.HIGHLIGHT_CSS, DocPaths.RESOURCE_FILES.resolve(DocPaths.HIGHLIGHT_CSS), true);
copyResource(DocPaths.HIGHLIGHT_JS, DocPaths.SCRIPT_FILES.resolve(DocPaths.HIGHLIGHT_JS), true);
}

// If a stylesheet file is not specified, copy the default stylesheet
// and replace newline with platform-specific newline.
if (options.stylesheetFile().isEmpty()) {
Original file line number Diff line number Diff line change
@@ -511,6 +511,7 @@ public void printHtmlDocument(List<String> metakeywords,
.setStylesheets(configuration.getMainStylesheet(), additionalStylesheets, localStylesheets)
.setAdditionalScripts(configuration.getAdditionalScripts())
.setIndex(options.createIndex(), mainBodyScript)
.setSyntaxHighlight(options.syntaxHighlight())
.addContent(extraHeadContent);

HtmlDocument htmlDocument = new HtmlDocument(
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -198,6 +198,11 @@ public class HtmlOptions extends BaseOptions {
*/
private String stylesheetFile = "";

/**
* Argument for command line option {@code --syntax-highlight}.
*/
private boolean syntaxHighlight = false;

/**
* Argument for command-line option {@code -tagletpath}.
* The path to Taglets
@@ -423,6 +428,14 @@ public boolean process(String opt, List<String> args) {
}
},

new Option(resources, "--syntax-highlight") {
@Override
public boolean process(String opt, List<String> args) {
syntaxHighlight = true;
return true;
}
},

new Option(resources, "-tag", 1) {
@Override
public boolean process(String opt, List<String> args) {
@@ -806,6 +819,13 @@ String stylesheetFile() {
return stylesheetFile;
}

/**
* Argument for command line option {@code --syntax-highlight}.
* True if command line option "--syntax-highlight" is used and syntax
* highlighting should be enabled. Default value is false.
*/
public boolean syntaxHighlight() { return syntaxHighlight; }

/**
* Argument for command-line option {@code -tagletpath}.
* The path to Taglets
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ public class Head extends Content {
private final List<Content> extraContent;
private boolean addDefaultScript = true;
private DocPath canonicalLink;
private boolean syntaxHighlight = false;

/**
* Creates a {@code Head} object, for a given file and HTML version.
@@ -238,6 +239,16 @@ public void setCanonicalLink(DocPath link) {
this.canonicalLink = link;
}

/**
* Enables or disables support for syntax highlighting.
* @param value {@code true} to enable syntax highligting
* @return this object
*/
public Head setSyntaxHighlight(boolean value) {
this.syntaxHighlight = value;
return this;
}

/**
* Adds additional content to be included in the HEAD element.
*
@@ -339,6 +350,11 @@ private void addStylesheets(HtmlTree head) {
addStylesheet(head, DocPaths.RESOURCE_FILES.resolve(path));
}

if (syntaxHighlight) {
addStylesheet(head, DocPaths.RESOURCE_FILES.resolve(DocPaths.HIGHLIGHT_CSS));
addScriptElement(head, DocPaths.HIGHLIGHT_JS);
}

for (DocPath path : localStylesheets) {
// Local stylesheets are contained in doc-files, so omit resource-files prefix
addStylesheet(head, path);
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
/* Syntax highlight style sheet */
.hljs-title.function_,
.hljs-template-variable {
color: #00738F;
}
.hljs-code,
.hljs-comment,
.hljs-quote {
color: #6e6e71;
font-style: italic;
}
.hljs-meta {
color: #836F00;
}
.hljs-symbol,
.hljs-template-tag,
.hljs-keyword,
.hljs-literal,
.hljs-name,
.hljs-built_in,
.hljs-char.escape_ {
color: #0C40C2;
}
.hljs-variable,
.hljs-property,
.hljs-attr,
.hljs-section {
color: #841191;
}
.hljs-attribute {
color: #164ad9;
}
.hljs-regexp,
.hljs-number {
color: #104BEB;
}
.hljs-link {
color: #47688a;
}
.hljs-string {
color: #008313;
}
.hljs-doctag {
text-decoration: underline;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-subst,
.hljs-title,
.hljs-params,
.hljs-bullet,
.hljs-formula,
.hljs-tag,
.hljs-type {
/* ignored */
}

Loading
Oops, something went wrong.