All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.curs.adocwrapper.block.StructuralNode.kt Maven / Gradle / Ivy

The newest version!
package ru.curs.adocwrapper.block

import ru.curs.adocwrapper.block.image.Image
import ru.curs.adocwrapper.block.list.OList
import ru.curs.adocwrapper.block.list.UList
import ru.curs.adocwrapper.block.paragraph.Paragraph
import ru.curs.adocwrapper.block.section.Section
import ru.curs.adocwrapper.block.signature.Signature
import ru.curs.adocwrapper.block.table.Table
import ru.curs.adocwrapper.block.video.Video
import ru.curs.adocwrapper.inline.Inline
import ru.curs.adocwrapper.inline.InlineContent
import ru.curs.adocwrapper.inline.text.Link
import ru.curs.adocwrapper.utils.*
import java.lang.IllegalStateException
import java.util.*

@DslMarker
annotation class AsciidocTagMarker

@AsciidocTagMarker
open class StructuralNode {
    var sectionLevel: Int? = null
    open val type = NodeType.Any
    val blocks = arrayListOf()
    val roles: SortedSet = sortedSetOf()
    private var id: IdString? = null
    var title: TitleString? = null
    var discrete: Discrete? = null
    val attrs: SortedMap = sortedMapOf()

    fun attr(name: String, value: String) {
        if (!name.matches(Regex("[a-zA-Z][a-zA-Z0-9-_]*"))) {
            throw IllegalStateException("Incorrect attribute name")
        }
        attrs[AttrNameString(name)] = HtmlEscaper.escape(value)
    }


    open fun toHabrMd(): String {
        return this.toString()
    }

    open fun toText(): String {
        return this.toString()
    }

    protected var inlineContent: InlineContent = InlineContent()

    protected open fun text(string: String) {
        inlineContent.add(Inline(string))
    }

    fun link(text: String, url: String): Link {
        return Link(text, url)
    }

    fun video(text: String): Video {
        return Video(text)
    }

    fun id(id: String) {
        this.id = IdString(id)
    }

    fun title(title: String) {
        this.title = TitleString(title)
    }

    fun roles(vararg roles: String) {
        roles.forEach { role ->
            this.roles.add(RoleString(role))
        }
    }

    protected open fun ol(init: OList.() -> Unit): OList {
        val list = OList()
        list.apply(init)
        this.blocks.add(list)
        return list
    }

    protected open fun ul(init: UList.() -> Unit): UList {
        val list = UList()
        list.apply(init)
        this.blocks.add(list)
        return list
    }

    protected open fun section(init: Section.() -> Unit): Section {
        val section = Section()
        section.apply(init)
        section.sectionLevel = this.sectionLevel !! + 1
        this.blocks.add(section)
        return section
    }

    protected open fun table(init: Table.() -> Unit): Table {
        val table = Table()
        table.apply(init)
        this.blocks.add(table)
        return table
    }

    protected open fun image(init: Image.() -> Unit): Image {
        val image = Image()
        image.apply(init)
        this.blocks.add(image)
        return image
    }

    protected open fun signature(init: Signature.() -> Unit): Signature {
        val signature = Signature()
        signature.apply(init)
        this.blocks.add(signature)
        return signature
    }

    protected open fun p(init: Paragraph.() -> Unit): Paragraph {
        val para = Paragraph()
        para.apply(init)
        this.blocks.add(para)
        return para
    }

    protected open fun discrete(init: Discrete.() -> Unit): Discrete {
        val discrete = Discrete()
        discrete.apply(init)
        this.blocks.add(discrete)
        return discrete
    }

    fun getIdRoleSyntax(): String {
        if (id == null && roles.isEmpty()) return ""
        val strings = mutableListOf()
        if (id != null) {
            strings += mutableListOf("#$id")
        }
        strings += roles.map { role -> ".$role" }.toMutableList()
        return strings.joinToString("", "[", "]\n")
    }

    private fun getLocalTitleSyntax(): String {
        if (title != null) {
            return ".$title\n"
        }
        return ""
    }

    fun getAttributesSyntax(): String {
        if (attrs.isNotEmpty()) {
            return attrs.map { "${it.key}=\"${it.value}\"" }.joinToString(", ", "[", "]\n")
        }
        return ""
    }

    fun getBlockMetaSyntax(): String {
        return getIdRoleSyntax() + getLocalTitleSyntax() + getAttributesSyntax()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy