2

I have a simple static page app that i have built with nanoc and i want to deploy it as a github page.

Everything goes well except that the assets (like css, javascripts) and all the links in general point to the root of the repo:

like

/css/style.css

instead of being

/docs/css/style.css

Everything works well on localhost but fails when published.

i am using rake publish to push it to gh-pages.

Here is my 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

Rules

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
DArkO
  • 15,880
  • 12
  • 60
  • 88
  • You might want to use ``` route '/static/*/' item.identifier.gsub(/\/static\//, '/').chop ``` so that if you change the name of the folder you remember to update the string change. – iono Jan 16 '13 at 04:10

1 Answers1

6

nanoc generates absolute URLs by default, but you can use the relativize_paths filter to make all URLs relative. For HTML, use filter :relativize_paths, :type => :html. For CSS, use :css instead of :html.

Cheers

Denis

Denis Defreyne
  • 2,213
  • 15
  • 18
  • hey denis. here is how the src is generated. . So i think this is a relative path right? – DArkO Nov 22 '12 at 14:21
  • 1
    A path is absolute if it starts with a slash. "/images/logo_developer.png" is absolute so it is resolved to http://example.com/images/logo_developer.png instead of http://example.com/docs/images/logo_developer.png. If you run the `relativize_paths` filter, that path will be relative, so it will be e.g. "../images/logo_developer.png" depending on where it is referenced from. This way, you can build a site and be able to put it in any subdirectory you want--even move it to a different location without having to recompile. – Denis Defreyne Nov 23 '12 at 09:04
  • So how do i change this to have relative paths. i haven't been able to find any good examples of doing it... – DArkO Nov 25 '12 at 20:48
  • 1
    The answer to that question is in my answer. Use the `relativize_paths` filter to relativize paths in HTML and CSS. – Denis Defreyne Nov 26 '12 at 16:23
  • An example can be found at https://github.com/lifepillar/nanoc4-template/blob/master/Rules – sbkm Jul 20 '21 at 07:52