1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Content; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File Based Content, code for loading additional content into view through |
7
|
|
|
* data["meta"]. |
8
|
|
|
*/ |
9
|
|
|
trait TFBCUtilities |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Support relative routes. |
13
|
|
|
* |
14
|
|
|
* @param string $route to load. |
15
|
|
|
* @param string $routeIndex to use. |
16
|
|
|
* |
17
|
|
|
* @return string with active route. |
18
|
|
|
*/ |
19
|
|
|
private function getActiveRoute($route, $routeIndex) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
if (substr_compare($route, "./", 0, 2) === 0) { |
22
|
|
|
$route = dirname($routeIndex) . "/" . substr($route, 2); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $route; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Load view data for additional route, merged with meta if any. |
32
|
|
|
* |
33
|
|
|
* @param string $route to load. |
34
|
|
|
* |
35
|
|
|
* @return array with view data details. |
36
|
|
|
*/ |
37
|
|
|
private function getDataForAdditionalRoute($route) |
38
|
|
|
{ |
39
|
|
|
// From configuration |
40
|
|
|
$filter = $this->config["textfilter"]; |
|
|
|
|
41
|
|
|
|
42
|
|
|
// Get filtered content from route |
43
|
|
|
list(, , $filtered) = |
44
|
|
|
$this->mapRoute2Content($route); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// Set data to be content of frontmatter, merged with meta |
47
|
|
|
$meta = $this->getMetaForRoute($route); |
|
|
|
|
48
|
|
|
$data = $filtered->frontmatter; |
49
|
|
|
$data = array_merge_recursive_distinct($meta, $data); |
50
|
|
|
unset($data["__toc__"]); |
51
|
|
|
unset($data["views"]); |
52
|
|
|
|
53
|
|
|
// Do phase 2 processing |
54
|
|
|
$new = $this->di->get("textFilter")->parse($filtered->text, $filter); |
|
|
|
|
55
|
|
|
|
56
|
|
|
// Creates urls based on baseurl |
57
|
|
|
$baseurl = isset($data["baseurl"]) |
58
|
|
|
? isset($data["baseurl"]) |
59
|
|
|
: null; |
60
|
|
|
$this->addBaseurl2AnchorUrls($new, $baseurl); |
|
|
|
|
61
|
|
|
$data["content"] = $new->text; |
62
|
|
|
|
63
|
|
|
return $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Parse text, find and update all a href to use baseurl. |
70
|
|
|
* |
71
|
|
|
* @param object &$filtered with text and excerpt to process. |
72
|
|
|
* @param string $baseurl add as baseurl for all relative urls. |
73
|
|
|
* |
74
|
|
|
* @return void. |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
private function addBaseurl2AnchorUrls(&$filtered, $baseurl) |
77
|
|
|
{ |
78
|
|
|
$textf = $this->di->get("textFilter"); |
79
|
|
|
$url = $this->di->get("url"); |
80
|
|
|
|
81
|
|
|
// Use callback to url->create() instead of string concat |
82
|
|
|
$callback = function ($route) use ($url, $baseurl) { |
83
|
|
|
return $url->create($route, $baseurl); |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
$filtered->text = |
87
|
|
|
$textf->addBaseurlToRelativeLinks($filtered->text, $baseurl, $callback); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get published date. |
94
|
|
|
* |
95
|
|
|
* @param array $frontmatter with details on dates. |
96
|
|
|
* |
97
|
|
|
* @return integer as time for publish time. |
98
|
|
|
*/ |
99
|
|
|
private function getPublishTime($frontmatter) |
100
|
|
|
{ |
101
|
|
|
list(, $date) = $this->di->get("view")->getPublishedDate($frontmatter); |
102
|
|
|
return strtotime($date); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|