-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpax_grep.php
254 lines (193 loc) · 10.8 KB
/
pax_grep.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
// This is a PLUGIN TEMPLATE for Textpattern CMS.
// Copy this file to a new name like abc_myplugin.php. Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt
// Plugin name is optional. If unset, it will be extracted from the current
// file name. Plugin names should start with a three letter prefix which is
// unique and reserved for each plugin author ("abc" is just an example).
// Uncomment and edit this line to override:
$plugin['name'] = 'pax_grep';
// Allow raw HTML help, as opposed to Textile.
// 0 = Plugin help is in Textile format, no raw HTML allowed (default).
// 1 = Plugin help is in raw HTML. Not recommended.
# $plugin['allow_html_help'] = 1;
$plugin['version'] = '0.2.3';
$plugin['author'] = 'John Stephens';
$plugin['author_uri'] = 'https://cyclexo.com/';
$plugin['description'] = 'Replace all occurrences of a regular expression with replacements.';
// Plugin load order:
// The default value of 5 would fit most plugins, while for instance comment
// spam evaluators or URL redirectors would probably want to run earlier
// (1...4) to prepare the environment for everything else that follows.
// Values 6...9 should be considered for plugins which would work late.
// This order is user-overrideable.
$plugin['order'] = '5';
// Plugin 'type' defines where the plugin is loaded
// 0 = public : only on the public side of the website (default)
// 1 = public+admin : on both the public and admin side
// 2 = library : only when include_plugin() or require_plugin() is called
// 3 = admin : only on the admin side (no AJAX)
// 4 = admin+ajax : only on the admin side (AJAX supported)
// 5 = public+admin+ajax : on both the public and admin side (AJAX supported)
$plugin['type'] = '0';
// Plugin "flags" signal the presence of optional capabilities to the core plugin loader.
// Use an appropriately OR-ed combination of these flags.
// The four high-order bits 0xf000 are available for this plugin's private use
if (!defined('PLUGIN_HAS_PREFS')) define('PLUGIN_HAS_PREFS', 0x0001); // This plugin wants to receive "plugin_prefs.{$plugin['name']}" events
if (!defined('PLUGIN_LIFECYCLE_NOTIFY')) define('PLUGIN_LIFECYCLE_NOTIFY', 0x0002); // This plugin wants to receive "plugin_lifecycle.{$plugin['name']}" events
$plugin['flags'] = '0';
// Plugin 'textpack' is optional. It provides i18n strings to be used in conjunction with gTxt().
// Syntax:
// ## arbitrary comment
// #@event
// #@language ISO-LANGUAGE-CODE
// abc_string_name => Localized String
/** Uncomment me, if you need a textpack
$plugin['textpack'] = <<< EOT
#@admin
#@language en-gb
abc_sample_string => Sample String
abc_one_more => One more
#@language de-de
abc_sample_string => Beispieltext
abc_one_more => Noch einer
EOT;
**/
// End of textpack
if (!defined('txpinterface'))
@include_once('zem_tpl.php');
# --- BEGIN PLUGIN CODE ---
/**
* This is pax_grep: A plugin for Textpattern
* version 0.2.3
* by John Stephens, adapted from rah_replace by Jukka Svahn
* https://cyclexo.com/
*/
// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('pax_grep');
}
// pax_grep :: String -> String
function pax_grep ($atts, $thing = null) {
global $pretext;
extract (lAtts (array (
'from' => '',
'to' => '',
'delimiter' => ','
), $atts));
$from = explode ($delimiter, $from);
$to = explode ($delimiter, $to);
$count = count ($to);
if ($count == 1) $to = implode ('', $to);
if ($count == 0) $to = '';
return preg_replace ($from, $to, parse($thing));
}
# --- END PLUGIN CODE ---
if (0) {
?>
<!--
# --- BEGIN PLUGIN HELP ---
<h1>pax_grep</h1>
<dl>
<dt>Summary</dt>
<dd>Replace all occurrences of a regular expression with replacements.</dd>
<dt>Version</dt>
<dd>0.2.3 (updated 14 Jun 2018)</dd>
</dl>
<h2>Table of contents</h2>
<ol>
<li><a href="#help-section01" rel="nofollow">Overview</a></li>
<li><a href="#help-section02" rel="nofollow">Installing and uninstalling</a></li>
<li><a href="#help-section03" rel="nofollow">Usage and syntax</a></li>
<li><a href="#help-section04" rel="nofollow">Attributes</a>
<ol>
<li><a href="#help-section05" rel="nofollow">from</a></li>
<li><a href="#help-section06" rel="nofollow">to</a></li>
<li><a href="#help-section07" rel="nofollow">delimiter</a></li>
</ol></li>
<li><a href="#help-section08" rel="nofollow">Examples</a></li>
<li><a href="#help-section09" rel="nofollow">License</a></li>
<li><a href="#help-section10" rel="nofollow">Author contact</a></li>
<li><a href="#help-section11" rel="nofollow">Changelog</a></li>
</ol>
<h2 id="help-section01">Overview</h2>
<p>This plugin allows you to <em>find occurrences of a <a href="http://en.wikipedia.org/wiki/Regular_expression" rel="nofollow">regular expression</a> pattern</em> and <em>replace them with something else</em>. It’s almost identical to it’s awesome parent plugin <a href="http://rahforum.biz/plugins/rah_replace" rel="nofollow">rah_replace by Jukka Svahn</a>, but <strong>pax_grep</strong> uses <span class="caps">PHP</span>’s <a href="http://fi.php.net/preg_replace" rel="nofollow">preg_replace()</a> function, packaged in a compact and easy-to-use Textpattern tag.</p>
<h2 id="help-section02">Installing and uninstalling</h2>
<p>In Textpattern, navigate to the “Plugins” tab under “Admin”, and paste the code into the “Install plugin” pane. Install and enable the plugin.</p>
<p>To uninstall, simply delete the plugin from the “Plugins” tab.</p>
<h2 id="help-section03">Usage and syntax</h2>
<p><strong>pax_grep</strong> takes a regular expression and searches content for matching patterns. Then it replaces matches with the string(s) you supply. This works just like <a href="http://rahforum.biz/plugins/rah_replace" rel="nofollow">rah_replace</a>, except it supports regular expression searches. If you don’t need to search and replace based on a regular expression, <strong>you should use rah_replace instead</strong>.</p>
<p><strong>pax_grep</strong> s a container tag with three attributes.</p>
<pre><code><txp:pax_grep ↩
from="/search pattern/" ↩
to="replacement text" ↩
delimiter="|">
Content
</txp:pax_grep>
</code></pre>
<h3 id="help-section04">Attributes</h3>
<h4 id="help-section05"><code>from</code> — <em>Required</em></h4>
<p>Give this attribute the value of a pattern or patterns you wish to search for. A pattern should be <strong>delimited with single quotes</strong> or some other character not used in the pattern. Separate multiple patterns by commas (use the <code>delimiter</code> attribute to specify and alternate separator).</p>
<dl>
<dt>Default</dt>
<dd><code>from=""</code></dd>
<dt>Example</dt>
<dd><code>from="'^foo','bar$'"</code></dd>
</dl>
<h4 id="help-section06"><code>to</code> — <em>Required</em></h4>
<p>This attribute holds the replacement value(s) for each pattern in the <code>from</code> attribute. No delimiters are needed, but <strong>multiple values must be separated by commas (use the <code>delimiter</code> attribute to specify and alternate separator)</strong>. Each <code>from</code> value will be replaced with the corresponding <code>to</code> value.</p>
<dl>
<dt>Default</dt>
<dd><code>to=""</code></dd>
<dt>Example</dt>
<dd><code>to="fox,bat"</code></dd>
</dl>
<h4 id="help-section07"><code>delimiter</code> — <em>Optional</em></h4>
<p>So you don’t like using commas to separate your search patterns or replacement values? Sometimes, you need to use a comma in the search pattern, and you need a different separator to break search patterns and replacement values. Use this attribute to specify an alternate separator.</p>
<dl>
<dt>Default</dt>
<dd><code>delimiter=","</code></dd>
<dt>Example</dt>
<dd><code>delimiter=" | "</code></dd>
</dl>
<h2 id="help-section08">Examples</h2>
<h3>Example 1: Get words out of Textpattern’s <code>request_uri</code>.</h3>
<p>This example outputs the current request <acronym title="Uniform Resource Identifier"><span class="caps">URI</span></acronym>, and uses a regular expression to drop the leading slash and transform many delimiter characters into spaces. You might use code like this on a 404 error page to populate a search field with information from a mistyped <span class="caps">URL</span>.</p>
<pre><code><txp:pax_grep
from="'^\/','\/','%20','\-','\+','\?=','\_'" ↩
to=", , , , , , ">
<txp:page_url type="request_uri"/>
</txp:pax_grep>
</code></pre>
<h3>Example 2: Strip Textile-generated <code>p</code> elements from Textpattern’s excerpt output</h3>
<p>Sometimes you might want to show the excerpt of a Textpattern article without mucking around with a bunch of paragraph tags. Here’s how.</p>
<pre><code><txp:pax_grep from="/<\/?p>/,/\t/" to="">
<txp:excerpt/>
</txp:pax_grep>
</code></pre>
<h2 id="help-section09">Licence</h2>
<p>This plugin is licenced under <a href="https://textpattern.com/license" rel="nofollow"><span class="caps">GPL</span>, Version 2</a>.</p>
<h2 id="help-section10">Author contact</h2>
<p>John Stephens is known as “johnstephens” on the Textpattern support forum and on Twitter. You can reach me at <a href="https://cyclexo.com/" rel="nofollow">Cycle Design Co.</a> or find <a href="https://twitter.com/johnstephens" rel="nofollow">@johnstephens</a> on Twitter <a href="https://twitter.com/johnstephens" rel="nofollow">here</a>.</p>
<h2 id="#help-section11">Changelog</h2>
<dl>
<dt>Version 0.2.3</dt>
<dd>2018-06-14: Clean up code style and fix plugin help navigation.</dd>
<dt>Version 0.2.2</dt>
<dd>2016-12-05: Support the tag registry in Textpattern 4.6.0+.</dd>
<dt>Version 0.2.1</dt>
<dd>2012-04-26: Expand and revise plugin help.</dd>
<dt>Version 0.2</dt>
<dd>2010-10-01: Add an optional <code>delimiter</code> attribute, so you can use commas in your search pattern. <a href="http://forum.textpattern.com/viewtopic.php?pid=235323#p235323" rel="nofollow">Thanks, Jan</a>.</dd>
<dt>Version 0.1.1</dt>
<dd>2009-01-25: Change plugin name from <strong>opus_grep</strong> to <strong>pax_grep</strong> in compliance with de facto standard of three-letter prefix for plugins.</dd>
<dt>Version 0.1</dt>
<dd>2009-01-23: Branch from <a href="http://rahforum.biz/plugins/rah_replace" rel="nofollow">rah_replace</a> by Jukka Svahn— use <code>preg_replace()</code> instead of <code>str_replace()</code> to allow regex search patterns.</dd>
</dl>
# --- END PLUGIN HELP ---
-->
<?php
}
?>