如何使用DocPad部署不在服务器根目录的站点?

时间:2013-01-09 20:05:32

标签: docpad

我正在使用DocPad制作演示网站,我希望将其部署在例如: http://www.example.com/demo/ 但是当我使用docpad generate --env static时,链接相对于服务器的根而不是demo目录,因此链接被破坏。我能做什么 ?是否存在要在某处声明的元数据?

编辑: 在这种情况下,我使用Twitter Bootsrap骨架,因此例如默认布局中主导航中的链接如下所示:

<div class="nav-collapse collapse">
  <ul class="nav">
    <% for document in @getCollection('pages').toJSON(): %>
      <li class="<%= 'active'  if @document.url is document.url %>">
        <a href="<%= document.url %>"><%= document.title %></a>
      </li>
    <% end %>
  </ul>
</div>

3 个答案:

答案 0 :(得分:1)

我写了一个小插件来完成这项工作,可能有更好的解决方案,但它运行良好:

# Export Plugin
module.exports = (BasePlugin) ->
  # Define Plugin
  class absolutePathPlugin extends BasePlugin
    # Plugin Name
    name: 'absolutepath'
    config:
      url: "/"

    renderAfter: (opts,next) ->
      docpad = @docpad
      if 'static' in docpad.getEnvironments()
        docpad.log 'debug', 'Writing absolute urls'
        href = 'href="' + @config.url
        src = 'src="' + @config.url
        database = docpad.getCollection('html')
        database.forEach (document) ->
          content = document.get('contentRendered')
          if /href="\//.test(content)
            content = content.replace(/href="\//g, href)
          if /src="\//.test(content)
            content = content.replace(/src="\//g, src)
          document.set('contentRendered',content)
        next()?
      else
        next()?

      # Chain
      @

在我的docpad.coffee文件中,我只需要配置url,如果你使用cleanUrls插件,你必须调整getRedirectTemplate函数来考虑绝对url:

plugins:
  absolutepath:
    url: "http://www.example.com/demo/"
  cleanurls:
    getRedirectTemplate: (document) ->
      absolutepath = docpadConfig.plugins.absolutepath.url.slice(0, - 1) 
      """
      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
          <title>#{document.get('title') or 'Redirect'}</title>
          <meta http-equiv="REFRESH" content="0;url=#{absolutepath + document.get('url')}">
        </head>
        <body>
          This page has moved. You will be automatically redirected to its new location. If you aren't forwarded to the new page, <a href="#{absolutepath + document.get('url')}">click here</a>.
        </body>
      </html>
      """

答案 1 :(得分:1)

另一种选择是将所有绝对URL转换为相对URL,以便网站可以从任何路径前缀(//path//some/other/path/开始工作而无需重建。用自定义域思考gh页面。

Here's a grunt task在静态docpad生成后对HTML和CSS进行转换。它目前建立在由@ Dharma的docpad插件启发的正则表达式替换上。

答案 2 :(得分:0)

这是DocPad可以加强的一个领域。

到目前为止,人们最常使用的方法是在任何地方使用<%- @site.url+@document.url %>,但由于它的挑剔性,当然这并不理想。这里有一个要点更详细地展示了这一点:https://gist.github.com/3939146

DocPad可能需要做的是为absoluteUrl自动生成的文件/文档添加site.url属性。然后我们可以改为<%- @document.absoluteUrl %>。这里为此创建了一个问题:https://github.com/bevry/docpad/issues/402