Skip to content
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

Customizable table rendering #145

Open
mikaelstaldal opened this issue Dec 22, 2023 · 0 comments
Open

Customizable table rendering #145

mikaelstaldal opened this issue Dec 22, 2023 · 0 comments

Comments

@mikaelstaldal
Copy link

mikaelstaldal commented Dec 22, 2023

Related to #140, I experimented with JSON rendering of tables. I managed to do it like this (proof of concept, not production ready):

fun jsonTable(init: TableBuilder.() -> Unit): String {
    val tableBuilder = TableBuilderInstance().apply(init)
    return buildString {
        append("[\n")
        for (row in tableBuilder.bodySection.rows.withIndex()) {
            append('{')
            for (cell in row.value.cells.withIndex()) {
                append('"')
                append((tableBuilder.headerSection.rows[0].cells[cell.index].content as CellContent.TextContent).text)
                append('"')
                append(':')
                append('"')
                append((cell.value.content as CellContent.TextContent).text) // TODO escape value
                append('"')
                if (cell.index < row.value.cells.size - 1) append(',')
            }
            append('}')
            if (row.index < tableBuilder.bodySection.rows.size - 1) append(',')
            append('\n')
        }
        append("]\n")
    }
}

The problem is that it requires access to some code which is internal in Mordant, in particular TableBuilderInstance.

Maybe JSON rendering should not be part of Mordant, this code only works for simple tables and it might be hard to make it fully generic. But it would be nice to expose those classes, or some kind of similar API, to make it possible to implement this yourself like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant