97
97
public function apply($text) {
98
$prefix = $this->getObjectNamePrefix();
99
$prefix = preg_quote($prefix, '@');
100
$id = $this->getObjectIDPattern();
102
98
$text = preg_replace_callback(
103
'@\B{'.$prefix.'('.$id.')((?:[^}\\\\]|\\\\.)*)}\B@u',
99
$this->getObjectEmbedPattern(),
104
100
array($this, 'markupObjectEmbed'),
103
$text = preg_replace_callback(
104
$this->getObjectReferencePattern(),
105
array($this, 'markupObjectReference'),
111
private function getObjectEmbedPattern() {
112
$prefix = $this->getObjectNamePrefix();
113
$prefix = preg_quote($prefix);
114
$id = $this->getObjectIDPattern();
116
return '(\B{'.$prefix.'('.$id.')((?:[^}\\\\]|\\\\.)*)}\B)u';
119
private function getObjectReferencePattern() {
120
$prefix = $this->getObjectNamePrefix();
121
$prefix = preg_quote($prefix);
123
$id = $this->getObjectIDPattern();
107
125
// If the prefix starts with a word character (like "D"), we want to
108
126
// require a word boundary so that we don't match "XD1" as "D1". If the
109
127
// prefix does not start with a word character, we want to require no word
121
139
// The "\b" allows us to link "(abcdef)" or similar without linking things
122
140
// in the middle of words.
124
$text = preg_replace_callback(
125
'((?<![#-])'.$boundary.$prefix.'('.$id.')(?:#([-\w\d]+))?(?!\w))u',
126
array($this, 'markupObjectReference'),
142
return '((?<![#-])'.$boundary.$prefix.'('.$id.')(?:#([-\w\d]+))?(?!\w))u';
147
* Extract matched object references from a block of text.
149
* This is intended to make it easy to write unit tests for object remarkup
150
* rules. Production code is not normally expected to call this method.
152
* @param string Text to match rules against.
153
* @return wild Matches, suitable for writing unit tests against.
155
public function extractReferences($text) {
156
$embed_matches = null;
158
$this->getObjectEmbedPattern(),
161
PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
165
$this->getObjectReferencePattern(),
168
PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
172
'embed' => $embed_matches,
173
'ref' => $ref_matches,
175
foreach ($sets as $type => $matches) {
176
$formatted = array();
177
foreach ($matches as $match) {
179
'offset' => $match[1][1],
180
'id' => $match[1][0],
182
if (isset($match[2][0])) {
183
$format['tail'] = $match[2][0];
185
$formatted[] = $format;
187
$results[$type] = $formatted;
132
193
public function markupObjectEmbed($matches) {