File: demo.php

package info (click to toggle)
php-texy 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 680 kB
  • ctags: 1,299
  • sloc: php: 4,754; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (2)
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
<?php

/**
 * This demo shows how implement Texy! as comment formatter
 *     - relative links to other comment
 *     - rel="nofollow"
 *     - used links checking (antispam)
 */


// include Texy!
require_once dirname(__FILE__) . 'Texy.php';


/**
 * User handler for unknown reference
 *
 * @param TexyHandlerInvocation  handler invocation
 * @param string   [refName]
 * @return TexyHtml|string
 */
function newReferenceHandler($parser, $refName)
{
	$names = array('Me', 'Punkrats', 'Servats', 'Bonifats');

	if (!isset($names[$refName])) return FALSE; // it's not my job

	$name = $names[$refName];

	$el = TexyHtml::el('a');
	$el->attrs['href'] = '#comm-' . $refName; // set link destination
	$el->attrs['class'][] = 'comment';        // set class name
	$el->attrs['rel'] = 'nofollow';           // enable rel="nofollow"
	$el->setText("[$refName] $name:"); // set link label (with Texy formatting)
	return $el;
}


$texy = new Texy();

// references link [1] [2] will be processed through user function
$texy->addHandler('newReference', 'newReferenceHandler');

// configuration
TexyConfigurator::safeMode($texy);     // safe mode prevets attacker to inject some HTML code and disable images

// how generally disable links or enable images? here is a way:
//    $disallow = array('image', 'figure', 'linkReference', 'linkEmail', 'linkURL', 'linkQuick');
//    foreach ($diallow as $item)
//        $texy->allowed[$item] = FALSE;


// processing
$text = file_get_contents('sample.texy');
$html = $texy->process($text);  // that's all folks!


// echo formated output
header('Content-type: text/html; charset=utf-8');
echo $html;


// do some antispam filtering - this is just very simple example ;-)
$spam = FALSE;
foreach ($texy->summary['links'] as $link) {
	if (strpos($link, 'casino')) {
		$spam = TRUE;
		break;
	}
}


// and echo generated HTML code
echo '<hr />';
echo '<pre>';
echo htmlSpecialChars($html);
echo '</pre>';