<?php
/*
Plugin Name: Useful text replacements
Plugin URI: http://blog.blackhc.net
Description: Some useful text replacement for annotations and other nifty things.
Version: 1.01
Author: Andreas 'BlackHC' Kirsch
Author URI: http://blog.blackhc.net
Plugin Name: Latex for WordPress
Plugin URI: http://zhiqiang.org/blog/plugin/mimetex
Description: using WordPress.com or public MimeTex service to add latex formula in post and comment. You don't need to install your own latex service.
Version: 2.01
Author: zhiqiang
Author URI: http://zhiqiang.org
*/
/*
1.3 and higher are maintained by Zhiqiang.
Copyright 2006 Anders Dahnielson (email : anders@dahnielson.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
Version-History:
1.1.2:
Tweaker: Martin Becker
Email: mbecker@physik.uni-wuerzburg.de
Homepage: http://fstyle.de
- Edited discription of plugin, so correct tags will be shown
- Put inclusion of Snoopy-Class down into the class (mimetex),
for in some cases WP will intialize the Snoopy-Class of its own
after we do here, causing it to not to be able to reinitialize.
For some reason I don't understand.
Note: I think all this workaround using our own extra Snoopy-Class can be
fixed by the wordpress-team by removing the if statement in their Snoopy-Class.
That would be the case for ./wp-includes/rss-functions.php in ./wp-admin/index.php
- Cleaner code
1.1.1:
Tweaker: Baris Evrim Demiroz
Email: b.evrim /AT/ anlak /DOT/ com
Homepage: http://www.anlak.com
- Switched to Snoopy
- Working in comments as well: Thanks to Robert Jones, he gave the code snippet for plugin to work with comments. You may wish to visit him: http://www.jonesieboy.co.uk/blog
1.1.0:
Anders Dahnielson, URI: http://dahnielson.com
- Anders Dahnielson's original version.
*/
class blackhc_replacements{
var $uniqueID = 0;
var $server = "http://l.wordpress.com/latex.php?bg=ffffff&amp;amp;amp;fg=000000&amp;amp;amp;latex=";
var $img_format = "png";
// $img_format is 'gif' when using mimetex service.
// more server:
// "http://l.wordpress.com/latex.php?latex=";
// "http://www.bytea.net/cgi-bin/mimetex.cgi?formdata=";
function getUniqueID() {
return $this->uniqueID++;
}
// parsing the text to display tex by putting tex-images-tags into the code created by createTex
function parse($toParse) {
// tag specification (which tags are to be replaced)
// change it to
// $regex = '#\1 stype in your post
//$regex = '#\$\$(.*?)\$\$#si';
$text = $toParse;
$oldText = "";
while( $oldText != $text ) {
$oldText = $text;
$text = preg_replace_callback('#\(\)!expandable\:(?:(?P<link>[^
!(?:(?>[^
]+)|(?P>brackets))*
!
]*?)\#(?P<link>[^
!
]*?)\#)?(.*?)
#si', array(&amp;amp;amp;$this, 'createAnnotation'), $text);
$text = preg_replace_callback('#\(\)!wikipedia\:(.*?)(?:(?:\#|\:)(.*?))?
#si', array(&amp;amp;amp;$this, 'createWikiLink'), $text);
$text = preg_replace( array('#\(\)!escape_left
#si', '#\(\)!escape_right
#si'), array( '[' . '[', ']' . ']'), $text );
return $text;
}
function createWikiLink($match)
{
$text = $match[2];
$page = $match[1];
if( !$text )
$text = $page;
return "<a href=\"http://en.wikipedia.org/wiki/".($page)."\" target=\"_blank\" >".($text)."</a>";
}
function createAnnotation($match)
{
$superscript = $match[1];
$text = $match[2];
if( $superscript == "" )
$marker = "<strong>&amp;amp;amp;#180;</strong>";
else
$marker = "<sup><strong>" . ($superscript) . "</strong></sup>";
return "<a class=\"annotation\" title=\"".($text)."\" href=\"javascript:;\">" . ($marker) . "</a>";
}
// Big Thanks to Salteh!
function createExpandableText($match)
{
$linkText = $match["link"];
$text = $match["content"];
if ($linkText == "") {
$linkText = "Click to expand";
}
$id = "blackhc_expandable_" . ($this->getUniqueID());
return '<div class="expandable">' .
'<a class="expandable_link" href="javascript:blackhc_replacements_togglediv(\''. $id . '\');">' . $linkText . ' &amp;amp;amp;#187;</a>' .
'<div class="expandable_text" name="' . $id . '" id="' . $id . '" style="display:none">' . $text . '</div><br>' .
'</div>';
}
// fix for a php bug
// the two functions can be removed as soon as preg_replace_callback passes named subpatterns correctly: http://bugs.php.net/bug.php?id=45748
function createExpandableTextFix( $match ) {
return $this->createExpandableText( array( "link" => $match[1], "content" => $match[2] ) );
}
function createExpandableTextExFix( $match ) {
return $this->createExpandableText( array( "link" => $match[2], "content" => $match[3] ) );
}
// reading the tex-expression and create an image and a image-tag representing that expression
function createTex($toTex) {
$formula_text = $toTex[1];
$imgtext=false;
if (substr($formula_text, -1, 1) == "!") return "
";
if (substr($formula_text, 0, 1) == "!") { $imgtext=true;$formula_text=substr($formula_text, 1);}
$formula_hash = md5($formula_text);
$formula_filename = 'tex_'.$formula_hash.'.'.$this->img_format;
$cache_path = ABSPATH . '/wp-content/cache/';
$cache_formula_path = $cache_path . $formula_filename;
$cache_url = get_bloginfo('wpurl') . '/wp-content/cache/';
$cache_formula_url = $cache_url . $formula_filename;
if ( !is_file($cache_formula_path)) {
if (!class_exists('Snoopy')) require_once (ABSPATH.'wp-includes/class-snoopy.php');
$snoopy = new Snoopy;
$snoopy->fetch( $this->server.rawurlencode(html_entity_decode($formula_text)));
// this will copy the created tex-image to your cache-folder
if(strlen($snoopy->results)){
$cache_file = fopen($cache_formula_path, 'w');
fputs($cache_file, $snoopy->results);
fclose($cache_file);
}
}
// returning the image-tag, referring to the image in your cache folder
if($imgtext) return "<center><img src=\"$cache_formula_url\" align=\"absmiddle\" class=\"tex\" alt=\"".($formula_text)."\" /></center>";
return "<img src=\"$cache_formula_url\" align=\"absmiddle\" class=\"tex\" alt=\"".($formula_text)."\" />";
}
function headerAdditions() {
// additions for expandable text
echo '
<script language="JavaScript">
function blackhc_replacements_togglediv(divname) {
var div = document.getElementById(divname);
div.style.display = (div.style.display=="none") ? "" : "none";
}
</script>
';
}
}
$mimetex_object = new blackhc_replacements;
// this specifies where parsing should be done. one can look up further information on wordpress.org
add_action('wp_head', array($mimetex_object, 'headerAdditions') );
add_filter('the_title', array($mimetex_object, 'parse'), 1);
add_filter('the_content', array($mimetex_object, 'parse'), 1);
add_filter('the_excerpt', array($mimetex_object, 'parse'), 1);
// dont allow embedding stuff into comments for security reasons
//add_filter('comment_text', array($mimetex_object, 'parseTex'), 1);
?>