-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
executable file
·94 lines (74 loc) · 2.71 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
class Family extends \Phile\Plugin\AbstractPlugin implements \Phile\Gateway\EventObserverInterface {
private $curr_page_path;
public function __construct() {
\Phile\Event::registerEvent('template_engine_registered', $this);
}
public function on($eventKey, $data = null) {
if($eventKey == 'template_engine_registered'){
$this->curr_path = $data['data']['current_page']->getFilePath();
$this->curr_file = basename($this->curr_path);
$this->curr_dir = dirname($this->curr_path);
$data['data'] = $this->buildFamily($data['data']);
return $data;
}
}
private function buildFamily($data){
$ancestor_paths = $this->buildAncestorPaths($data);
$siblings = [];
$ancestors = [];
foreach($data['pages'] as $Page){
$Page->is_dir = FALSE;
$loop_path = $Page->getFilePath();
$loop_file = basename($loop_path);
$loop_dir = dirname($loop_path);
$loop_granddir = dirname($loop_dir);
// We've found a sibling page
if($loop_dir == $this->curr_dir && ($loop_path != $this->curr_path)){
$siblings[] = $Page;
}
// We've found a sibling directory, so add the index file from that directory as a sibling
if($this->settings['sibling_dirs'] && $this->curr_dir == $loop_granddir && $loop_file == 'index'.CONTENT_EXT){
$Page->is_dir = TRUE;
$siblings[] = $Page;
}
// We've found an ancestor file
if(in_array($loop_path,$ancestor_paths)){
$ancestors[$Page->getFilePath()] = $Page;
}
}
if($this->settings['ancestor_sort'] && $this->settings['ancestor_sort'] == 'desc')
ksort($ancestors);
else
krsort($ancestors);
$data['siblings'] = $siblings;
$data['ancestors'] = $ancestors;
return $data;
}
#
# This function builds an array of paths that the ancestors of the current page must have.
# Used by buildFamily to know when an ancestor page has been found when looping through all pages
#
# Returns an array of paths, sorted by first ancestor, to parent
#
private function buildAncestorPaths($data){
$ancestor_paths = [];
$index_filename = 'index'.CONTENT_EXT;
$content_dir = rtrim(CONTENT_DIR,'/');
$content_dir_length = strlen($content_dir);
// If we're viewing an index file...
$active_dir = ($this->curr_file == $index_filename)
// the first ancestor is in the parent directory
? dirname($this->curr_dir)
// otherwise the first ancestor is the index file in the current directory
: $this->curr_dir;
while(strlen($active_dir) >= $content_dir_length){
$ancestor_paths[] = $active_dir.'/'.$index_filename;
$active_dir = dirname($active_dir);
}
#echo '<pre>';
#print_r($ancestor_paths);
# print_r($ancestor_paths);
return $ancestor_paths;
}
}