Class PreLanguageRewriter

java.lang.Object
org.codehaus.groovy.tools.groovydoc.PreLanguageRewriter

public final class PreLanguageRewriter extends Object
Post-process pass that retrofits syntax-highlighting classes onto bare <pre> blocks in generated HTML. Used by both the Ant Groovydoc task and the groovydoc CLI so their outputs stay identical.

Bare <pre>...</pre> blocks — openings with no attributes at all — are rewritten to <pre class="language-xxx"><code>...</code></pre> so Prism's highlighter picks them up (Prism only walks <code> descendants of language-classed elements). A <pre> whose body already contains a <code> element (e.g. the canonical form emitted by {@snippet}) only receives the class on its opening tag, to avoid nested <code><code>. Any <pre> with an existing attribute — class, id, or other — is left untouched.

Since:
6.0.0
  • Method Details

    • rewriteTags

      public static String rewriteTags(String html, String preLanguage)
      Returns html with <pre> blocks rewritten so Prism can highlight them. Three cases:
      • Bare <pre>body</pre> with no attributes becomes <pre class="language-xxx"><code>body</code></pre>.
      • <pre class="...language-...">body</pre> that lacks an inner <code> gets its body wrapped in <code> (Prism walks <code> descendants, so a language-classed <pre> without a <code> child is skipped by the highlighter).
      • All other cases — <pre> with an existing <code> descendant, or with a class that doesn't mention language-* — are left untouched.
      No-op when preLanguage is null or empty.
    • rewriteDirectory

      public static void rewriteDirectory(File dir, String preLanguage)
      Walks dir recursively and applies rewriteTags(java.lang.String, java.lang.String) to every .html file in place. A no-op when preLanguage is null or empty.