From e67d67919401049341402525c63fd5cb5dbeefbb Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 14 Mar 2012 03:15:35 +0000 Subject: [PATCH] regexes are fun and pleasant to work with, in the same way that oranges are purple --- ext/bbcode/main.php | 71 +++++++++++++++++++-------------------------- lib/shimmie.js | 21 +++++++++----- 2 files changed, 43 insertions(+), 49 deletions(-) diff --git a/ext/bbcode/main.php b/ext/bbcode/main.php index 87eea7c8..eb5acf05 100644 --- a/ext/bbcode/main.php +++ b/ext/bbcode/main.php @@ -28,30 +28,25 @@ class BBCode extends FormatterExtension { public function format(/*string*/ $text) { $text = $this->extract_code($text); - $text = preg_replace("/site:\/\//s", make_http(get_base_href())."/", $text); - $text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text); - $text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text); - $text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text); - $text = preg_replace("/\[s\](.*?)\[\/s\]/s", "\\1", $text); - $text = preg_replace("/\[sup\](.*?)\[\/sup\]/s", "\\1", $text); - $text = preg_replace("/\[sub\](.*?)\[\/sub\]/s", "\\1", $text); - $text = preg_replace("/>>(\d+)(#c?(\d+))?/s", ">>\\1\\2", $text); - $text = preg_replace("/(^|\s)#(\d+)/s", "\\1#\\2", $text); - $text = preg_replace("/>>([^\d].+)/", "
\\1
", $text); - $text = preg_replace("/\[url=((?:https?|ftp|irc|mailto|site):\/\/.*?)\](.*?)\[\/url\]/s", "\\2", $text); - $text = preg_replace("/\[url\]((?:https?|ftp|irc|mailto|site):\/\/.*?)\[\/url\]/s", "\\1", $text); - $text = preg_replace("/\[email\](.*?)\[\/email\]/s", "\\1", $text); - $text = preg_replace("/\[img\](https?:\/\/.*?)\[\/img\]/s", "", $text); - $text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "\\2", $text); - $text = preg_replace("/\[\[([^\]]+)\]\]/s", "\\1", $text); - $text = preg_replace("/\n\s*\n/", "\n\n", $text); + foreach(array( + "b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4", + ) as $el) { + $text = preg_replace("!\[$el\](.*?)\[/$el\]!s", "<$el>$1", $text); + } + $text = preg_replace('!>>([^\d].+)!s', '
$1
', $text); + $text = preg_replace('!>>(\d+)(#c?\d+)?!s', '>>$1$2', $text); + $text = preg_replace('!\[url=site://(.*?)(#c\d+)?\](.*?)\[/url\]!s', '$3', $text); + $text = preg_replace('!\[url\]site://(.*?)(#c\d+)?\[/url\]!s', '$1$2', $text); + $text = preg_replace('!\[url=((?:https?|ftp|irc|mailto)://.*?)\](.*?)\[/url\]!s', '$2', $text); + $text = preg_replace('!\[url\]((?:https?|ftp|irc|mailto)://.*?)\[/url\]!s', '$1', $text); + $text = preg_replace('!\[email\](.*?)\[/email\]!s', '$1', $text); + $text = preg_replace('!\[img\](https?:\/\/.*?)\[/img\]!s', '', $text); + $text = preg_replace('!\[\[([^\|\]]+)\|([^\]]+)\]\]!s', '$2', $text); + $text = preg_replace('!\[\[([^\]]+)\]\]!s', '$1', $text); + $text = preg_replace("!\n\s*\n!", "\n\n", $text); $text = str_replace("\n", "\n
", $text); $text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "
\\1
", $text); $text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "
\\1 said:
\\2
", $text); - $text = preg_replace("/\[h1\](.*?)\[\/h1\]/s", "

\\1

", $text); - $text = preg_replace("/\[h2\](.*?)\[\/h2\]/s", "

\\1

", $text); - $text = preg_replace("/\[h3\](.*?)\[\/h3\]/s", "

\\1

", $text); - $text = preg_replace("/\[h4\](.*?)\[\/h4\]/s", "

\\1

", $text); while(preg_match("/\[list\](.*?)\[\/list\]/s", $text)) $text = preg_replace("/\[list\](.*?)\[\/list\]/s", "", $text); while(preg_match("/\[ul\](.*?)\[\/ul\]/s", $text)) @@ -68,26 +63,20 @@ class BBCode extends FormatterExtension { } public function strip(/*string*/ $text) { - $text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text); - $text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text); - $text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text); - $text = preg_replace("/\[s\](.*?)\[\/s\]/s", "\\1", $text); - $text = preg_replace("/\[code\](.*?)\[\/code\]/s", "\\1", $text); - $text = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/s", "\\2", $text); - $text = preg_replace("/\[url\](.*?)\[\/url\]/s", "\\1", $text); - $text = preg_replace("/\[email\](.*?)\[\/email\]/s", "\\1", $text); - $text = preg_replace("/\[img\](.*?)\[\/img\]/s", "", $text); - $text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "\\2", $text); - $text = preg_replace("/\[\[([^\]]+)\]\]/s", "\\1", $text); - $text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "", $text); - $text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "", $text); - $text = preg_replace("/\[h1\](.*?)\[\/h1\]/s", "\\1", $text); - $text = preg_replace("/\[h2\](.*?)\[\/h2\]/s", "\\1", $text); - $text = preg_replace("/\[h3\](.*?)\[\/h3\]/s", "\\1", $text); - $text = preg_replace("/\[h4\](.*?)\[\/h4\]/s", "\\1", $text); - $text = preg_replace("/\[\/?(list|ul|ol)\]/", "", $text); - $text = preg_replace("/\[li\](.*?)\[\/li\]/s", "\\1", $text); - $text = preg_replace("/\[\*\](.*?)/s", "\\1", $text); + foreach(array( + "b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4", + "code", "url", "email", "li", + ) as $el) { + $text = preg_replace("!\[$el\](.*?)\[/$el\]!s", '$1', $text); + } + $text = preg_replace("!\[url=(.*?)\](.*?)\[/url\]!s", '$2', $text); + $text = preg_replace("!\[img\](.*?)\[/img\]!s", "", $text); + $text = preg_replace("!\[\[([^\|\]]+)\|([^\]]+)\]\]!s", '$2', $text); + $text = preg_replace("!\[\[([^\]]+)\]\]!s", '$1', $text); + $text = preg_replace("!\[quote\](.*?)\[/quote\]!s", "", $text); + $text = preg_replace("!\[quote=(.*?)\](.*?)\[/quote\]!s", "", $text); + $text = preg_replace("!\[/?(list|ul|ol)\]!", "", $text); + $text = preg_replace("!\[\*\](.*?)!s", '$1', $text); $text = $this->strip_spoiler($text); return $text; } diff --git a/lib/shimmie.js b/lib/shimmie.js index 3b215126..b277b834 100644 --- a/lib/shimmie.js +++ b/lib/shimmie.js @@ -41,14 +41,19 @@ $(document).ready(function() { $("TABLE.sortable").tablesorter(); - $(".comment_link").each(function(idx, elm) { - var target_id = $(elm).text().match(/#c?(\d+)/); - if(target_id && $("#c"+target_id[1])) { - var target_name = $("#c"+target_id[1]+" .username").html(); - if(target_name) { - $(elm).attr("href", "#c"+target_id[1]); - $(elm).html("@"+target_name); - } + $(".shm-clink").each(function(idx, elm) { + var target_id = $(elm).data("clink-sel"); + if(target_id && $(target_id)) { + // if the target comment is already on this page, don't bother + // switching pages + $(elm).attr("href", target_id); + // highlight it when clicked + $(elm).click(function(e) { + $(target_id).effect('highlight', {}, 5000); + }); + // vanilla target name should already be in the URL tag, but this + // will include the anon ID as displayed on screen + $(elm).html("@"+$(target_id+" .username").html()); } });