My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
          
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php

/**
* Texy! - web text markup-language
* --------------------------------
*
* Copyright (c) 2004, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the GNU GPL license that is bundled
* with this package in the file license.txt.
*
* For more information please see http://texy.info/
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE version 2 or 3
* @category Text
* @package Texy
* @link http://texy.info/
*/



/**
* Phrases module
* @package Texy
* @version $Revision$ $Date$
*/
final class TexyPhraseModule extends TexyModule
{

public $tags = array(
'phrase/strong' => 'strong', // or 'b'
'phrase/em' => 'em', // or 'i'
'phrase/em-alt' => 'em',
'phrase/ins' => 'ins',
'phrase/del' => 'del',
'phrase/sup' => 'sup',
'phrase/sup-alt' => 'sup',
'phrase/sub' => 'sub',
'phrase/sub-alt' => 'sub',
'phrase/span' => 'a',
'phrase/span-alt' => 'a',
'phrase/cite' => 'cite',
'phrase/acronym' => 'acronym',
'phrase/acronym-alt' => 'acronym',
'phrase/code' => 'code',
'phrase/quote' => 'q',
'phrase/quicklink' => 'a',
);


/** @var bool are links allowed? */
public $linksAllowed = TRUE;



public function __construct($texy)
{
$this->texy = $texy;

$texy->addHandler('phrase', array($this, 'solve'));

/*
// UNIVERSAL
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#((?>([*+/^_"~`-])+?))(?!\s)(.*(?!\\2).)'.TEXY_MODIFIER.'?(?<!\s)\\1(?!\\2)'.TEXY_LINK.'??()#Uus',
'phrase/strong'
);
*/

// ***strong+emphasis***
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\*)\*\*\*(?![\s*])(.+)'.TEXY_MODIFIER.'?(?<![\s*])\*\*\*(?!\*)'.TEXY_LINK.'??()#Uus',
'phrase/strong+em'
);

// **strong**
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\*)\*\*(?![\s*])(.+)'.TEXY_MODIFIER.'?(?<![\s*])\*\*(?!\*)'.TEXY_LINK.'??()#Uus',
'phrase/strong'
);

// //emphasis//
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<![/:])\/\/(?![\s/])(.+)'.TEXY_MODIFIER.'?(?<![\s/])\/\/(?!\/)'.TEXY_LINK.'??()#Uus',
'phrase/em'
);

// *emphasisAlt*
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\*)\*(?![\s*])(.+)'.TEXY_MODIFIER.'?(?<![\s*])\*(?!\*)'.TEXY_LINK.'??()#Uus',
'phrase/em-alt'
);

// ++inserted++
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\+)\+\+(?![\s+])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s+])\+\+(?!\+)()#Uu',
'phrase/ins'
);

// --deleted--
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<![<-])\-\-(?![\s>-])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s<-])\-\-(?![>-])()#Uu',
'phrase/del'
);

// ^^superscript^^
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\^)\^\^(?![\s^])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s^])\^\^(?!\^)()#Uu',
'phrase/sup'
);

// m^2 alternative superscript
$texy->registerLinePattern(
array($this, 'patternSupSub'),
'#(?<=[a-z0-9])\^([-\x{2212}]?[0-9]{1,4})(?![a-z0-9])#Uui',
'phrase/sup-alt'
);

// __subscript__
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\_)\_\_(?![\s_])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s_])\_\_(?!\_)()#Uu',
'phrase/sub'
);

// m_2 alternative subscript
$texy->registerLinePattern(
array($this, 'patternSupSub'),
'#(?<=[a-z])\_([0-9]{1,3})(?![a-z0-9])#Uui',
'phrase/sub-alt'
);

// "span"
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\")\"(?!\s)([^\"\r]+)'.TEXY_MODIFIER.'?(?<!\s)\"(?!\")'.TEXY_LINK.'??()#Uu',
'phrase/span'
);

// ~alternative span~
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\~)\~(?!\s)([^\~\r]+)'.TEXY_MODIFIER.'?(?<!\s)\~(?!\~)'.TEXY_LINK.'??()#Uu',
'phrase/span-alt'
);

// ~~cite~~
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\~)\~\~(?![\s~])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s~])\~\~(?!\~)'.TEXY_LINK.'??()#Uu',
'phrase/cite'
);

// >>quote<<
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\>)\>\>(?![\s>])([^\r\n]+)'.TEXY_MODIFIER.'?(?<![\s<])\<\<(?!\<)'.TEXY_LINK.'??()#Uu',
'phrase/quote'
);

// acronym/abbr "et al."((and others))
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!\")\"(?!\s)([^\"\r\n]+)'.TEXY_MODIFIER.'?(?<!\s)\"(?!\")\(\((.+)\)\)()#Uu',
'phrase/acronym'
);

// acronym/abbr NATO((North Atlantic Treaty Organisation))
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(?<!['.TEXY_CHAR.'])(['.TEXY_CHAR.']{2,})()\(\((.+)\)\)#Uu',
'phrase/acronym-alt'
);

// ''notexy''
$texy->registerLinePattern(
array($this, 'patternNoTexy'),
'#(?<!\')\'\'(?![\s\'])([^'.TEXY_MARK.'\r\n]*)(?<![\s\'])\'\'(?!\')()#Uu',
'phrase/notexy'
);

