2015-07-11 71 views
2

我正在用Middleman構建一個網站,並使用Redcarpet作爲我的降價引擎,主要用於支持GFM如何擴展Middleman的Redcarpet降價渲染器?

我想進入或先於markdown渲染過程來添加對各種語法選項的支持。舉一個例子,我想這樣的:

[file:/path/to/file] 

被渲染爲:

<p class="file"> 
    <code>/path/to/file</code> 
</p> 

在任何情況下,我不會呈現任何會干擾影響其餘降價模板,所以我會懷疑我可以先於渲染過程。另外,如果使用其他渲染器可以更簡單,那麼除了我更願意擁有GFM支持之外,我並不以任何方式與Redcarpet綁定。

+0

如果你決定pandoc去代替,看看[pandoc腳本](HTTP:/ /pandoc.org/scripting.html),祝你好運! – mb21

回答

0

首先,您需要根據config.rb文件中的redcarpet創建一個新的渲染器。是這樣的:

set renderer: myRenderer 

接下來,你需要創建「myRenderer」作爲一個新的類(你可以在你的config.rb頂部做到這一點,但你也可以把它放在一個外部文件)

require "middleman-core/renderers/redcarpet" 
class myRenderer < Middleman::Renderers::MiddlemanRedcarpetHTML 

def preprocess(document) 
    # insert ruby code to use a regex to find your tag in the document 
    # insert ruby code to generate your HTML and replace your tag with 
    # HTML that you want 
    return (document) 
end 

如果你想這樣做的最後一件事,用後處理(文件),而不是預處理(文件)