Class TextUtils.DocumentBuilder
- Enclosing class:
TextUtils
All settings have sensible defaults — the minimal usage is: TextUtils.documentBuilder().markdown(text).build().
Two-Layer Theme System
The generated document supports two independent CSS layers:
- Document theme (
theme(Theme)) — controls the overall page appearance: fonts, headings, tables, blockquotes, backgrounds. Choose fromBASIC,MODERN, orGITHUB(each with a dark variant), orNONEfor unstyled HTML. Default:TextUtils.Theme.GITHUB. - Syntax highlight theme (
highlightTheme(SyntaxHighlighter.HighlightTheme)) — controls code block colors whensyntaxHighlight(boolean)is enabled. 77 themes are available (16 curated + 61 extended). SeeSyntaxHighlighter.HighlightThemefor the full catalog.
When no highlight theme is specified, one is auto-selected to match the document theme's brightness: light documents get GITHUB, dark documents get GITHUB_DARK.
Example
String html = TextUtils.documentBuilder()
.markdown(text)
.title("API Reference")
.theme(TextUtils.Theme.GITHUB_DARK)
.syntaxHighlight(true)
.highlightTheme(SyntaxHighlighter.HighlightTheme.NORD)
.autoDetectLanguage(true)
.build();
- Author:
- Christopher Mindus
- See Also:
Method Summary
Modifier and TypeMethodDescriptionautoDetectLanguage(boolean b) Enables or disables automatic language detection for code blocks that do not have an explicitlanguage-xxxclass (i.e.CSS class on the<article>wrapper.build()Builds the complete HTML document.Sets the character encoding declared in<meta charset>.URL string for an external stylesheet (used wheninlineCss(boolean)isfalse).Additional CSS appended after the theme CSS (default: none).disposeHighlighterAfterBuild(boolean b) Iftrue, the highlighting engine is disposed afterbuild()completes, releasing the engine instance.escapeNonAscii(boolean b) Controls whether non-ASCII characters in the rendered HTML body are replaced with numeric HTML entities (&#xNNNN;).Sets the CSS theme for syntax-highlighted code blocks.inlineCss(boolean b) Iftrue(default), embeds the theme CSS in a<style>block.Sets thelangattribute on<html>(default:"en").Sets the Markdown source text to convert.syntaxHighlight(boolean b) Enables or disables server-side syntax highlighting of fenced code blocks.Provides an existingSyntaxHighlighterinstance to reuse.Sets the CSS theme (default:TextUtils.Theme.GITHUB).Sets the HTML<title>element (default: none).
Method Details
markdown
Sets the Markdown source text to convert.- Parameters:
md- The Markdown source text.- Returns:
- This builder, for method chaining.
title
Sets the HTML<title>element (default: none).- Parameters:
t- Applies the title.- Returns:
- This builder, for method chaining.
lang
Sets thelangattribute on<html>(default:"en").- Parameters:
l- Applies the language (code).- Returns:
- This builder, for method chaining.
charset
Sets the character encoding declared in<meta charset>.- Parameters:
cs- Applies the @code cs} as CharSet for Strings (default"UTF-8").- Returns:
- This builder, for method chaining.
theme
Sets the CSS theme (default:TextUtils.Theme.GITHUB). UseTextUtils.Theme.NONEfor no CSS.- Parameters:
t- Applies the Theme.- Returns:
- This builder, for method chaining.
inlineCss
Iftrue(default), embeds the theme CSS in a<style>block. Iffalse, usecssUrl(String)to provide an external stylesheet link.- Parameters:
b-trueto enable theme CSS (default:true).- Returns:
- This builder, for method chaining.
customCss
Additional CSS appended after the theme CSS (default: none).- Parameters:
css- The additional css ornullfor none.- Returns:
- This builder, for method chaining.
cssUrl
URL string for an external stylesheet (used wheninlineCss(boolean)isfalse).- Parameters:
url- Applies the URL for an external CSS file.- Returns:
- This builder, for method chaining.
cssFrom
- Parameters:
uri- Applies the URI for an external CSS file.- Returns:
- This builder, for method chaining.
cssFrom
- Parameters:
url- Applies the URL for an external CSS file.- Returns:
- This builder, for method chaining.
bodyClass
CSS class on the<article>wrapper.- Parameters:
cls- Applies the body class, default:"markdown-body".
syntaxHighlight
Enables or disables server-side syntax highlighting of fenced code blocks.When enabled, the
SyntaxHighlighterengine is loaded on demand and all<pre><code class="language-xxx">blocks in the output are processed. A suitable highlight CSS theme is included automatically (can be overridden withhighlightTheme(SyntaxHighlighter.HighlightTheme)).- Parameters:
b-trueto enable syntax highlighting (default:false).- Returns:
- This builder, for method chaining.
syntaxHighlighter
Provides an existingSyntaxHighlighterinstance to reuse.Implies
syntaxHighlight(true). The provided instance is not disposed afterbuild()— the caller retains ownership. This is efficient when rendering multiple documents, as the engine is initialized only once.- Parameters:
hl- An existing highlighter instance, ornullto create on demand.- Returns:
- This builder, for method chaining.
highlightTheme
Sets the CSS theme for syntax-highlighted code blocks.Implies
syntaxHighlight(true). If not set, a theme matching the document'sTextUtils.Themebrightness is selected automatically.- Parameters:
t- The highlight theme, ornullfor automatic selection.- Returns:
- This builder, for method chaining.
autoDetectLanguage
Enables or disables automatic language detection for code blocks that do not have an explicitlanguage-xxxclass (i.e. bare<pre><code>blocks produced by unfenced or unlabeled code in Markdown).Implies
syntaxHighlight(true). When enabled, the highlighter'shighlightAutomethod is used. Only results whose relevance score meets or exceedsSyntaxHighlighter.AUTO_DETECT_MIN_RELEVANCEare applied; below this threshold the code block is left as plain escaped text.When disabled (
false), bare code blocks are left entirely untouched — only blocks with an explicit language class are highlighted.- Parameters:
b-trueto enable auto-detection (default:true).- Returns:
- This builder, for method chaining.
- See Also:
disposeHighlighterAfterBuild
Iftrue, the highlighting engine is disposed afterbuild()completes, releasing the engine instance.This is the default behavior for one-off document generation. If you are rendering many documents, set this to
falseto avoid repeated engine initialization.Note: since the highlighting library is on the class path, disposal releases only this engine instance — the library's classes remain loaded. It no longer closes a class loader.
- Parameters:
b-trueto dispose after build (default:true).- Returns:
- This builder, for method chaining.
escapeNonAscii
Controls whether non-ASCII characters in the rendered HTML body are replaced with numeric HTML entities (&#xNNNN;).When enabled (the default), the output document is pure ASCII and renders correctly regardless of the HTTP
Content-Typecharset sent by the web server. This prevents mojibake when the server does not includecharset=UTF-8in the response headers.Disable this if the serving infrastructure guarantees UTF-8 encoding and you prefer smaller output without entity escaping.
- Parameters:
b-trueto escape non-ASCII (default:true).- Returns:
- This builder, for method chaining.
- See Also:
build
Builds the complete HTML document.- Returns:
- A full HTML5 document string.
- Throws:
IllegalStateException- if the Markdown engine could not be initialized.