|
| 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 |
0 commit comments