Skip to content

Commit 2124dc0

Browse files
authored
Merge pull request #260 from Simaris/256/fix_Prefixes_for_Posts
WIP: Fix prefixes for posts
2 parents 5ed98af + 414e42e commit 2124dc0

File tree

29 files changed

+335
-4
lines changed

29 files changed

+335
-4
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ matrix:
1111
- rvm: ruby-head
1212
before_install:
1313
- docker run -p 3030:3030 -d stain/jena-fuseki:latest ./fuseki-server --mem /remote
14+
- cd test/theme-gem
15+
- bundle install
16+
- cd ../..
1417
script:
1518
- bundle exec rake test
1619
deploy:

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
source 'https://rubygems.org'
22

3+
4+
gem "theme-gem", :path => 'test/theme-gem'
35
gemspec
6+

lib/jekyll-rdf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
require 'jekyll/helper/rdf_page_helper'
5454
require 'jekyll/helper/rdf_generator_helper'
5555
require 'jekyll/helper/rdf_filter_helper'
56+
require 'jekyll/helper/rdf_hook_helper'
5657
require 'jekyll/hooks/rdf_page_pointer'
5758
require 'jekyll/filters/rdf_sparql_query'
5859
require 'jekyll/filters/rdf_property'
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
##
2+
# MIT License
3+
#
4+
# Copyright (c) 2017 Sebastian Zänker
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
25+
module Jekyll
26+
module JekyllRdf
27+
module Helper
28+
module RdfHookHelper
29+
def backload_prefixes page, payload
30+
prefix_path = page.data["rdf_prefix_path"]
31+
begin
32+
if(!prefix_path.nil? && !page.data["rdf_prefix_set?"] && !page.data["layout"].nil?)
33+
# rdf_prefix_path is set but not defined through the page
34+
base_path = search_prefix_definition page.site.layouts[page.data["layout"]], prefix_path
35+
elsif (prefix_path.nil? && !page.data["layout"].nil?)
36+
# page might be a post (does not contain values from layout frontmatter)
37+
# |->rdf_prefix_path can still be set in a layout
38+
locations = check_prefix_definition page.site.layouts[page.data["layout"]]
39+
base_path = locations[0]
40+
prefix_path = locations[1]
41+
elsif(!prefix_path.nil? && page.data["rdf_prefix_set?"])
42+
# rdf_prefix_path is set directly in the fronmatter of the page
43+
base_path = page.instance_variable_get(:@base_dir)
44+
base_path ||= payload.site["source"]
45+
end
46+
rescue NoMethodError => ne
47+
#just in case the error was caused by something different then a missing template
48+
if(ne.message.eql? "undefined method `data' for nil:NilClass")
49+
return
50+
else
51+
raise
52+
end
53+
end
54+
if(page.data["rdf_prefixes"].nil? && !(prefix_path.nil? || base_path.nil?))
55+
Jekyll::JekyllRdf::Helper::RdfHelper.load_prefixes(
56+
File.join(
57+
base_path,
58+
prefix_path
59+
),
60+
page.data
61+
)
62+
end
63+
end
64+
65+
def search_prefix_definition layout, rdf_prefix_path
66+
if(rdf_prefix_path.eql? layout.data["rdf_prefix_path"])
67+
return layout.instance_variable_get(:@base_dir)
68+
end
69+
return search_prefix_definition layout.site.layouts[layout.data["layout"]], rdf_prefix_path unless layout.data["layout"].nil?
70+
return nil
71+
end
72+
73+
def check_prefix_definition layout
74+
unless(layout.data["rdf_prefix_path"].nil?)
75+
return [layout.instance_variable_get(:@base_dir), layout.data["rdf_prefix_path"]]
76+
end
77+
return check_prefix_definition layout.site.layouts[layout.data["layout"]] unless layout.data["layout"].nil?
78+
return [nil, nil]
79+
end
80+
81+
private
82+
class EqualObject
83+
def eql? object
84+
true
85+
end
86+
end
87+
88+
@@equal_object = EqualObject.new
89+
end
90+
end
91+
end
92+
end

lib/jekyll/helper/rdf_page_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def setData
112112

113113
##
114114
# loads the prefix data passed in the layout yaml-frontmatter into page.data["rdf_prefixes"] and page.data["rdf_prefix_map"]
115+
# only covers a specific case that can not be done by hooks (rdf_prefix_path is defined in a template that serves as page object)
115116
def load_prefixes_yaml
116117
unless self.data["rdf_prefix_path"].nil?
117118
load_prefixes(File.join(@site.layouts[@template].instance_variable_get(:@base_dir), self.data["rdf_prefix_path"].strip), self.data)

lib/jekyll/hooks/rdf_page_pointer.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,37 @@
2222
# SOFTWARE.
2323
#
2424

25+
Jekyll::Hooks.register :documents, :post_init do |page|
26+
if(page.data["rdf_prefix_path"].nil?)
27+
page.data["rdf_prefix_set?"] = false
28+
else
29+
page.data["rdf_prefix_set?"] = true
30+
end
31+
end
32+
33+
Jekyll::Hooks.register :pages, :post_init do |page|
34+
if(page.data["rdf_prefix_path"].nil?)
35+
page.data["rdf_prefix_set?"] = false
36+
else
37+
page.data["rdf_prefix_set?"] = true
38+
end
39+
end
40+
41+
Jekyll::Hooks.register :posts, :post_init do |page|
42+
if(page.data["rdf_prefix_path"].nil?)
43+
page.data["rdf_prefix_set?"] = false
44+
else
45+
page.data["rdf_prefix_set?"] = true
46+
end
47+
end
48+
2549

2650
Jekyll::Hooks.register :pages, :pre_render do |page, payload|
2751
unless(page.data['rdf'].nil?)
2852
payload["content"] = ""
2953
end
30-
if(page.data["rdf_prefixes"].nil? && !page.data["rdf_prefix_path"].nil?)
31-
Jekyll::JekyllRdf::Helper::RdfHelper.load_prefixes(File.join(page.instance_variable_get(:@base), page.data["rdf_prefix_path"]), page.data)
32-
end
54+
include Jekyll::JekyllRdf::Helper::RdfHookHelper
55+
backload_prefixes page, payload if page.data["rdf_prefixes"].nil?
3356
Jekyll::JekyllRdf::Helper::RdfHelper::page = page
3457
end
3558

@@ -38,5 +61,7 @@
3861
end
3962

4063
Jekyll::Hooks.register :posts, :pre_render do |page, payload|
64+
include Jekyll::JekyllRdf::Helper::RdfHookHelper
65+
backload_prefixes page, payload
4166
Jekyll::JekyllRdf::Helper::RdfHelper::page = page
42-
end
67+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
group :jekyll_plugins do
3+
gem 'jekyll-rdf', :path => '../../../'
4+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
baseurl: ""
2+
url: "http://example.org/"
3+
4+
plugins:
5+
- jekyll-rdf
6+
jekyll_rdf:
7+
path: "_data/knowledge-base.ttl"
8+
restriction: "SELECT ?resourceUri WHERE {?resourceUri ?p ?o}"
9+
default_template: "ontology"

test/cases/dontFailIfLayoutNoFound/_data/knowledge-base.ttl

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
layout: ontology
3+
---
4+
5+
This is a Blogpost

0 commit comments

Comments
 (0)