-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyntax.php
121 lines (106 loc) · 3.7 KB
/
syntax.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
<?php
/**
* RSS Syntax Plugin: display feeds in your wiki.
*
* @author Algorys
*/
if (!defined('DOKU_INC')) die();
class syntax_plugin_rss extends DokuWiki_Syntax_Plugin {
public function getType() {
return 'container';
}
/**
* @return string Paragraph type
**/
public function getPType() {
return 'normal';
}
// Keep syntax inside plugin
function getAllowedTypes() {
return array('container', 'baseonly', 'substition','protected','disabled','formatting','paragraphs');
}
public function getSort() {
return 198;
}
function connectTo($mode) {
$this->Lexer->addSpecialPattern('<rss[^>]*>(?=.*)', $mode,'plugin_rss');
}
/**
* Do the regexp
**/
function handle($match, $state, $pos, Doku_Handler $handler) {
switch($state){
case DOKU_LEXER_SPECIAL :
case DOKU_LEXER_ENTER :
$data = array(
'state'=>$state,
'feed'=> "",
);
// Looking for id
preg_match("/feed *= *(['\"])(.*?)\\1/", $match, $feed);
if( count($feed) != 0 ) {
$data['feed'] = $feed[2];
}
return $data;
case DOKU_LEXER_UNMATCHED :
return array('state'=>$state, 'text'=>$match);
default:
return array('state'=>$state, 'bytepos_end' => $pos + strlen($match));
}
}
function _check_rss($data) {
$feed = $data['feed'];
try {
if(!@$fluxrss=simplexml_load_file($feed)) {
throw new Exception('Flux invalide');
}
if(empty($fluxrss->channel->title) || empty($fluxrss->channel->description) || empty($fluxrss->channel->item->title)) {
throw new Exception('Invalid Feed !');
}
//$renderer->doc .= '<p>Flux RSS trouvé !</p>';
} catch(Exception $e){
echo $e->getMessage();
}
return $fluxrss;
}
function _render_rss($renderer, $data){
$fluxrss = $this->_check_rss($data);
if($fluxrss) {
$renderer->doc .= '<img src="lib/plugins/rss/images/rss.png" alt="rss" class="rss">';
$renderer->doc .= '<h3>'.(string)$fluxrss->channel->title.'</h3>';
$renderer->doc .= '<p>'.(string)$fluxrss->channel->description.'</p>';
$i = 0;
$nb_to_display = 5;
$renderer->doc .= '<ul>';
foreach($fluxrss->channel->item as $item) {
$renderer->doc .= '<li>';
$renderer->doc .= '<a href="'.(string)$item->link.'">'.(string)$item->title.'</a>';
$renderer->doc .= '<i> ✎ '.$this->getLang('rss.publish').' '.(string)date('G\hi, d/m/Y', strtotime($item->pubDate)).'</i>';
$renderer->doc .= '</li>';
if(++$i>=$nb_to_display)
break;
}
}else {
$renderer->doc .= '<p>'.$this->getLang('rss.empty').' </p>';
}
}
// Dokuwiki Renderer
function render($mode, Doku_Renderer $renderer, $data) {
if($mode != 'xhtml') return false;
if($data['error']) {
$renderer->doc .= $data['text'];
return true;
}
$renderer->info['cache'] = false;
switch($data['state']) {
case DOKU_LEXER_SPECIAL :
case DOKU_LEXER_ENTER :
$this->_render_rss($renderer, $data);
break;
case DOKU_LEXER_UNMATCHED :
$renderer->doc .= $renderer->_xmlEntities($data['text']);
break;
}
return true;
}
}