問(wèn)題描述
這是一個(gè)事實(shí)調(diào)查"問(wèn)題,旨在了解使用 showdown.js 解析器. 已經(jīng)有一個(gè)使用 showdown.js 的 java 實(shí)現(xiàn)(參見(jiàn)本文末尾的代碼),我想看看如何為 ColdFusion 實(shí)現(xiàn)它.我沒(méi)有 Java 方面的經(jīng)驗(yàn),我不會(huì)特別稱(chēng)自己為程序員",但我不希望這阻止我嘗試.
This is a "fact finding" question to see how difficult it would be to create a ColdFusion UDF to parse markdown on the server using the showdown.js parser. There is already a java implementation that utilizes showdown.js (see code at the end of this post) and I want to see how to go about implementing it for ColdFusion. I have no experience in Java and I would not particularly call myself "a programmer," but I don't want this to stop me from trying.
總結(jié)
我想在服務(wù)器端運(yùn)行 Shadown.js 以便將 Markdown 轉(zhuǎn)換為 HTML.
I would like to run Shadown.js server-side in order to convert markdown to HTML.
為什么?
保存用戶(hù)條目的兩個(gè)版本,一個(gè)是 Markdown 格式,另一個(gè)是 HTML,允許我們向最終用戶(hù)顯示原始 Markdown 版本,以防他們想要編輯他們的條目.
Saving two versions of a user entry, one in markdown format and another in HTML, allows us to display the raw markdown version to the end user in case they wanted to edit their entry.
為什么不使用服務(wù)器端解析器?
有兩個(gè)原因:
- 到目前為止,還沒(méi)有針對(duì)此特定目的的 ColdFusion 降價(jià)解析器
- 在客戶(hù)端使用 Showdown.js,然后在服務(wù)器端使用不同的解析器,將導(dǎo)致顯示給客戶(hù)端的預(yù)覽與存儲(chǔ)在數(shù)據(jù)庫(kù)中的版本之間的標(biāo)記不一致.鑒于 Markdown 定義松散,大多數(shù)解析器實(shí)現(xiàn)都會(huì)有細(xì)微差別.
有一個(gè)非常好的博客條目 討論這個(gè)問(wèn)題.
為什么不在客戶(hù)端進(jìn)行所有解析并發(fā)布兩個(gè)版本?
這并不是一個(gè)安全的解決方案.我還認(rèn)為用戶(hù)可能會(huì)使用不匹配的 HTML 發(fā)布降價(jià).
This does not strike me as a secure solution. I also think users would potentially be able to post markdown with HTML that does not match.
是否有任何現(xiàn)有的實(shí)現(xiàn)?
有 一個(gè)名為 CFShowdown 的實(shí)現(xiàn),但它不是為了這個(gè)特定目的.相反,它用于處理頁(yè)面上的輸出.上述博客的評(píng)論區(qū)具有一個(gè)名為 David 的用戶(hù)編寫(xiě)的純 Java 實(shí)現(xiàn):
There is one implementation called CFShowdown, but it's not for this specific purpose. Rather, it's for handling output on a page. The comments section of the aforementioned blog features a pure Java implementation written by a user called David:
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]";
}
目標(biāo)
創(chuàng)建一個(gè) java 類(lèi),允許我們將此實(shí)現(xiàn)與 ColdFusion UDF 或組件內(nèi)的自定義標(biāo)簽一起使用,類(lèi)似于 <cfset html = getMarkdown(string)>
Create a java class that would allow us to use this implementation with a ColdFusion UDF or a custom tag inside a component, something along the lines of <cfset html = getMarkdown(string)>
由于我沒(méi)有使用 Java 的經(jīng)驗(yàn),我想從用戶(hù)那里獲得一些建議和意見(jiàn),了解從何處以及如何開(kāi)始執(zhí)行此任務(wù).我創(chuàng)建了一個(gè)
Since I have no experience with Java, I want to get some advice and input from users on where and how to start going about this task. I created a
推薦答案
有文件 攤牌.js 和同一目錄中的文件 markdown.txt(如下示例).
Have files showdown.js and a file markdown.txt (example below) in the same directory.
showdown.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>
ma??rkdown.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"
更新
這是一個(gè)采用 Adam Presley 在 Java 中的工作 并且全部在 CFC 中完成.請(qǐng)注意,我把他在 showdown.js 末尾添加的那一點(diǎn)魔法放入一個(gè) CFC 函數(shù)中,該函數(shù)的返回值被附加(即 showdownAdapterJS()
).
Here's a version that takes Adam Presley's work in Java and does it all in a CFC. Note I took that little bit of magic he added at the end of showdown.js and put it into a CFC function whose return value is appended (i.e. 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>
用法
<cfset showdown = createObject("component", "Showdown").init()>
<cfset markdownString = fileRead("#getDirectoryFromPath(getCurrentTemplatePath())#/markdown.txt")>
<cfoutput>#showdown.toHTML(markdownString)#</cfoutput>
這篇關(guān)于在服務(wù)器端為 ColdFusion 實(shí)現(xiàn) Showdown.js 降價(jià)解析器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!