2014-05-16 81 views
4

我想調整我的multilungual DocPad博客,以便以* .ru.md結尾的頁面進入/ ru /目錄,並以* .en.md結尾的頁面進入/ en /目錄。爲不同的語言生成不同的docpad集合

比方說,這是最初的structire

src/ 
    pages/ 
    page1.ru.md 
    page1.en.md 
    page2.ru.md 

這就是我想要的東西:

./ 
    en/ 
    page1.html 
    ru/ 
    page1.html 
    page2.html 

./,因爲我將與gh-pages舉辦。

和帖子一樣。我想將它們存儲在

src/ 
    posts/ 
    post-about-docpad.ru.md 
    post-about-docpad.en.md 

並獲得

./ 
    en/ 
    posts/ 
     post-about-docpad.html 
    ru/ 
    posts/ 
     post-about-docpad.html 

我應該如何配置docpad?

回答

2

的第一步是重新命名您的文檔使用破折號像這樣的語言:page1-en.htmlpage1-ru.html,而不是page1.en.htmlpage1.ru.tml - 否則爲@kizu正確地指出,這將導致問題DocPad從擴展渲染擴展。

一旦做到這一點,你可以添加以下到您的docpad配置文件:

collections: 

    # Fetch documents in different languages 
    translate: (database) -> 
     languageRegex = /^(.+?)-(en|ru)$/ 
     #docpadOutPath = @getConfig().outPath 
     @getCollection('documents').findAllLive({basename: languageRegex}).on 'add', (document) -> 
      # Prepare 
      a = document.attributes 

      parts = a.basename.match(languageRegex) 
      basename = parts[1] 
      language = parts[2] 

      relativeOutPath = "#{language}/#{a.relativeOutDirPath}/#{basename}.#{a.outExtension}" 
      #outPath = "#{docpadOutPath}/#{relativeOutPath}" 

      urls = ["/#{relativeOutPath}"] 

      # Apply 
      document 
       .setMetaDefaults({ 
        #outPath 
        url: urls[0] 
       }) 
       .addUrl(urls) 

這將在你所希望的方式工作的網址。

然後用插件安裝cleanurls靜態環境中運行DocPad,寫文件到所需的位置。

docpad install cleanurls 
docpad generate --env static 
4

可以這樣做,但三個地變化的文件結構:

  1. 正如DocPad的路徑點在那裏劃定不同的渲染器,我不知道怎麼做我想建議在渲染器裏面,所以不會拋出Rendering the extension … didn't do anything.錯誤,我建議使用_en/_ru而不是.en/.ru,這樣你就可以做基於文件名的東西而沒有任何額外的麻煩。

  2. 最好使用Docpad的初始路徑,即將您的pagesblog文件夾放入documents文件夾中。

  3. 可能有一種方法可以在不使用.html.md部件的情況下使用這些文件,但由於它是DocPad方式,因此應該使用它。

也許有辦法做你想做什麼這個沒有這些變化,但它是沒有價值的:)

所以,代碼在docpadConfig放在​​(這裏是一個example project at GitHub):

docpadConfig = { 
    collections: 
     # Declare `ru` and `en` collections 
     ruDocuments: -> 
      @getCollection("documents").findAllLive({ 
       basename: /_ru$/ 
      }) 
     enDocuments: -> 
      @getCollection("documents").findAllLive({ 
       basename: /_en$/ 
      }) 

    events: 
     renderBefore:() -> 
      # Rewrite `pages/` to the root and `posts/` to the `blog/`. 
      this.docpad.getCollection('documents').forEach (page) -> 
       newOutPath = page.get('outPath') 
        .replace('/out/pages/', '/out/') 
        .replace('/out/posts/', '/out/blog/') 
       newUrl = page.get('url') 
        .replace('pages/', '') 
        .replace('posts/', 'blog/') 
       page.set('outPath', newOutPath) 
       page.setUrl(newUrl) 

      # Rewrite `_ru` to the `/ru/` 
      this.docpad.getCollection('ruDocuments').forEach (page) -> 
       newOutPath = page.get('outPath') 
        .replace('/out/', '/out/ru/') 
        .replace('_ru.', '.') 
       newUrl = '/ru' + page.get('url') 
       page.set('outPath', newOutPath) 
       page.setUrl(newUrl) 

      # Rewrite `_en` to the `/en/` 
      this.docpad.getCollection('enDocuments').forEach (page) -> 
       newOutPath = page.get('outPath') 
        .replace('/out/', '/out/en/') 
        .replace('_en.', '.') 
       page.set('outPath', newOutPath) 
       newUrl = '/en' + page.get('url').replace('_en.', '.') 
       page.setUrl(newUrl) 
} 
module.exports = docpadConfig 

首先我們聲明兩個集合:一個用於所有ru文檔,另一個用於所有en文檔。

然後我們在第一改寫pages/到根,然後_en/_ru文件改寫爲/en//ru/

請注意,這些更改只是源文件的存儲方式,結果輸出將與您的問題相同。

所以,與其

src/ 
    pages/ 
    page1.ru.md 
    page1.en.md 
    page2.ru.md 
    posts/ 
    post-about-docpad.ru.md 
    post-about-docpad.en.md 

你應該寫這樣:

src/ 
    documents/ 
    pages/ 
     page1_ru.html.md 
     page1_en.html.md 
     page2_ru.html.md 
    posts/ 
     post-about-docpad_ru.html.md 
     post-about-docpad_en.html.md 

但是,你會得到你就像你需要所需的輸出。唯一的是我不明白關於gh-pages的部分:你是否要將結果和源碼存儲在同一分支中?我建議使用master作爲源代碼,gh-pages作爲結果。

但是,如果你需要只有一個分支,好了,好了,你可以很容易重寫所有你想要的東西用.replace('/out/', '/ru/')等替代.replace('/out/', '/out/ru/') - 這會把渲染的文件上面達到一個水平out

+0

謝謝!這一切我需要。 –