1+ <?php
2+
3+ require_once dirname (__FILE__ ) . "/Debug.php " ;
4+ require_once dirname (__FILE__ ) . "/OutlineText.php " ;
5+ require_once dirname (__FILE__ ) . "/ContentsDatabaseManager.php " ;
6+ require_once dirname (__FILE__ ) . "/ContentsViewerUtils.php " ;
7+ require_once dirname (__FILE__ ) . "/Utils.php " ;
8+
9+ /**
10+ * Content記法拡張
11+ */
12+ class ContentTextParser {
13+ /**
14+ * [
15+ * 'path' => true, 'path' => true
16+ * ]
17+ */
18+ public static $ contentLinks = [];
19+ public static $ currentRootDirectory ='' ;
20+ public static $ isInitialized = false ;
21+
22+ public static function Init () {
23+ if (static ::$ isInitialized ) return ;
24+
25+ OutlineText \Parser::$ inlineElementPatternTable [] = [
26+ "/\[(.*?)\]/ " , null , ['ContentTextParser ' , 'ParseContentLink ' ]
27+ ];
28+ OutlineText \Parser::Init ();
29+ static ::$ isInitialized = true ;
30+ }
31+
32+ public static function Parse ($ text , $ contentPath , &$ context ) {
33+ if (!static ::$ isInitialized ) static ::Init ();
34+ static ::$ currentRootDirectory = substr (GetTopDirectory ($ contentPath ), 1 );
35+ return OutlineText \Parser::Parse ($ text , $ context );
36+ }
37+
38+ public static function CreateContext ($ contentPath ) {
39+ $ context = new OutlineText \Context ();
40+ $ context ->pathMacros = static ::CreatePathMacros ($ contentPath );
41+ return $ context ;
42+ }
43+
44+ public static function ParseContentLink ($ matches , $ context ) {
45+ $ contentPath = URI2Path (static ::$ currentRootDirectory . '/ ' . $ matches [1 ][0 ]);
46+ $ content = new Content ();
47+ if (!$ content ->SetContent ($ contentPath )) {
48+ // if not exists, return the text that matched the full pattern.
49+ return $ matches [0 ][0 ];
50+ }
51+ if (!array_key_exists ($ content ->path , static ::$ contentLinks )) {
52+ static ::$ contentLinks [$ content ->path ] = true ;
53+ }
54+ $ title = '' ;
55+ $ parent = $ content ->Parent ();
56+ if ($ parent !== false ) {
57+ $ title .= $ parent ->title . '- ' ;
58+ }
59+ $ title .= $ content ->title ;
60+ $ href = CreateContentHREF ($ content ->path );
61+ return '<a href=" ' . $ href .'"> ' . $ title . '</a> ' ;
62+ }
63+
64+ public static function CreatePathMacros ($ contentPath ) {
65+ return [
66+ ['CURRENT_DIR ' , 'ROOT_URI ' ],
67+ [ROOT_URI . Path2URI (dirname ($ contentPath )), ROOT_URI ]
68+ ];
69+ }
70+ }
0 commit comments