Index: trunk/ATTIC/silly-regex-linker/silly-regex-linker.php |
— | — | @@ -0,0 +1,111 @@ |
| 2 | +<?php |
| 3 | +/* |
| 4 | +Plugin Name: Silly Regex Linker |
| 5 | +Plugin URI: http://leuksman.com/pages/Silly_Regex_Linker |
| 6 | +Description: A silly regex linker plugin for WordPress. |
| 7 | +Version: 0.1 |
| 8 | +Author: Brion Vibber |
| 9 | +Author URI: http://leuksman.com/ |
| 10 | +*/ |
| 11 | + |
| 12 | +// filter hook... |
| 13 | + |
| 14 | +class SillyRegexLinker { |
| 15 | + public static function applyLinks( $text ) { |
| 16 | + $prev = error_reporting(E_ALL); |
| 17 | + |
| 18 | + $regexes = self::fetchRegexes(); |
| 19 | + foreach( $regexes as $regex => $callback ) { |
| 20 | + $text = preg_replace_callback( $regex, |
| 21 | + array( 'SillyRegexLinker', $callback ), |
| 22 | + $text ); |
| 23 | + } |
| 24 | + |
| 25 | + error_reporting( $prev ); |
| 26 | + return $text; |
| 27 | + } |
| 28 | + |
| 29 | + private static function fetchRegexes () { |
| 30 | + return array( |
| 31 | + '/r(\d+)/' => 'svnLink', |
| 32 | + '/bug\s+(\d+)/' => 'bugLink', |
| 33 | + '/\[\[([^]]+)\|([^]]+)\]\]/' => 'wikiLink', |
| 34 | + '/\[\[([^]|]+)\]\]/' => 'wikiLinkNoText', |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + private static function wikiLink( $matches ) { |
| 39 | + return self::doWikiLink( $matches[1], $matches[2] ); |
| 40 | + } |
| 41 | + |
| 42 | + private static function wikiLinkNoText( $matches ) { |
| 43 | + return self::doWikiLink( $matches[1], $matches[1] ); |
| 44 | + } |
| 45 | + |
| 46 | + private static function doWikiLink( $link, $text ) { |
| 47 | + $link = self::deCurl( $link ); |
| 48 | + |
| 49 | + list( $base, $page ) = self::splitInterwiki( trim( $link ) ); |
| 50 | + |
| 51 | + $clean = str_replace( '%2f', '/', |
| 52 | + str_replace( '%3a', ':', |
| 53 | + rawurlencode( |
| 54 | + str_replace( ' ', '_', $page ) ) ) ); |
| 55 | + $url = str_replace( '$1', $clean, $base ); |
| 56 | + |
| 57 | + return self::doLink( $url, $text ); |
| 58 | + } |
| 59 | + |
| 60 | + private static function deCurl( $text ) { |
| 61 | + return str_replace( '’', "'", $text ); |
| 62 | + } |
| 63 | + |
| 64 | + private static function splitInterwiki( $link ) { |
| 65 | + $chunks = array_map( 'trim', explode( ':', $link, 2 ) ); |
| 66 | + if( count( $chunks > 1 ) ) { |
| 67 | + $normalizedPrefix = strtolower( $chunks[0] ); |
| 68 | + $interwikiBase = self::getInterwiki( $normalizedPrefix ); |
| 69 | + if( $interwikiBase ) { |
| 70 | + return array( $interwikiBase, $chunks[1] ); |
| 71 | + } |
| 72 | + } |
| 73 | + $defaultBase = 'http://leuksman.com/pages/$1'; |
| 74 | + return array( $defaultBase, $link ); |
| 75 | + } |
| 76 | + |
| 77 | + private static function getInterwiki( $prefix ) { |
| 78 | + $map = array( |
| 79 | + 'wikipedia' => 'http://en.wikipedia.org/wiki/$1', |
| 80 | + 'meta' => 'http://meta.wikimedia.org/wiki/$1', |
| 81 | + 'mw' => 'http://www.mediawiki.org/wiki/$1', |
| 82 | + ); |
| 83 | + if( isset( $map[$prefix] ) ) { |
| 84 | + return $map[$prefix]; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + private static function svnLink( $matches ) { |
| 90 | + $rev = intval( $matches[1] ); |
| 91 | + return self::doLink( |
| 92 | + "http://svn.wikimedia.org/viewvc/mediawiki?view=rev&revision=$rev", |
| 93 | + $matches[0] ); |
| 94 | + } |
| 95 | + |
| 96 | + private static function bugLink( $matches ) { |
| 97 | + $bug = intval( $matches[1] ); |
| 98 | + return self::doLink( |
| 99 | + "http://bugzilla.wikimedia.org/show_bug.cgi?id=$bug", |
| 100 | + $matches[0] ); |
| 101 | + } |
| 102 | + |
| 103 | + private static function doLink( $url, $text ) { |
| 104 | + $escapedUrl = htmlspecialchars( $url ); |
| 105 | + return "<a href=\"$escapedUrl\">$text</a>"; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +add_filter( 'the_content', array( 'SillyRegexLinker', 'applyLinks' ) ); |
| 110 | +add_filter( 'comment_text', array( 'SillyRegexLinker', 'applyLinks' ) ); |
| 111 | + |
| 112 | +?> |
\ No newline at end of file |
Property changes on: trunk/ATTIC/silly-regex-linker/silly-regex-linker.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 113 | + native |