这是可以做到的,但需要对文件结构进行三处更改:
由于 DocPad 路径中的点用于分隔不同的渲染器,并且我不确定如何在渲染器内执行我建议的操作,因此它不会抛出Rendering the extension … didn't do anything.
错误,我建议使用_en
/_ru
代替.en
/.ru
,这样您就可以根据文件名执行操作,而无需任何额外的麻烦。
最好使用 Docpad 的初始路径,即将您的pages
and blog
里面的文件夹documents
folder.
可能有一种方法可以在不使用文件的情况下使用这些文件.html.md
部分,但由于它是 DocPad 方式,因此您应该使用它。
也许有一些方法可以在不进行这些更改的情况下完成您想要的操作,但这不会那么简单:)
所以,代码放置在docpadConfig
in docpad.coffee
(这是一个GitHub 上的示例项目 https://github.com/kizu/docpad-test):
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/
and /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/', '/out/ru/')
with .replace('/out/', '/ru/')
等等——这会将渲染的文件提升到上一级out
.