// `code`
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#\`(\S[^'.TEXY_MARK.'\r\n]*)'.TEXY_MODIFIER.'?(?<!\s)\`'.TEXY_LINK.'??()#Uu',
'phrase/code'
);


// ....:LINK
$texy->registerLinePattern(
array($this, 'patternPhrase'),
'#(['.TEXY_CHAR.'0-9@\#$%&.,_-]+)()(?=:\[)'.TEXY_LINK.'()#Uu',
'phrase/quicklink'
);


$texy->allowed['phrase/ins'] = FALSE;
$texy->allowed['phrase/del'] = FALSE;
$texy->allowed['phrase/sup'] = FALSE;
$texy->allowed['phrase/sub'] = FALSE;
$texy->allowed['phrase/cite'] = FALSE;
}



/**
* Callback for: **.... .(title)[class]{style}**:LINK
*
* @param TexyLineParser
* @param array regexp matches
* @param string pattern name
* @return TexyHtml|string|FALSE
*/
public function patternPhrase($parser, $matches, $phrase)
{
list(, $mContent, $mMod, $mLink) = $matches;
// [1] => **
// [2] => ...
// [3] => .(title)[class]{style}
// [4] => LINK

$tx = $this->texy;
$mod = new TexyModifier($mMod);
$link = NULL;

$parser->again = $phrase !== 'phrase/code' && $phrase !== 'phrase/quicklink';

if ($phrase === 'phrase/span' || $phrase === 'phrase/span-alt') {
if ($mLink == NULL) {
if (!$mMod) return FALSE; // means "..."
} else {
$link = $tx->linkModule->factoryLink($mLink, $mMod, $mContent);
}

} elseif ($phrase === 'phrase/acronym' || $phrase === 'phrase/acronym-alt') {
$mod->title = trim(Texy::unescapeHtml($mLink));

} elseif ($phrase === 'phrase/quote') {
$mod->cite = $tx->blockQuoteModule->citeLink($mLink);

} elseif ($mLink != NULL) {
$link = $tx->linkModule->factoryLink($mLink, NULL, $mContent);
}

return $tx->invokeAroundHandlers('phrase', $parser, array($phrase, $mContent, $mod, $link));
}



/**
* Callback for: any^2 any_2
*
* @param TexyLineParser
* @param array regexp matches
* @param string pattern name
* @return TexyHtml|string|FALSE
*/
public function patternSupSub($parser, $matches, $phrase)
{
list(, $mContent) = $matches;
$mod = new TexyModifier();
$link = NULL;
$mContent = str_replace('-', "\xE2\x88\x92", $mContent); // &minus;
return $this->texy->invokeAroundHandlers('phrase', $parser, array($phrase, $mContent, $mod, $link));
}



/**
* @param TexyLineParser
* @param array regexp matches
* @param string pattern name
* @return string
*/
public function patternNoTexy($parser, $matches)
{
list(, $mContent) = $matches;
return $this->texy->protect(Texy::escapeHtml($mContent), Texy::CONTENT_TEXTUAL);
}



/**
* Finish invocation
*
* @param TexyHandlerInvocation handler invocation
* @param string
* @param string
* @param TexyModifier
* @param TexyLink
* @return TexyHtml
*/
public function solve($invocation, $phrase, $content, $mod, $link)
{
$tx = $this->texy;

$tag = isset($this->tags[$phrase]) ? $this->tags[$phrase] : NULL;

if ($tag === 'a')
$tag = $link && $this->linksAllowed ? NULL : 'span';

if ($phrase === 'phrase/code')
$content = $tx->protect(Texy::escapeHtml($content), Texy::CONTENT_TEXTUAL);

if ($phrase === 'phrase/strong+em') {
$el = TexyHtml::el($this->tags['phrase/strong']);
$el->create($this->tags['phrase/em'], $content);
$mod->decorate($tx, $el);

} elseif ($tag) {
$el = TexyHtml::el($tag)->setText($content);
$mod->decorate($tx, $el);

if ($tag === 'q') $el->attrs['cite'] = $mod->cite;
} else {
$el = $content; // trick
}

if ($link && $this->linksAllowed) return $tx->linkModule->solve(NULL, $link, $el);

return $el;
}

}
Show details Hide details

Change log

r187 by Administrator on Nov 09, 2007   Diff
superscript m^-2 converts dash to minus
Go to: 
Project members, sign in to write a code review

Older revisions

r182 by Administrator on Oct 30, 2007   Diff
[No log message]
r179 by Administrator on Oct 09, 2007   Diff
* TexyHtml: renamed add() -> create,
addChild() -> add()
* added Texy::$nontextParagraph
* Texy for PHP4 - emulated throw and
Exception
...
r175 by Administrator on Aug 30, 2007   Diff
* improved TexyHeadingModule & dynamic
headers
* renamed TexyQuoteModule ->
TexyBlockQuoteModule
* added around handler for horizline
...
All revisions of this file

File info

Size: 10380 bytes, 335 lines

File properties

svn:keywords
Date Revision
Hosted by Google Code