【问题标题】:Nanoc changing the base path when deploying page in githubNanoc 在 github 中部署页面时更改基本路径
【发布时间】:2012-11-10 20:40:50
【问题描述】:

我有一个使用 nanoc 构建的简单静态页面应用程序,我想将其部署为 github 页面。

除了资产(如 css、javascripts)和所有链接通常指向 repo 的根之外,一切都很好:

喜欢

/css/style.css

而不是被

/docs/css/style.css

在本地主机上一切正常,但发布时失败。

我正在使用 rake publish 将其推送到 gh-pages。

这是我的 Rakefile

require 'nanoc3/tasks'

BASE_URL = "http://darko1002001.github.com/docs/"

desc "Compile the site"
task :compile do
  `nanoc compile`
end

desc "Publish to http://documentation.getchute.com"
task :publish => [:clean] do
  FileUtils.rm_r('output') if File.exist?('output')

  sh "nanoc compile"

  ENV['GIT_DIR'] = File.expand_path(`git rev-parse --git-dir`.chomp)
  old_sha = `git rev-parse refs/remotes/origin/gh-pages`.chomp
  Dir.chdir('output') do
    ENV['GIT_INDEX_FILE'] = gif = '/tmp/dev.gh.i'
    ENV['GIT_WORK_TREE'] = Dir.pwd
    File.unlink(gif) if File.file?(gif)
    `git add -A`
    tsha = `git write-tree`.strip
    puts "Created tree   #{tsha}"
    if old_sha.size == 40
      csha = `echo 'boom' | git commit-tree #{tsha} -p #{old_sha}`.strip
    else
      csha = `echo 'boom' | git commit-tree #{tsha}`.strip
    end
    puts "Created commit #{csha}"
    puts `git show #{csha} --stat`
    puts "Updating gh-pages from #{old_sha}"
    `git update-ref refs/heads/gh-pages #{csha}`
    `git push origin gh-pages`
  end
end

规则

compile '/static/*' do
end

compile '/CNAME/' do
end

compile '/feed/' do
  filter :erb
  filter :kramdown, :toc_levels => [2]
end

%w(v3 */).each do |version|
  compile "/changes/#{version}" do
    filter :erb
    filter :kramdown, :toc_levels => [2]
    filter :colorize_syntax,
      :colorizers => {:javascript => :pygmentsrb}
    layout 'changes' if version[0] == '*'
    layout 'default'
  end
end

compile '*' do
  filter :erb
  filter :kramdown, :toc_levels => [2]
  filter :colorize_syntax,
    :colorizers => {:javascript => :pygmentsrb}
  layout 'default'
end

route '/static/*' do
  item.identifier[7..-2]
end

route '/CNAME/' do
  '/CNAME'
end

route '/feed' do
  '/changes.atom'
end

route '*' do
  item.identifier + 'index.html'
end

layout '*', :erb

【问题讨论】:

  • 你可能想使用 ``` route '/static/*/' item.identifier.gsub(/\/st​​atic\//, '/').chop ``` 这样如果你更改文件夹的名称你记得更新字符串更改。

标签: ruby-on-rails ruby github nanoc github-pages


【解决方案1】:

nanoc 默认生成绝对 URL,但您可以使用 relativize_paths 过滤器使所有 URL 成为相对 URL。对于 HTML,请使用 filter :relativize_paths, :type => :html。对于 CSS,请使用 :css 而不是 :html

干杯

丹尼斯

【讨论】:

  • 嘿,丹尼斯。这是 src 的生成方式。 。所以我认为这是一条相对路径,对吧?
  • 以斜杠开头的路径是绝对路径。 “/images/logo_developer.png”是绝对的,所以它被解析为example.com/images/logo_developer.png而不是example.com/docs/images/logo_developer.png。如果您运行relativize_paths 过滤器,则该路径将是相对的,因此它将是例如“../images/logo_developer.png”取决于它的引用位置。通过这种方式,您可以构建一个站点并能够将其放在您想要的任何子目录中——甚至可以将其移动到不同的位置,而无需重新编译。
  • 那么我该如何更改它以具有相对路径。我还没有找到任何好的例子......
  • 这个问题的答案在我的回答中。使用 relativize_paths 过滤器将 HTML 和 CSS 中的路径相对化。
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2014-01-05
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
相关资源
最近更新 更多