Class TocMacro
java.lang.Object
org.apache.maven.doxia.macro.AbstractMacro
com.puppycrawl.tools.checkstyle.site.TocMacro
- All Implemented Interfaces:
org.apache.maven.doxia.logging.LogEnabled,org.apache.maven.doxia.macro.Macro
@Component(role=org.apache.maven.doxia.macro.Macro.class,
hint="sitetoc")
public class TocMacro
extends org.apache.maven.doxia.macro.AbstractMacro
A macro that generates an "In this article" table of contents for an xdoc
page. Every canonical section key in
SECTION_NAMES is attempted
for every page; the anchor id used in the link is read directly from a
matching <subsection> tag's real id attribute, so links
can never drift from what the page actually renders, and any section not
present in a given file's source is silently skipped -- pages do not need
to declare which sections they have. Examples and Use Cases subsections
additionally get nested entries, titled either by the first non-default
property=value found in the example's referenced source file, or
by a shortened form of the example's descriptive paragraph when no
distinguishing property can be found.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordBundles the per-page context needed while writing TOC entries, so helper methods don't need long parameter lists. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringFallback title when no descriptive paragraph is available.private static final StringBase directory that examplepathparams are resolved against.private static final intRegex group index of thepathparam's value inITEM_WITH_PATH_PATTERN.private static final intRegex group index of theidattribute when it appears first (id, name order).private static final intRegex group index of theidattribute when it appears second (name, id order).private static final intRegex group index of thenameattribute when it appears second (id, name order).private static final PatternMatches an Example/UseCase paragraph followed immediately by its example macro.private static final PatternStrips the common "To configure the check to produce a violation on/when" lead-in.private static final StringClosing anchor/list-item tag pair used to end a TOC entry.Section keys that get nested sub-entries.private static final PatternMatches a property assignment inside an example's embedded config comment.private static final StringA single double quote character, used to open HTML attribute values.private static final StringClosing quote and angle bracket used when terminating an HTML attribute.private static final StringSection key/name: Description.private static final StringSection key/name: Examples.Maps every canonical section key, in the fixed order TOC entries should appear, to the subsection'snameattribute value.private static final StringSection key/name: Properties.private static final StringSection key: UseCases.private static final PatternMatches a subsection tag and captures its name/id attributes, in either order.private static final PatternStrips inline HTML tags left in scraped paragraph text except<code>tags.private static final StringProperty name to always skip when deriving a property=value title.private static final PatternCollapses any run of whitespace (including newlines) into a single space.Fields inherited from interface org.apache.maven.doxia.macro.Macro
EOL, ROLE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionderivePropertyTitle(String examplePath, TocMacro.TocContext context) Reads an example's referenced source file and finds the first property whose value differs from its documented default, skippingtokens.voidexecute(org.apache.maven.doxia.sink.Sink sink, org.apache.maven.doxia.macro.MacroRequest request) extractPropertyTitle(Matcher propMatcher, Map<String, PropertyDetails> propertyDetails) Extracts a property title from a matcher group if the property differs from its default value.private static StringextractSectionBody(String sourceContent, String anchorId) Extracts the text of one subsection from the full source, bounded by that subsection's opening tag and the next subsection's opening tag.findSubsectionAnchor(String sourceContent, String sectionName) Finds the actualidattribute value of the<subsection>tag whosenameattribute matches the given section name.private static Map<String, PropertyDetails> loadPropertyDetails(String modulePath) Loads documented property defaults for the current module, if amodulePathparam was supplied.private static StringnormalizeWhitespace(String text) Collapses internal newlines/indentation from raw xdoc paragraph text into single spaces, so tooltips and labels render on one line.private static PathresolveRepoRoot(String modulePath) Derives the repository root from the module's source path so example resource paths (relative toEXAMPLES_BASE_DIR) can be resolved to an absolute file location.private static StringtoSentenceTitle(String rawParagraph) Converts a raw "ExampleN-config"/"UseCaseN-config" paragraph into a short sentence-based title by removing inline markup, normalizing whitespace, stripping the common lead-in clause, and trimming the trailing colon.private static voidwriteNestedItems(org.apache.maven.doxia.sink.Sink sink, String sectionBody, TocMacro.TocContext context) Writes nested<li>entries for each Example/UseCase found within a subsection's body text, titled by property=value when possible, falling back to a shortened descriptive sentence.private static voidwriteSectionEntry(org.apache.maven.doxia.sink.Sink sink, String section, TocMacro.TocContext context) Writes a single top-level<li>, with nested Example/UseCase entries when the section is Examples or Use Cases.Methods inherited from class org.apache.maven.doxia.macro.AbstractMacro
enableLogging, getAttributesFromMap, getLog, required
-
Field Details
-
SECTION_DESCRIPTION
Section key/name: Description.- See Also:
-
SECTION_PROPERTIES
Section key/name: Properties.- See Also:
-
SECTION_EXAMPLES
Section key/name: Examples.- See Also:
-
SECTION_USE_CASES_KEY
Section key: UseCases.- See Also:
-
QUOTE_CLOSE_TAG
Closing quote and angle bracket used when terminating an HTML attribute.- See Also:
-
DEFAULT_TITLE
Fallback title when no descriptive paragraph is available.- See Also:
-
QUOTE
A single double quote character, used to open HTML attribute values.- See Also:
-
EXAMPLES_BASE_DIR
Base directory that examplepathparams are resolved against.- See Also:
-
ITEM_WITH_PATH_PATTERN
Matches an Example/UseCase paragraph followed immediately by its example macro. -
PROPERTY_PATTERN
Matches a property assignment inside an example's embedded config comment. -
LEAD_IN_PATTERN
Strips the common "To configure the check to produce a violation on/when" lead-in. -
TAG_PATTERN
Strips inline HTML tags left in scraped paragraph text except<code>tags. -
WHITESPACE_PATTERN
Collapses any run of whitespace (including newlines) into a single space. -
SUBSECTION_TAG_PATTERN
Matches a subsection tag and captures its name/id attributes, in either order. -
TOKENS_PROPERTY
Property name to always skip when deriving a property=value title.- See Also:
-
GROUP_ID_NAME_FIRST
Regex group index of theidattribute when it appears second (name, id order).- See Also:
-
GROUP_ID_ID_FIRST
Regex group index of theidattribute when it appears first (id, name order).- See Also:
-
GROUP_NAME_ID_FIRST
Regex group index of thenameattribute when it appears second (id, name order).- See Also:
-
GROUP_EXAMPLE_PATH
Regex group index of thepathparam's value inITEM_WITH_PATH_PATTERN. Coincidentally the same numeric value asGROUP_ID_ID_FIRST, but kept as a distinct constant since the two patterns and group meanings are otherwise unrelated.- See Also:
-
LI_CLOSE
Closing anchor/list-item tag pair used to end a TOC entry.- See Also:
-
SECTION_NAMES
Maps every canonical section key, in the fixed order TOC entries should appear, to the subsection'snameattribute value. Every key is attempted for every page; any section not present in a given file's source is silently skipped bywriteSectionEntry(org.apache.maven.doxia.sink.Sink, java.lang.String, com.puppycrawl.tools.checkstyle.site.TocMacro.TocContext). -
NESTED_SECTIONS
Section keys that get nested sub-entries.
-
-
Constructor Details
-
TocMacro
public TocMacro()Creates a newTocMacroinstance.
-
-
Method Details
-
execute
public void execute(org.apache.maven.doxia.sink.Sink sink, org.apache.maven.doxia.macro.MacroRequest request) -
writeSectionEntry
private static void writeSectionEntry(org.apache.maven.doxia.sink.Sink sink, String section, TocMacro.TocContext context) Writes a single top-level<li>, with nested Example/UseCase entries when the section is Examples or Use Cases. The anchor id is read directly from the matching<subsection>tag's actualidattribute in the source, rather than assumed from a naming convention, so the link can never drift from what the page really renders. If no matching<subsection>exists in the source, nothing is written for this section -- pages are not required to have every canonical section.- Parameters:
sink- sink to write to.section- the canonical section key.context- shared per-page context for resolving anchors and titles.
-
findSubsectionAnchor
Finds the actualidattribute value of the<subsection>tag whosenameattribute matches the given section name.- Parameters:
sourceContent- the full template source text.sectionName- the subsection'snameattribute value to match.- Returns:
- the subsection's real id, or empty if no matching tag is found.
-
extractSectionBody
Extracts the text of one subsection from the full source, bounded by that subsection's opening tag and the next subsection's opening tag.- Parameters:
sourceContent- the full template source text.anchorId- the subsection's id attribute value.- Returns:
- the subsection's raw inner text, or an empty string if not found.
-
writeNestedItems
private static void writeNestedItems(org.apache.maven.doxia.sink.Sink sink, String sectionBody, TocMacro.TocContext context) Writes nested<li>entries for each Example/UseCase found within a subsection's body text, titled by property=value when possible, falling back to a shortened descriptive sentence. Titles are shown in full and allowed to wrap across lines, rather than being truncated with an ellipsis, so the whole label is always readable.- Parameters:
sink- sink to write to.sectionBody- the raw text of the subsection.context- shared per-page context for resolving titles.
-
loadPropertyDetails
Loads documented property defaults for the current module, if amodulePathparam was supplied. Returns an empty map otherwise, or if lookup fails for any reason -- property-based titling is a nice-to-have, not something that should break page generation.- Parameters:
modulePath- path to the module's Java source, ornull.- Returns:
- a map of property name to its documented details.
-
resolveRepoRoot
Derives the repository root from the module's source path so example resource paths (relative toEXAMPLES_BASE_DIR) can be resolved to an absolute file location.- Parameters:
modulePath- path to the module's Java source, ornull.- Returns:
- the repository root, or the current working directory if
modulePathis absent or doesn't contain the expected marker.
-
derivePropertyTitle
private static Optional<String> derivePropertyTitle(String examplePath, TocMacro.TocContext context) Reads an example's referenced source file and finds the first property whose value differs from its documented default, skippingtokens.- Parameters:
examplePath- thepathparam value from the example macro.context- shared per-page context providing property defaults and repo root.- Returns:
- an "name=value" title, or empty if the file can't be read or every property in it matches its documented default.
-
extractPropertyTitle
private static Optional<String> extractPropertyTitle(Matcher propMatcher, Map<String, PropertyDetails> propertyDetails) Extracts a property title from a matcher group if the property differs from its default value.- Parameters:
propMatcher- the property matcher positioned at a match.propertyDetails- the documented property defaults.- Returns:
- an "name=value" title, or empty if the property matches default.
-
toSentenceTitle
Converts a raw "ExampleN-config"/"UseCaseN-config" paragraph into a short sentence-based title by removing inline markup, normalizing whitespace, stripping the common lead-in clause, and trimming the trailing colon. Used as a fallback when no distinguishing property can be found.- Parameters:
rawParagraph- the paragraph's raw inner text.- Returns:
- a short, human-readable title.
-
normalizeWhitespace
Collapses internal newlines/indentation from raw xdoc paragraph text into single spaces, so tooltips and labels render on one line.- Parameters:
text- the raw text, possibly containing multi-line whitespace.- Returns:
- the text with all whitespace runs collapsed to a single space.
-