在服务器端为ColdFusion实现Showdown.js markdown解析器

Moh*_*mad 5 java coldfusion markdown parsing

这是一个"事实发现"问题,看看使用showdown.js解析器创建ColdFusion UDF来解析服务器上的markdown是多么困难.已经有一个使用showdown.js的java实现(参见本文末尾的代码),我想看看如何为ColdFusion实现它.我没有Java经验,我不会特别称自己为"程序员",但我不希望这阻止我尝试.

摘要

我想运行Shadown.js服务器端,以便将markdown转换为HTML.

为什么?

保存两个版本的用户条目,一个是markdown格式,另一个是HTML格式,允许我们向最终用户显示原始markdown版本,以防他们想要编辑他们的条目.

为什么不使用服务器端解析器?

有两个原因:

  1. 截至目前,没有针对此特定目的的ColdFusion markdown解析器
  2. 在客户端使用Showdown.js,然后在服务器端使用不同的解析器将导致显示给客户端的预览和存储在数据库中的版本之间的标记不一致.鉴于markdown定义松散,大多数解析器实现都会有细微差别.

一个非常好的博客文章讨论了这个问题.

为什么不在客户端进行所有解析并发布两个版本?

这并不是一个安全的解决方案.我还认为用户可能能够使用不匹配的HTML发布降价.

有没有现有的实施?

一个名为CFShowdown的实现,但它不是出于这个特定目的.相反,它是用于处理页面上的输出.上述博客的评论部分包含一个名为David的用户编写的纯Java实现:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("js");
try
{
    jsEngine.eval(new InputStreamReader(getClass().getResourceAsStream("showdown.js")));
    showdownConverter = jsEngine.eval("new Showdown.converter()");
}
catch (Exception e)
{
    log.error("could not create showdown converter", e);
}

try
{
    return ((Invocable) jsEngine).invokeMethod(
        showdownConverter, 
        "makeHtml", 
        markdownString
    ) + "";
}
catch (Exception e)
{
    log.error("error while converting markdown to html", e);
    return "[could not convert input]";
}
Run Code Online (Sandbox Code Playgroud)

目的

创建一个java类,允许我们将此实现与ColdFusion UDF或组件内的自定义标记一起使用,这类似于 <cfset html = getMarkdown(string)>

由于我没有Java经验,我想从用户那里获得一些建议和意见,了解在哪里以及如何开始这项任务.我创造了一个

ora*_*ips 4

将文件showdown.js和文件 markdown.txt (下面的示例)放在同一目录中。

摊牌.cfm

<cfscript>
manager = createObject("java", "javax.script.ScriptEngineManager").init();
jsEngine = manager.getEngineByName("js");

showdownJS = fileRead('#getDirectoryFromPath(getCurrentTemplatePath())#/showdown.js');

jsEngine.eval(showdownJS);
showdownConverter = jsEngine.eval("new Showdown.converter()");

markdownString = fileRead("#getDirectoryFromPath(getCurrentTemplatePath())#/markdown.txt");

args = [markdownString];

result = jsEngine.invokeMethod(
    showdownConverter,
    "makeHtml",
    args
) & "";
</cfscript>
Run Code Online (Sandbox Code Playgroud)

降价.txt

Showdown Demo
-------------

You can try out Showdown on this page:

  - Type some [Markdown] text on the left side.
  - See the corresponding HTML on the right.

For a Markdown cheat-sheet, switch the right-hand window from *Preview* to *Syntax Guide*.

Showdown is a JavaScript port of the original Perl version of Markdown.  You can get the full [source code] by clicking on the version number at the bottom of the page.

Also check out [WMD, the Wysiwym Markdown Editor][wmd].  It'll be open source soon; email me at the address below if you'd like to help me test the standalone version.

**Start with a [blank page] or edit this document in the left window.**

  [Markdown]: http://daringfireball.net/projects/markdown/
  [source code]: http://attacklab.net/showdown/showdown-v0.9.zip
  [wmd]: http://wmd-editor.com/
  [blank page]: ?blank=1 "Clear all text"
Run Code Online (Sandbox Code Playgroud)

更新

这是一个采用Adam Presley 在 Java 中完成的工作并在 CFC 中完成的版本。请注意,我采用了他在 showdown.js 末尾添加的一点点魔法,并将其放入一个 CFC 函数中,该函数附加了返回值(即showdownAdapterJS())。

氟氯化碳

<cfcomponent output="false" accessors="true">
    <cffunction name="init" output="false" access="public" returntype="Showdown" hint="Constructor">
        <cfset variables.manager = createObject("java", "javax.script.ScriptEngineManager").init()>
        <cfset variables.engine = manager.getEngineByName("javascript")>
        <cfreturn this/>
    </cffunction>

    <cffunction name="toHTML" output="false" access="public" returntype="any" hint="">
        <cfargument name="markdownText" type="string" required="true"/>
        <cfset var local = structNew()/>
        <cfset var bindings = variables.engine.createBindings()>
        <cfset var result = "">

        <cftry>
            <cfset bindings.put("markdownText", arguments.markdownText)>
            <cfset variables.engine.setBindings(bindings, createObject("java", "javax.script.ScriptContext").ENGINE_SCOPE)>
            <cfset var showdownJS = fileRead('#getDirectoryFromPath(getCurrentTemplatePath())#/showdown.js')>
            <cfset showdownJS &= showdownAdapterJS()>
            <cfset result = engine.eval(showdownJS)>
            <cfcatch type="javax.script.ScriptException">
                <cfset result = "The script had an error: " & cfcatch.Message>
            </cfcatch>
        </cftry>

        <cfreturn result>
    </cffunction>

    <cffunction name="showdownAdapterJS" output="false" access="private" returntype="string" hint="">
        <cfset var local = structNew()/>
<cfsavecontent variable="local.javascript">
<cfoutput>#chr(13)##chr(10)#var __converter = new Showdown.converter();
__converter.makeHtml(markdownText);</cfoutput>
</cfsavecontent>
        <cfreturn local.javascript>
    </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

用法

<cfset showdown = createObject("component", "Showdown").init()>
<cfset markdownString = fileRead("#getDirectoryFromPath(getCurrentTemplatePath())#/markdown.txt")>
<cfoutput>#showdown.toHTML(markdownString)#</cfoutput>
Run Code Online (Sandbox Code Playgroud)