Nanoc が Github にページをデプロイするときにベースパスを変更する
概要
nanoc を使用して構築した単純な静的ページ アプリがあり、それを github ページとしてデプロイしたいと考えています。
アセット (CSS、JavaScript など) とすべてのリンクが一般的にリポジトリのルートを指していることを除いて、すべてがうまくいきます。
のように
である代わりに
localhost ではすべて正常に動作しますが、公開すると失敗します。
私はrake publicを使用して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
解決策
nanoc はデフォルトで絶対 URL を生成しますが、relativeize_paths フィルターを使用してすべての URL を相対にすることができます。 HTML の場合、フィルター :relativeize_paths、:type => :html を使用します。 CSS の場合は、:html の代わりに :css を使用します。
乾杯
デニス