-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContentTextParser.php
More file actions
109 lines (93 loc) · 3.34 KB
/
ContentTextParser.php
File metadata and controls
109 lines (93 loc) · 3.34 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
require_once dirname(__FILE__) . "/Logger.php";
require_once dirname(__FILE__) . "/OutlineText.php";
require_once dirname(__FILE__) . "/ContentDatabaseControls.php";
require_once dirname(__FILE__) . "/ContentsViewerUtils.php";
require_once dirname(__FILE__) . "/PathUtils.php";
require_once dirname(__FILE__) . "/Utils.php";
use ContentDatabaseControls as DBControls;
use ContentsViewerUtils as CVUtils;
// FIXME: Should be instantiable class
/**
* Content記法拡張
*/
class ContentTextParser
{
/**
* [
* 'path' => true, 'path' => true
* ]
*/
public static $contentLinks = [];
/**
* ./Master/Contents
*/
public static $currentRootDirectory = '';
public static $currentDirectory = '';
public static $isInitialized = false;
public static $database = null;
public static function Init($database = null)
{
if (static::$isInitialized) return;
self::$database = $database ?? new ContentDatabase;
OutlineText\Parser::$inlineElementPatternTable[] = [
"/\[(.*?)\]/", null, ['ContentTextParser', 'ParseContentLink']
];
OutlineText\Parser::Init();
static::$isInitialized = true;
}
public static function Parse($text, $contentPath, &$context)
{
if (!static::$isInitialized) static::Init();
static::$currentRootDirectory = DBControls\GetContentsFolder($contentPath);
static::$currentDirectory = dirname($contentPath);
return OutlineText\Parser::Parse($text, $context);
}
public static function CreateContext($contentPath)
{
$context = new OutlineText\Context();
$context->pathMacros = static::CreatePathMacros($contentPath);
return $context;
}
public static function ParseContentLink($matches, $context)
{
$path = $matches[1][0];
$contentPath = '';
if (strpos($path, '/') === 0) {
// To navigate from the root directory
$contentPath = static::$currentRootDirectory . $path;
} else {
// To navigate from the current directory
$contentPath = static::$currentDirectory . '/' . $path;
}
// logger()->debug($contentPath);
$content = self::$database->get($contentPath);
if (!$content) {
// if not exists, return the text that matched the full pattern.
return $matches[0][0];
}
if (strpos($content->path, static::$currentRootDirectory . '/') !== 0) {
// not start with current root directory.
// logger()->debug('Permission denied.');
return $matches[0][0];
}
if (!array_key_exists($content->path, static::$contentLinks)) {
static::$contentLinks[$content->path] = true;
}
$title = '';
$parent = $content->parent();
if ($parent !== false) {
$title .= NotBlankText([$parent->title, basename($parent->path)]) . '/';
}
$title .= NotBlankText([$content->title, basename($content->path)]);
$href = CVUtils\CreateContentHREF($content->path);
return '<a href="' . $href . '">[' . $title . ']</a>';
}
public static function CreatePathMacros($contentPath)
{
return [
['CURRENT_DIR', 'ROOT_URI'],
[ROOT_URI . Path2URI(dirname($contentPath)), ROOT_URI]
];
}
}