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
|
<?php
/**
* TEXY! USER SYNTAX DEMO
*/
// include Texy!
require_once dirname(__FILE__) . 'Texy.php';
$texy = new Texy();
// disable *** and ** and * phrases
$texy->allowed['phrase/strong+em'] = FALSE;
$texy->allowed['phrase/strong'] = FALSE;
$texy->allowed['phrase/em-alt'] = FALSE;
$texy->allowed['phrase/em-alt2'] = FALSE;
// register my syntaxes:
// add new syntax: *bold*
$texy->registerLinePattern(
'userInlineHandler', // callback function or method
'#(?<!\*)\*(?!\ |\*)(.+)'.TexyPatterns::MODIFIER.'?(?<!\ |\*)\*(?!\*)()#U', // regular expression
'myInlineSyntax1' // any syntax name
);
// add new syntax: _italic_
$texy->registerLinePattern(
'userInlineHandler',
'#(?<!_)_(?!\ |_)(.+)'.TexyPatterns::MODIFIER.'?(?<!\ |_)_(?!_)()#U',
'myInlineSyntax2'
);
// add new syntax: .h1 ...
$texy->registerBlockPattern(
'userBlockHandler',
'#^\.([a-z0-9]+)\n(.+)$#m', // block patterns must be multiline and line-anchored
'myBlockSyntax1'
);
/**
* Pattern handler for inline syntaxes
*
* @param TexyLineParser
* @param array reg-exp matches
* @param string pattern name (myInlineSyntax1 or myInlineSyntax2)
* @return TexyHtml|string
*/
function userInlineHandler($parser, $matches, $name)
{
list(, $mContent, $mMod) = $matches;
$texy = $parser->getTexy();
// create element
$tag = $name === 'myInlineSyntax1' ? 'b' : 'i';
$el = TexyHtml::el($tag);
// apply modifier
$mod = new TexyModifier($mMod);
$mod->decorate($texy, $el);
$el->attrs['class'] = 'myclass';
$el->setText($mContent);
// parse inner content of this element
$parser->again = TRUE;
return $el;
}
/**
* Pattern handler for block syntaxes
*
* @param TexyBlockParser
* @param array regexp matches
* @param string pattern name (myBlockSyntax1)
* @return TexyHtml|string|FALSE
*/
function userBlockHandler($parser, $matches, $name)
{
list(, $mTag, $mText) = $matches;
$texy = $parser->getTexy();
// create element
if ($mTag === 'perex') {
$el = TexyHtml::el('div');
$el->attrs['class'][] = 'perex';
} else {
$el = TexyHtml::el($mTag);
}
// create content
$el->parseLine($texy, $mText);
return $el;
}
// processing
$text = file_get_contents('syntax.texy');
$html = $texy->process($text); // that's all folks!
// echo formated output
header('Content-type: text/html; charset=utf-8');
echo $html;
// and echo generated HTML code
echo '<hr />';
echo '<pre>';
echo htmlSpecialChars($html);
echo '</pre>';
|