Beispiel #1
0
 /**
  * transform codes to html
  *
  * [php]
  * // build the page
  * $context['text'] .= ...
  *
  * // transform codes
  * $context['text'] = Codes::render($context['text']);
  *
  * // final rendering
  * render_skin();
  * [/php]
  *
  * @link http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/
  *
  * @param string the input string
  * @return string the transformed string
  */
 public static function render($text)
 {
     global $context;
     // the formatting code interface
     include_once $context['path_to_root'] . 'codes/code.php';
     // streamline newlines, even if this has been done elsewhere
     $text = str_replace(array("\r\n", "\r"), "\n", $text);
     // prevent wysiwyg editors to bracket our own tags
     $text = preg_replace('#^<p>(\\[.+\\])</p>$#m', '$1', $text);
     // initialize only once
     static $patterns_map;
     if (!isset($patterns_map)) {
         if (Safe::filesize('codes/patterns.auto.php')) {
             include_once $context['path_to_root'] . 'codes/patterns.auto.php';
         } else {
             // core patterns
             $patterns_map['|<!-- .* -->|i'] = '';
             // remove HTML comments
             $patterns_map['|</h(\\d)>\\n+|i'] = '</h$1>';
             // strip \n after title
             $patterns_map['/\\n[ \\t]*(From|To|cc|bcc|Subject|Date):(\\s*)/i'] = BR . '$1:$2';
             // common message headers
             $patterns_map['/\\[escape\\](.*?)\\[\\/escape\\]/is'] = 'Codes::render_escaped';
             // [escape]...[/escape] (before everything)
             $patterns_map['/\\[php\\](.*?)\\[\\/php\\]/is'] = 'Codes::render_pre_php';
             // [php]...[/php]
             $patterns_map['/\\[snippet\\](.*?)\\[\\/snippet\\]/is'] = 'Codes::render_pre';
             // [snippet]...[/snippet]
             $patterns_map['/(\\[page\\].*)$/is'] = '';
             // [page] (provide only the first one)
             $patterns_map['/\\[(associate|member|anonymous|hidden|restricted|authenticated)\\](.*?)\\[\\/\\1\\]/is'] = 'Codes::render_hidden';
             // [associate]...[/associate]
             $patterns_map['/\\[redirect=([^\\]]+?)\\]/is'] = 'Codes::render_redirect';
             // [redirect=<link>]
             $patterns_map['/\\[execute=([^\\]]+?)\\]/is'] = 'Codes::render_execute';
             // [execute=<name>]
             $patterns_map['/\\[parameter=([^\\]]+?)\\]/is'] = 'Codes::render_parameter';
             // [parameter=<name>]
             $patterns_map['/\\[lang=([^\\]]+?)\\](.*?)\\[\\/lang\\]/is'] = 'Codes::render_lang';
             // [lang=xy]...[/lang]
             $patterns_map['/\\[(images)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [images=<ids>] (before other links)
             $patterns_map['/\\[(image)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [image=<id>]
             $patterns_map['/##(\\S.*?\\S)##/is'] = '<code>$1</code>';
             // ##...##
             $patterns_map['/\\[code\\](.*?)\\[\\/code\\]/is'] = '<code>$1</code>';
             // [code]...[/code]
             $patterns_map['/\\[indent\\](.*?)\\[\\/(indent)\\]/is'] = 'Skin::build_block';
             // [indent]...[indent]
             $patterns_map['/\\[quote\\](.*?)\\[\\/(quote)\\]/is'] = 'Skin::build_block';
             // [quote]...[quote]
             $patterns_map['/\\[folded(?:=([^\\]]+?))?\\](.*?)\\[\\/(folded)\\]\\s*/is'] = 'Skin::build_box';
             // [folded=title]...[/folded],[folded]...[/folded]
             $patterns_map['/\\[unfolded(?:=([^\\]]+?))?\\](.*?)\\[\\/(unfolded)\\]\\s*/is'] = 'Skin::build_box';
             // [unfolded=title]...[/unfolded],[unfolded]...[/unfolded]
             $patterns_map['/\\[sidebar(?:=([^\\]]+?))?\\](.*?)\\[\\/(sidebar)\\]\\s*/is'] = 'Skin::build_box';
             // [sidebar=title]...[/sidebar],[sidebar]...[/sidebar]
             $patterns_map['/\\[note\\](.*?)\\[\\/(note)\\]\\s*/is'] = 'Skin::build_block';
             // [note]...[/note]
             $patterns_map['/\\[caution\\](.*?)\\[\\/(caution)\\]\\s*/is'] = 'Skin::build_block';
             // [caution]...[/caution]
             $patterns_map['/\\[center\\](.*?)\\[\\/(center)\\]/is'] = 'Skin::build_block';
             // [center]...[/center]
             $patterns_map['/\\[right\\](.*?)\\[\\/(right)\\]/is'] = 'Skin::build_block';
             // [right]...[/right]
             $patterns_map['/\\[(go)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [go=<name>]
             $patterns_map['/\\[(article\\.description)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [article.description=<id>]
             $patterns_map['/\\[(article)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [article=<id>] or [article=<id>, title]
             $patterns_map['/\\[(next)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [next=<id>]
             $patterns_map['/\\[(previous)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [previous=<id>]
             $patterns_map['/\\[random(?:=([^\\]]+?))?\\]/i'] = 'Codes::render_random';
             // [random], [random=section:<id>] or [random=category:<id>]
             $patterns_map['/\\[(section)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [section=<id>] or [section=<id>, title]
             $patterns_map['/\\[(category(?:\\.description)?)=([^\\]]+?)\\]\\n*/is'] = 'Codes::render_object';
             // [category=<id>], [category=<id>, title] or [category.description=<id>]
             $patterns_map['/\\[(user)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [user=<id>]
             $patterns_map['/\\[(users)=([^\\]]+?)\\]/i'] = 'Codes::render_users';
             // [users=present]
             $patterns_map['/\\[(file|download)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [file=<id>] or [file=<id>, title] or download=<id>] or [download=<id>, title]
             $patterns_map['/\\[(comment)=([^\\]]+?)\\]/i'] = 'Codes::render_object';
             // [comment=<id>] or [comment=<id>, title]
             $patterns_map['/\\[(link)(?:=([^\\]]+?))?\\](.*?)\\[\\/link\\]/is'] = 'Codes::render_link';
             // [link]url[/link] or [link=label]url[/link]
             $patterns_map['/\\[(button)=([^\\]]+?)\\](.*?)\\[\\/button\\]/is'] = 'Codes::render_link';
             // [button=label]url[/button]
             $patterns_map['/\\[(button)=([^\\|]+?)\\|([^\\]]+?)]/is'] = 'Codes::render_link';
             // [button=label|url]
             $patterns_map['/\\[(click)=([^\\|]+?)\\|([^\\]]+?)]/is'] = 'Codes::render_link';
             // [click=label|url]
             $patterns_map['/(\\[)([^ ][^\\]\\|]+?[^ ])\\|([^ ][^\\]]+?[^ ])\\]/is'] = 'Codes::render_link';
             // [label|url]
             $patterns_map['#(\\s)([a-z]+?://[a-z0-9_\\-\\.\\~\\/@&;:=%$\\?]+)#'] = 'Codes::render_link';
             // make URL clickable
             $patterns_map['#(\\s)(www\\.[a-z0-9\\-]+\\.[a-z0-9_\\-\\.\\~]+(?:/[^,< \\r\\n\\)]*)?)#i'] = 'Codes::render_link';
             // web server url
             $patterns_map['/http[s]*:\\/\\/www\\.youtube\\.com\\/watch\\?v=([a-zA-Z0-9_\\-]+)[a-zA-Z0-9_\\-&=]*/i'] = '<iframe class="youtube-player" type="text/html" width="445" height="364" src="http://www.youtube.com/embed/$1" frameborder="0"></iframe>';
             // YouTube link
             $patterns_map['/http[s]*:\\/\\/youtu\\.be\\/([a-zA-Z0-9_\\-]+)/i'] = '<iframe class="youtube-player" type="text/html" width="445" height="364" src="http://www.youtube.com/embed/$1" frameborder="0"></iframe>';
             // YouTube link too
             $patterns_map['/\\[clicks=([^\\]]+?)]/is'] = 'Codes::render_clicks';
             // [clicks=url]  // @TODO: put in extension
             $patterns_map['/\\[email\\](.*?)\\[\\/email\\]/is'] = 'Codes::render_email';
             // [email]url[/email]
             $patterns_map['/(\\s)([a-z0-9_\\-\\.\\~]+?@[a-z0-9_\\-\\.\\~]+\\.[a-z0-9_\\-\\.\\~]+)/i'] = 'Codes::render_email';
             //  mail address
             $patterns_map['/\\[published(?:\\.([^\\]=]+?))?(?:=([^\\]]+?))?\\]\\n*/is'] = 'Codes::render_published';
             // [published(.decorated)], [published=section:4029], [published.decorated=section:4029,x]
             $patterns_map['/\\[updated(?:\\.([^\\]=]+?))?(?:=([^\\]]+?))?\\]\\n*/is'] = 'Codes::render_updated';
             // [updated(.decorated)], [updated(.decorated)=section:4029,x]
             $patterns_map['/\\[sections(?:\\.([^\\]=]+?))?(?:=([^\\]]+?))?\\]\\n*/is'] = 'Codes::render_sections';
             // [sections(.decorated)] (site map), [sections(.decorated)=section:4029] (sub-sections), [sections.simple=self] (assigned)
             $patterns_map['/\\[categories(?:\\.([^\\]=]+?))?(?:=([^\\]]+?))?\\]\\n*/is'] = 'Codes::render_categories';
             // [categories(.decorated)] (category tree), [categories(.decorated)=categories:4029] (sub-categories)
             $patterns_map['/\\[wikipedia=([^\\]]+?)\\]/is'] = 'Codes::render_wikipedia';
             // [wikipedia=keyword] or [wikipedia=keyword, title]
             $patterns_map['/\\[be\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/be.gif" alt="belgian flag" /> ';
             // [be] belgian flag
             $patterns_map['/\\[ca\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/ca.gif" alt="canadian flag" /> ';
             // [ca] canadian flag
             $patterns_map['/\\[ch\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/ch.gif" alt="swiss flag" /> ';
             // [ch] swiss flag
             $patterns_map['/\\[de\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/de.gif" alt="german flag" /> ';
             // [de] german flag
             $patterns_map['/\\[en\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/gb.gif" alt="english flag" /> ';
             // [en] english flag
             $patterns_map['/\\[es\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/es.gif" alt="spanish flag" /> ';
             // [es] spanish flag
             $patterns_map['/\\[fr\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/fr.gif" alt="french flag" /> ';
             // [fr] french flag
             $patterns_map['/\\[gr\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/gr.gif" alt="greek flag" /> ';
             // [gr] greek flag
             $patterns_map['/\\[it\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/it.gif" alt="italian flag" /> ';
             // [it] italian flag
             $patterns_map['/\\[pt\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/pt.gif" alt="portugal flag" /> ';
             // [pt] portugal flag
             $patterns_map['/\\[us\\]/i'] = ' <img src="' . $context['url_to_root'] . 'skins/_reference/flags/us.gif" alt="us flag" /> ';
             // [us] us flag
             $patterns_map['/\\[clear\\]\\n*/i'] = ' <br style="clear: both;" /> ';
             // [clear]
             $patterns_map['/\\[nl\\]\\n*/i'] = BR;
             // [nl] new line
             // load formatting codes from files
             $dir = $context['path_to_root'] . 'codes/';
             if ($handle = Safe::opendir($dir)) {
                 while (false !== ($file = Safe::readdir($handle))) {
                     if ($file == '..') {
                         continue;
                     }
                     if ($file == '.') {
                         continue;
                     }
                     //convention :
                     //get file only begining with code_
                     if (!(substr($file, 0, 5) == 'code_')) {
                         continue;
                     }
                     include_once $dir . $file;
                     //get formatting code patterns from this class
                     $classname = stristr($file, '.', TRUE);
                     $code = new $classname();
                     $code->get_pattern($patterns_map);
                     unset($code);
                 }
                 Safe::closedir($handle);
             }
             // cache all patterns in one unique file for next time
             Codes::save_patterns($patterns_map);
         }
         // end generating patterns from scratch
     }
     // end setting $patterns
     $text = Codes::process($text, $patterns_map);
     // done
     return $text;
 }