Rendering Markdown in Jasper Reports

Started by Chris Usher, February 22, 2021, 18:21:52 PM

Previous topic - Next topic

Chris Usher

Hi,

For those who use Jasper Reports and wish to improve the style of view/element/relationship documentation with Mardown, it is possible with repeated string replacements to convert some of the simpler Markdown in your reports.

Replace the existing documentation code in each .JRXML template:
<textFieldExpression><![CDATA[$F{documentation}]]</textFieldExpression>

with this:
<textFieldExpression><![CDATA[$F{documentation}
                    .replaceAll("(?m)^#### (.*$)", "<style size=\"12\" isBold=\"true\">$1</style>")
                    .replaceAll("(?m)^### (.*$)", "<style size=\"13\" isBold=\"true\">$1</style>")
                    .replaceAll("(?m)^## (.*$)", "<style size=\"14\" isBold=\"true\">$1</style>")
                    .replaceAll("(?m)^# (.*$)", "<style size=\"16\" isBold=\"true\">$1</style>")
                    .replaceAll("(?m)^\\> (.*$)", "<style leftBorder=\"1Point\" backcolor=\"#5CB8B2\">$1</style>")
                    .replaceAll("(?s)\\`\\`\\`(.+?)\\`\\`\\`", "<style forecolor=\"#EC9363\" fontName=\"DejaVu Sans Mono\">$1</style>")
                    .replaceAll("\\*\\*(.+?)\\*\\*", "<style isBold=\"true\">$1</style>")
                    .replaceAll("\\*(.+?)\\*", "<style isItalic=\"true\">$1</style>")
                    .replaceAll("__(.+?)__", "<style isBold=\"true\">$1</style>")
                    .replaceAll("_(.+?)_", "<style isItalic=\"true\">$1</style>")
                    .replaceAll("(?m)^[*+-][ \t]+(\\[(x|X)\\])[ \t]+(.+)$", "\u2612 $3")
                    .replaceAll("(?m)^[*+-][ \t]+(\\[( )?\\])[ \t]+(.+)$", "\u2610 $3")
                    .replaceAll("(?m)^[*+-][ \t]+(.+)$", "\u2022 $1")]]></textFieldExpression>


Substitutions handle four levels of heading, quotes (to some degree), code blocks, unordered lists, unordered lists with checkboxes, bold, italic.
It may be possible to do more with custom Java code, however I have accepted this as my limit so far.

I hope this helps someone.

Chris