You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2014/12/17 18:23:52 UTC
[18/50] [abbrv] incubator-brooklyn git commit: use Pathname ruby api
in plugin so relative paths are resolved better
use Pathname ruby api in plugin so relative paths are resolved better
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2b8a4910
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2b8a4910
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2b8a4910
Branch: refs/heads/master
Commit: 2b8a4910fad5a7a4edff1f2665c6c38cde4a0a5c
Parents: ba6612e
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Mon Dec 15 21:12:58 2014 +0000
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Wed Dec 17 02:02:55 2014 +0000
----------------------------------------------------------------------
docs/_plugins/read.rb | 47 ++++++++++++++++++++++++----------------------
1 file changed, 25 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b8a4910/docs/_plugins/read.rb
----------------------------------------------------------------------
diff --git a/docs/_plugins/read.rb b/docs/_plugins/read.rb
index aedbceb..74a7613 100644
--- a/docs/_plugins/read.rb
+++ b/docs/_plugins/read.rb
@@ -28,6 +28,8 @@
# the argument can be a variable or a filename literal (not quoted)
# TODO: figure out how to accept a quoted string as an argument
+require 'pathname'
+
module JekyllRead
class ReadTag < Liquid::Tag
def initialize(tag_name, text, tokens)
@@ -35,17 +37,17 @@ module JekyllRead
@text = text
end
def render(context)
- jekyllSite = context.registers[:site]
- dir = jekyllSite.source+'/'+File.dirname(context['page']['url'])
filename = @text.strip
filename = context[filename] || filename
- if !filename.match(/^\/.*/)
- filename = dir + '/' + filename
- else
- filename = jekyllSite.source+'/'+filename
- end
- filename = filename.gsub(/\/\/+/,'/')
- file = File.open(filename, "rb")
+
+ # Pathname API ignores first arg below if second is absolute
+ file = Pathname.new(File.dirname(context['page']['path'])) + filename
+ file = file.cleanpath
+ # is there a better way to trim a leading / ?
+ file = file.relative_path_from(Pathname.new("/")) unless file.relative?
+ raise "No such file #{file} in read call (from #{context['page']['path']})" unless file.exist?
+
+ file = File.open(file, "rb")
return file.read
end
end
@@ -56,20 +58,21 @@ module JekyllRead
@text = text
end
def render(context)
- jekyllSite = context.registers[:site]
- filename = @text.strip
- filename = context[filename] || filename
-# support vars (above) and relative paths in filename (below - need the right path if there is a subsequent link)
- dir = filename
- if !filename.match(/^\/.*/)
- dir = File.dirname(context['page']['url']) + '/' + filename
- end
- dir = dir.gsub(/\/\/+/,'/')
- filename = dir.sub(/^.*\//, '')
- dir = dir.gsub(/\/[^\/]*$/, '/')
- targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, dir, filename)
+ filename = @text.strip
+ filename = context[filename] || filename
+
+ # Pathname API ignores first arg below if second is absolute
+ file = Pathname.new(File.dirname(context['page']['path'])) + filename
+ file = file.cleanpath
+ # is there a better way to trim a leading / ?
+ file = file.relative_path_from(Pathname.new("/")) unless file.relative?
+ raise "No such file #{file} in readj call (from #{context['page']['path']})" unless file.exist?
+
+ # with readj we support vars and paths relative to a file being readj'd
+ jekyllSite = context.registers[:site]
+ targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(file), File.basename(file))
targetPage.render(jekyllSite.layouts, jekyllSite.site_payload)
- targetPage.output
+ return targetPage.output
end
end
end