/
tagger.php
188 lines (173 loc) · 4.75 KB
/
tagger.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php if (!defined('PmWiki')) exit();
/*
* Copyright 2008 Kathryn Andersen
*
* This program is free software; you can redistribute it and/or modify it
* under the Gnu Public Licence or the Artistic Licence.
*/
/** \file tagger.php
* \brief multiple category groups (tags)
*
* See Also: http://www.pmwiki.org/wiki/Cookbook/Tagger
*
* This script enables one to auto-link with (a subset of) page-text-variable
* markup, to different "category" groups.
* The markup is
* name:value
*
* This also makes corresponding Page Variables <name>Linked, which
* contains the "linked" value.
*
* This also allows multiple values, separated by commas.
*
* This requires 2.2-beta64 or later.
*
* To activate this script, copy it into the cookbook/ directory, then add
* the following line to your local/config.php:
*
* include_once("$FarmD/cookbook/multicat.php");
*
* File modified by Petko Yotov to work with PHP 5.5
*/
$RecipeInfo['Tagger']['Version'] = '20140515';
SDVA($TaggerGroups, array());
SDV($TaggerTagSeparators, array(',','/','&'));
/*======================================================================
* Tagger
*/
// set up multiple category groups
function TaggerSetup () {
global $TaggerGroups;
global $FmtPV;
if ($TaggerGroups)
{
foreach ($TaggerGroups as $tagname => $catgroup)
{
$FmtPV['$' . $tagname . 'Linked'] = "TaggerLinksVar(\$pn, '$tagname', '$catgroup', 'LinkedTitle')";
$FmtPV['$' . $tagname . 'LinkedName'] = "TaggerLinksVar(\$pn, '$tagname', '$catgroup', 'LinkedName')";
$FmtPV['$' . $tagname . 'Name'] = "TaggerLinksVar(\$pn, '$tagname', '$catgroup', 'Name')";
}
// markup to insert the links
$tags = array_keys($TaggerGroups);
$tagpat = implode('|', $tags);
if(function_exists('Markup_e')) { # added by Petko Yotov
Markup_e("tagger",
'<directives','/^(\(:)?(' . $tagpat . '):(.*?)(:\))?$/',
"TaggerLinks(\$pagename, \$m[1], \$m[2], \$m[3], \$m[4])");
// deal with the "hidden" PTVs
Markup_e('textvar:', '<split', '/\\(:(\\w[-\\w]*):((?!\\)).*?):\\)/s', "TaggerHiddenVars(\$pagename, \$m[1], \$m[2])");
}
else {
Markup("tagger",
'<directives','/^(\(:)?(' . $tagpat . '):(.*?)(:\))?$/e',
"TaggerLinks(\$pagename, '$1', '$2', '$3', '$4')");
// deal with the "hidden" PTVs
Markup('textvar:', '<split', '/\\(:(\\w[-\\w]*):((?!\\)).*?):\\)/se', "TaggerHiddenVars(\$pagename, '$1', '$2')");
}
// hidden Tagger PTVs get hidden after link-processing
Markup('textvar:ol', ">links", '/^\(:\w[-\w]*:.*?:\)$/', '');
}
}
TaggerSetup();
// deal with hidden PTVs
function TaggerHiddenVars($pagename, $varname, $varval)
{
global $TaggerGroups;
if (strpos($varval, "\n") !== false)
{
// Multi-line PTVs shall be fully hidden
return '';
}
else
{
// if this is a Tagger tag, keep it, otherwise hide it
if ($TaggerGroups["$varname"])
{
return "(:$varname:$varval:)";
}
else
{
return '';
}
}
}
// process link shortcut markup
function TaggerLinks($pagename, $prefix, $tagname, $inval, $postfix) {
global $TaggerGroups;
$catgroup = $TaggerGroups["$tagname"];
//print "prefix=$prefix, tagname=$tagname, inval=$inval, postfix=$postfix\n";
$out = "$prefix$tagname:";
$out .= TaggerProcessTags($pagename, $catgroup, $inval);
$out .= "$postfix";
return $out;
}
// Page-Variable
function TaggerLinksVar($pagename, $tagname, $catgroup, $label) {
$inval = PageTextVar($pagename, $tagname);
$outval = TaggerProcessTags($pagename, $catgroup, $inval, $label);
rtrim($outval);
return $outval;
}
function TaggerProcessTags($pagename, $catgroup, $inval, $label='LinkedName') {
global $TaggerTagSeparators;
$out = '';
// don't process if there are already links there
if (strpos($inval, '[[') !== false)
{
$out = $inval;
}
else
{
$array_sep = '';
foreach ($TaggerTagSeparators as $tsep)
{
if (strpos($inval, $tsep) !== false)
{
$array_sep = $tsep;
break;
}
}
$oo = array();
if ($label == 'Name') // one page name, not parts
{
$pn = str_replace($array_sep, ' ', $inval);
$cpage = MakePageName($pagename, "$catgroup.$pn");
$out = PageVar($cpage, '$Name');
}
else
{
$parts = ($array_sep
? explode($array_sep, $inval)
: array($inval));
foreach($parts as $part)
{
$part = trim($part);
if ($part)
{
$cpage = MakePageName($pagename, "$catgroup.$part");
if ($label == 'LinkedTitle')
{
$oo[] = "[[$cpage|+]]";
}
else
{
$oo[] = "[[$cpage|$part]]";
}
}
}
}
if ($array_sep == ',')
{
$out .= implode("$array_sep ", $oo);
}
else if ($array_sep == '/')
{
$out .= implode($array_sep, $oo);
}
else
{
$out .= implode(" $array_sep ", $oo);
}
}
return $out;
}