plugin:xfortune
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
plugin:xfortune [2010-06-04 14:11] – 38.113.92.61 | plugin:xfortune [2018-06-06 10:06] – [Download] Klap-in | ||
---|---|---|---|
Line 6: | Line 6: | ||
email : andi@splitbrain.org | email : andi@splitbrain.org | ||
type : syntax | type : syntax | ||
- | lastupdate : 2008-01-25 | + | lastupdate : 2017-04-20 |
- | compatible : 2006-03-05 and higher | + | compatible : 2009-12-25, 2010-11-07 |
depends | depends | ||
conflicts | conflicts | ||
- | similar | + | similar |
tags : quotes, ajax, random | tags : quotes, ajax, random | ||
+ | |||
+ | downloadurl: | ||
+ | sourcerepo : https:// | ||
+ | bugtracker : https:// | ||
+ | donationurl: | ||
---- | ---- | ||
This plugin is able to read datafiles used by the popular Unix tool [[man> | This plugin is able to read datafiles used by the popular Unix tool [[man> | ||
+ | |||
+ | ===== Download ===== | ||
+ | |||
+ | Download and install the plugin using the [[plugin: | ||
+ | |||
+ | ==== Changes ==== | ||
+ | |||
+ | {{rss> | ||
===== Usage ===== | ===== Usage ===== | ||
Line 30: | Line 43: | ||
===== Input files ===== | ===== Input files ===== | ||
- | The cookie file needs to be a text file ((the uncompiled source files for fortune, not the binary ones created by [[man> | + | The cookie file needs to be a text file ((the uncompiled source files for fortune, not the binary ones created by [[man> |
Here is an example: | Here is an example: | ||
Line 45: | Line 58: | ||
</ | </ | ||
- | ===== Code ===== | + | ===== Known Shortcomings |
- | Create a directory called '' | + | * If your input files are not in UTF-8 this plugin will assume they are in latin1 and tries to convert them to UTF-8. If you get any charset problems, make sure the file is valid UTF-8. |
- | ==== syntax.php ==== | + | * Using compiled fortune cookies would probably be more effective |
+ | * It is implied above, but I think it is better to state it explicitly: at least for DW //Lemming// and the latest version of the plugin, | ||
- | Create this file in '' | ||
- | <code php syntax.php> | ||
- | <?php | ||
- | /** | ||
- | * Display Fortune cookies | ||
- | * | ||
- | * @license | ||
- | * @author | ||
- | */ | ||
- | |||
- | if(!defined(' | ||
- | if(!defined(' | ||
- | require_once(DOKU_PLUGIN.' | ||
- | |||
- | class syntax_plugin_xfortune extends DokuWiki_Syntax_Plugin { | ||
- | /** | ||
- | * return some info | ||
- | */ | ||
- | function getInfo(){ | ||
- | return array( | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ); | ||
- | } | ||
- | |||
- | /** | ||
- | * What kind of syntax are we? | ||
- | */ | ||
- | function getType(){ | ||
- | return ' | ||
- | } | ||
- | |||
- | /** | ||
- | * What about paragraphs? | ||
- | */ | ||
- | function getPType(){ | ||
- | return ' | ||
- | } | ||
- | |||
- | /** | ||
- | * Where to sort in? | ||
- | */ | ||
- | function getSort(){ | ||
- | return 302; | ||
- | } | ||
- | |||
- | /** | ||
- | * Connect pattern to lexer | ||
- | */ | ||
- | function connectTo($mode) { | ||
- | $this-> | ||
- | } | ||
- | |||
- | |||
- | /** | ||
- | * Handle the match | ||
- | */ | ||
- | function handle($match, | ||
- | $match = substr($match, | ||
- | |||
- | $data = array(); | ||
- | |||
- | //handle params | ||
- | list($cookie, | ||
- | |||
- | //xfortune cookie file | ||
- | $data[' | ||
- | |||
- | //time interval for changing cookies | ||
- | if(preg_match('/ | ||
- | $data[' | ||
- | }else{ | ||
- | $data[' | ||
- | } | ||
- | //no hammering please! | ||
- | if($data[' | ||
- | |||
- | return $data; | ||
- | } | ||
- | |||
- | /** | ||
- | * Create output | ||
- | */ | ||
- | function render($mode, | ||
- | if($mode == ' | ||
- | $renderer-> | ||
- | $renderer-> | ||
- | $renderer-> | ||
- | $renderer-> | ||
- | return true; | ||
- | } | ||
- | return false; | ||
- | } | ||
- | |||
- | function _script($cookie, | ||
- | $str = '< | ||
- | $str .= 'var plugin_xfortune_time = ' | ||
- | $str .= 'var plugin_xfortune_cookie = \'' | ||
- | $str .= " | ||
- | $str .= '</ | ||
- | return $str; | ||
- | } | ||
- | |||
- | /** | ||
- | * Returns one random cookie | ||
- | * | ||
- | * @author Andreas Gohr < | ||
- | */ | ||
- | function _getCookie($cookie){ | ||
- | $file = mediaFN($cookie); | ||
- | if(!@file_exists($file)) return ' | ||
- | |||
- | $dim = filesize($file); | ||
- | if($dim < 2) return " | ||
- | mt_srand( (double) microtime() * 1000000); | ||
- | $rnd = mt_rand(0, | ||
- | |||
- | $fd = fopen($file, | ||
- | if (!$fd) return " | ||
- | |||
- | // jump to random place in file | ||
- | fseek($fd, $rnd); | ||
- | |||
- | $text = ''; | ||
- | $line = ''; | ||
- | $cookie = false; | ||
- | $test = 0; | ||
- | while(true){ | ||
- | $seek = ftell($fd); | ||
- | $line = fgets($fd, 1024); | ||
- | |||
- | if($seek == 0){ | ||
- | // start of file always starts a cookie | ||
- | $cookie = true; | ||
- | if($line == " | ||
- | // ignore delimiter if exists | ||
- | continue; | ||
- | }else{ | ||
- | // part of the cookie | ||
- | $text .= htmlspecialchars($line).'< | ||
- | continue; | ||
- | } | ||
- | } | ||
- | |||
- | if(feof($fd)){ | ||
- | if($cookie){ | ||
- | // we had a cookie already, stop here | ||
- | break; | ||
- | }else{ | ||
- | // no cookie yet, wrap around | ||
- | fseek($fd, | ||
- | continue; | ||
- | } | ||
- | } | ||
- | |||
- | if($line == " | ||
- | if($cookie){ | ||
- | // we had a cookie already, stop here | ||
- | break; | ||
- | }elseif($seek == $dim -2){ | ||
- | // it's the end of file delimiter, wrap around | ||
- | fseek($fd, | ||
- | continue; | ||
- | }else{ | ||
- | // start of the cookie | ||
- | $cookie = true; | ||
- | continue; | ||
- | } | ||
- | } | ||
- | |||
- | // part of the cookie? | ||
- | if($cookie){ | ||
- | $text .= htmlspecialchars($line).'< | ||
- | } | ||
- | } | ||
- | fclose($fd); | ||
- | |||
- | // if it is not valid UTF-8 assume it's latin1 | ||
- | if(!utf8_check($text)) return utf8_encode($text); | ||
- | |||
- | return $text; | ||
- | } | ||
- | } | ||
- | |||
- | //Setup VIM: ex: et ts=4 enc=utf-8 : | ||
- | </ | ||
- | ==== ajax.php ==== | ||
- | |||
- | This is the backend to handle the AJAX requests. Put it into '' | ||
- | |||
- | <code php ajax.php> | ||
- | <?php | ||
- | /** | ||
- | * AJAX Backend Function for plugin_xfortune | ||
- | * | ||
- | * @author Andreas Gohr < | ||
- | */ | ||
- | |||
- | //fix for Opera XMLHttpRequests | ||
- | if(!count($_POST) && $HTTP_RAW_POST_DATA){ | ||
- | parse_str($HTTP_RAW_POST_DATA, | ||
- | } | ||
- | |||
- | if(!defined(' | ||
- | if(!defined(' | ||
- | require_once(DOKU_INC.' | ||
- | //close sesseion | ||
- | session_write_close(); | ||
- | |||
- | require_once(DOKU_INC.' | ||
- | require_once(DOKU_INC.' | ||
- | require_once(DOKU_PLUGIN.' | ||
- | |||
- | header(' | ||
- | $cookie = cleanID($_POST[' | ||
- | print syntax_plugin_xfortune:: | ||
- | |||
- | ?> | ||
- | </ | ||
- | ==== script.js ==== | ||
- | |||
- | The needed JavaScript goes into '' | ||
- | |||
- | <code javascript script.js> | ||
- | /** | ||
- | * Script for plugin_xfortune | ||
- | * | ||
- | * Fetches a new cookie | ||
- | * | ||
- | * @author Andreas Gohr < | ||
- | */ | ||
- | |||
- | function plugin_xfortune(){ | ||
- | if(!document.getElementById){ | ||
- | return; | ||
- | } | ||
- | var obj = document.getElementById(' | ||
- | if(obj === null){ | ||
- | return; | ||
- | } | ||
- | |||
- | // We use SACK to do the AJAX requests | ||
- | var ajax = new sack(DOKU_BASE+' | ||
- | ajax_qsearch.sack.AjaxFailedAlert = ''; | ||
- | ajax_qsearch.sack.encodeURIString = false; | ||
- | |||
- | // define callback | ||
- | ajax.onCompletion = function(){ | ||
- | var data = this.response; | ||
- | if(data === '' | ||
- | var out = document.getElementById(' | ||
- | |||
- | out.style.visibility = ' | ||
- | out.innerHTML = data; | ||
- | out.style.visibility = ' | ||
- | |||
- | // restart timer | ||
- | window.setTimeout(" | ||
- | }; | ||
- | |||
- | ajax.runAJAX(' | ||
- | } | ||
- | </ | ||
- | ==== style.css ==== | ||
- | |||
- | Style your fortune cookies through this file: '' | ||
- | |||
- | <code css style.css> | ||
- | div# | ||
- | height: 120px; | ||
- | padding: 0.5em; | ||
- | margin: 1em; | ||
- | text-align: right; | ||
- | font-style: italic; | ||
- | overflow: auto; | ||
- | } | ||
- | </ | ||
- | ===== Known Shortcomings ===== | ||
- | |||
- | * If your input files are not in UTF-8 this plugin will assume they are in latin1 and tries to convert them to UTF-8. If you get any charset problems, make sure the file is valid UTF-8. | ||
- | * Using compiled fortune cookies may be more effective |
plugin/xfortune.txt · Last modified: 2023-10-30 23:43 by Klap-in