redundent / kotlin-xml-builder

A lightweight type safe builder to build xml documents in Kotlin
Apache License 2.0
155 stars 18 forks source link

Option to disable indentation #61

Closed vincentburesi-es closed 8 months ago

vincentburesi-es commented 8 months ago

It would be nice to have an option to easily disable all indentation and unnecessary spaces to get a "minified" output.

Currently setting pretty to false in the toString function arguments doesn't seem to do that, which is what I would expect it to do.

redundent commented 8 months ago

Hi @vincentburesi-es Can you provide a sample of your code where you are seeing indentation when pretty is set to false? When pretty is false, all indentation and new lines should be removed.

Zordid commented 8 months ago

I can ensure that with pretty print off, there is no indentation whatsoever... So I think there is something wrong in the setup used.

vincentburesi-es commented 8 months ago

As I was testing to send you the code snippet, it seemed to work perfectly fine, both with toString(false) and toString(PrintOptions(pretty = false)).

I'm not sure what happened, but since the project I work on is rather large and complex, maybe I didn't recompile the right part when testing at the time? Anyway, thank you, I'm sorry for wasting your time and have marked the ticket as closed.

If you're curious here is the code sample anyway:

fun getSitemap(): String {
        val urlList = listOf(SiteMapUrl(loc = "test", lastMod = "test", listOf(AlternateLink(rel = "test", hrefLang = "test", href = "test"), AlternateLink(rel = "test", hrefLang = "test", href = "test"))),
            SiteMapUrl(loc = "test", lastMod = "test", listOf(AlternateLink(rel = "test", hrefLang = "test", href = "test"))))

        val sitemap = xml("urlset") {
            val xhtmlNamespace = Namespace("xhtml", "http://www.w3.org/1999/xhtml")

            xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
            namespace(xhtmlNamespace)

            for (url in urlList) {
                "url" {
                    "loc" {
                        -url.loc
                    }
                    if (url.lastMod != null)
                        "lastmod" {
                            -url.lastMod
                        }
                    for (link in url.alternateLinks) {
                        "link"(xhtmlNamespace) {
                            attribute("rel", link.rel)
                            attribute("hreflang", link.hrefLang)
                            attribute("href", link.href)
                        }
                    }
                }
            }
        }

        return sitemap.toString(false)
    }

NB: SiteMapUrl and AlternateLink are plain old data class with nothing special

redundent commented 8 months ago

No worries. Glad it's working as expected.