From 12b26fe7045d00255a454e2f6f79f95542c6cf78 Mon Sep 17 00:00:00 2001 From: Mike West Date: Thu, 25 Nov 2010 16:37:49 +0100 Subject: [PATCH] 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 --- CHANGELOG.markdown | 2 ++ bin/rocco | 9 +++++++++ lib/rocco/layout.rb | 6 +++++- test/test_source_list.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/test_source_list.rb diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 5680bce..dd72c66 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -43,6 +43,8 @@ Changelog * Fixing language support for Pygments webservice (Issue #11) +* The source jumplist now generates correctly relative URLs (Issue #26) + [vast]: https://github.com/vast 0.5 diff --git a/bin/rocco b/bin/rocco index a0d99ca..36431f4 100755 --- a/bin/rocco +++ b/bin/rocco @@ -59,6 +59,15 @@ if sources.empty? abort_with_note "#{File.basename($0)}: no input s given" 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. libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '') begin diff --git a/lib/rocco/layout.rb b/lib/rocco/layout.rb index 9f0f102..a2a59c4 100644 --- a/lib/rocco/layout.rb +++ b/lib/rocco/layout.rb @@ -1,4 +1,5 @@ require 'mustache' +require 'pathname' class Rocco::Layout < Mustache self.template_path = File.dirname(__FILE__) @@ -36,11 +37,14 @@ class Rocco::Layout < Mustache end def sources + currentpath = Pathname.new( File.dirname( @doc.file ) ) @doc.sources.sort.map do |source| + htmlpath = Pathname.new( source.sub( Regexp.new( "#{File.extname(source)}$"), ".html" ) ) + { :path => source, :basename => File.basename(source), - :url => source.sub( Regexp.new( "#{File.extname(source)}$"), ".html" ) + :url => htmlpath.relative_path_from( currentpath ) } end end diff --git a/test/test_source_list.rb b/test/test_source_list.rb new file mode 100644 index 0000000..b08c9fe --- /dev/null +++ b/test/test_source_list.rb @@ -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?( 'issue26a.sh' ) && + html.include?( 'issue26b.sh' ) && + html.include?( 'issue26c.sh' ), + "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?( 'issue26a.sh' ) && + html.include?( 'issue26b.sh' ) && + html.include?( 'issue26c.sh' ), + "URLs correctly generated for files in a flat directory structure" + ) + end + +end