2014-05-16 79 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

谢谢!这一切我需要。 –