Jfinal enjoy 模板引擎练习小工具

配置好JDK8环境,下载体验

[jfinalEnjoy.zip]

显示行号、关键词高亮

可渲染所有内容、渲染鼠标所在行、渲染选中内容

关键代码:

class EnjoyView : View("Jfinal enjoy 模板引擎练习小工具") {
    val engine = Engine.use().setEncoding("utf-8").addSharedObject("dt", Date()).addSharedMethod(StrKit())
    var kv = Kv.create()
    val strOut = stringProperty()
    override val root = borderpane {
        top = label("提示:在左边文本框中点击鼠标右键,选择相应的菜单进行操作,右边的文本框会显示结果") {
            paddingAll = 10.0
            style{
                textFill= Color.RED
            }
        }
        center = hbox(5) {
            codearea {
                val ca = this
                contextmenu {
                    item("渲染所有").action {
                        strOut.value = renderStr(ca.text, kv)
                    }
                    item("渲染当前行").action {
                        strOut.value = renderStr(ca.getText(ca.currentParagraph), kv)
                    }
                    item("渲染选中").action {
                        strOut.value = renderStr(ca.selectedText, kv)
                    }
                    item("清除").action {
                        ca.replaceText("")
                    }
                }
                paragraphGraphicFactory = LineNumberFactory.get(ca)
                isAutoScrollOnDragDesired = true
                prefWidth = 500.0
                multiPlainChanges().successionEnds(Duration.ofMillis(500))
                        .subscribe { ca.setStyleSpans(0, computeHighlighting(ca.text)) }
                replaceText(0, 0, sampleCode)

            }
            textarea(strOut) {
                prefHeight = 600.0
                isWrapText = true
                isEditable = false
                promptText="这是结果文本框"
            }
        }
    }

    private fun renderStr(inStr: String, kv: Kv): String {
        var result = ""
        try {
            result = engine.getTemplateByString(inStr).renderToString(kv)
        } catch (e: Exception) {
            information("模板中存在错误:$e")
        }

        return result
    }

    init {
        Pattern.compile(
                "(?<KEYWORD>" + KEYWORD_PATTERN + ")"
                        + "|(?<PAREN>" + PAREN_PATTERN + ")"
                        + "|(?<BRACE>" + BRACE_PATTERN + ")"
                        + "|(?<BRACKET>" + BRACKET_PATTERN + ")"
                        + "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
                        + "|(?<STRING>" + STRING_PATTERN + ")"
                        + "|(?<COMMENT>" + COMMENT_PATTERN + ")"
        )
    }
}

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券