Relative paths in the source jumplist

Adding a dependency on `Pathname`, which provides the excellent
`relative_path_from` method.  That happens to be exactly what's needed
to fix the pathnames for the source jumplist issue that [Topfunky][]
reported.

Closes GH-26.

[Topfunky]: https://github.com/topfunky
This commit is contained in:
Mike West 2010-11-25 16:37:49 +01:00
parent 9d49139090
commit 12b26fe704
4 changed files with 47 additions and 1 deletions

View File

@ -43,6 +43,8 @@ Changelog
* Fixing language support for Pygments webservice (Issue #11) * Fixing language support for Pygments webservice (Issue #11)
* The source jumplist now generates correctly relative URLs (Issue #26)
[vast]: https://github.com/vast [vast]: https://github.com/vast
0.5 0.5

View File

@ -59,6 +59,15 @@ if sources.empty?
abort_with_note "#{File.basename($0)}: no input <file>s given" abort_with_note "#{File.basename($0)}: no input <file>s given"
end end
# Postprocess `sources` so that paths like `bin/file.rb` are written as `./bin/file.rb`:
sources = sources.map do |filename|
if filename.match( '^[a-zA-Z]' )
File.join( '.', filename )
else
filename
end
end
# What a fucking mess. Most of this is duplicated in rocco.rb too. # What a fucking mess. Most of this is duplicated in rocco.rb too.
libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '') libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '')
begin begin

View File

@ -1,4 +1,5 @@
require 'mustache' require 'mustache'
require 'pathname'
class Rocco::Layout < Mustache class Rocco::Layout < Mustache
self.template_path = File.dirname(__FILE__) self.template_path = File.dirname(__FILE__)
@ -36,11 +37,14 @@ class Rocco::Layout < Mustache
end end
def sources def sources
currentpath = Pathname.new( File.dirname( @doc.file ) )
@doc.sources.sort.map do |source| @doc.sources.sort.map do |source|
htmlpath = Pathname.new( source.sub( Regexp.new( "#{File.extname(source)}$"), ".html" ) )
{ {
:path => source, :path => source,
:basename => File.basename(source), :basename => File.basename(source),
:url => source.sub( Regexp.new( "#{File.extname(source)}$"), ".html" ) :url => htmlpath.relative_path_from( currentpath )
} }
end end
end end

31
test/test_source_list.rb Normal file
View File

@ -0,0 +1,31 @@
require File.dirname(__FILE__) + '/helper'
class RoccoSourceListTests < Test::Unit::TestCase
def test_flat_sourcelist
r = Rocco.new( 'issue26.sh', [ 'issue26a.sh', 'issue26b.sh', 'issue26c.sh' ] ) {
"# Comment 1\n# Comment 1\nprint 'omg!'"
}
html = r.to_html
puts r.to_html
assert(
html.include?( '<a class="source" href="issue26a.html">issue26a.sh</a>' ) &&
html.include?( '<a class="source" href="issue26b.html">issue26b.sh</a>' ) &&
html.include?( '<a class="source" href="issue26c.html">issue26c.sh</a>' ),
"URLs correctly generated for files in a flat directory structure"
)
end
def test_heiarachical_sourcelist
r = Rocco.new( 'a/issue26.sh', [ 'a/issue26a.sh', 'b/issue26b.sh', 'c/issue26c.sh' ] ) {
"# Comment 1\n# Comment 1\nprint 'omg!'"
}
html = r.to_html
puts r.to_html
assert(
html.include?( '<a class="source" href="issue26a.html">issue26a.sh</a>' ) &&
html.include?( '<a class="source" href="../b/issue26b.html">issue26b.sh</a>' ) &&
html.include?( '<a class="source" href="../c/issue26c.html">issue26c.sh</a>' ),
"URLs correctly generated for files in a flat directory structure"
)
end
end