Including wiki-style links (i.e., non-munged URLs)

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
brianko
Posts: 1
Joined: 09 Mar 2008, 16:18

Including wiki-style links (i.e., non-munged URLs)

Post by brianko »

I'm at a loss as to why all the voodoo over munging up URLs, but since I obviously don't have the Big Picture, I decided it was best not to hack all that nonsense away in my install. So, I've modified John Pye's original hack to implement wiki-style links that render correctly as highlighted targets by:
  • Updating his code to 1.0.8 (the latest version I'm running)
  • Adding "interlink" functionality that permits multiple URLs to be constructed
The latter necessitated some changes in syntax, so this patch is probably not backwards-compatible with John's original patch.

The syntax looks something like this:

Code: Select all

[[interlink:path target]]
where interlink is a string defined in $g_wiki_interlinks
path is the URL path
target is analogous to the anchor tag target (what you want displayed)

So, if you have this in config_inc.php:

Code: Select all

$g_wiki_interlinks = array(
  'wikka' => 'http://wikkawiki.org');
you would create a link to the WikkaWiki HomePage like this:

Code: Select all

[[wikka:HomePage WikkaWiki Home Page]]
and that would display as WikkaWiki Home Page

The patch is below (I tried to attach a file, but after several tries with getting past the "extension not allowed" filter, I gave up). It's not been thoroughly vetted except in my own installation, so YMMV. I've not tested this in any release other than 1.0.8.

Code: Select all

--- ./config_inc.php	2008/03/08 16:48:05	1.2
+++ ./config_inc.php	2008/03/09 16:14:47	1.3
@@ -1647,4 +1647,14 @@
 	#   )
 	# );
 	$g_custom_group_actions = array();
+
+	### Wiki linking ###
+	# format: [[interlink:basepath target]]
+	# where 'interlink' is a string defined in $g_wiki_interlinks
+	#       'url' is the base path of the desired URL
+	#       'target' is the string to be displayed for the link
+	$g_wiki_link_url = $g_path;
+	$g_wiki_interlinks = array(
+		'wikka' => 'http://wikkawiki.org/');
+	$g_changeset_link_url = '';
 ?>
--- ./string_api.php	2008/03/08 17:11:24	1.1
+++ ./string_api.php	2008/03/09 05:06:14	1.2
@@ -101,6 +101,8 @@
 		$p_string = string_process_bug_link( $p_string );
 		$p_string = string_process_bugnote_link( $p_string );
 		$p_string = string_process_cvs_link( $p_string );
+		$p_string = string_process_wiki_link($p_string, true);
+		$p_string = string_process_changeset_link($p_string);
 
 		return $p_string;
 	}
@@ -347,6 +349,73 @@
 
 		return $t_result;
 	}
+# Process the string, returning full HTML links to a wiki, if configured.
+
+function string_process_wiki_link($string, $fullHTML = true) {
+	if (!config_get('wiki_link_url')) return $string;
+	if ($fullHTML) return preg_replace_callback('/\[\[([^ ]*)\s+([^\]]*)\]\]/', 'string_process_wiki_link_callback_fullhtml', $string);
+	return preg_replace_callback('/\[\[(([^\]]*?))\]\]/', 'string_process_wiki_link_callback_nohtml', $string);
+}
+
+# Parse interlink string (the part preceded by :)
+function string_process_parse_interlink($string)
+{
+	if(!config_get('wiki_interlinks')) return config_get('wiki_link_url');	
+	$interlinks = config_get('wiki_interlinks');
+	if(!isset($string)) return config_get('wiki_link_url'); 
+	$url = $interlinks["$string"];
+	if(!isset($url)) return config_get('wiki_link_url');
+	return $url;
+}
+
+# --------------------
+# Subsitution callback for the regexp in string_process_wiki_link(); HTML version.
+function string_process_wiki_link_callback_fullhtml($words) {
+	$p_url = $words[1];
+	$p_target = $words[2];
+	$interlink = split(":", $p_url);
+	$p_link = '';
+	if(!empty($interlink[1])) 
+	{
+		 $p_link = string_process_parse_interlink($interlink[0]).$interlink[1];
+	}
+	else
+	{
+		$p_link = config_get('wiki_link_url').$interlink[0];
+	}
+	return '<span class="bracket-link">'. "<a href=\"$p_link\" target=\"_blank\">$p_target</a>". '</span> ';
+}
+
+# --------------------
+# Subsitution callback for the regexp in string_process_wiki_link(); plaintext version.
+function string_process_wiki_link_callback_nohtml($words) {
+	$p_url_text = $words[2];
+	$p_link = config_get('wiki_link_url').$p_url_text;
+	return "$p_url_text ($p_link)";
+}
+
+# ---------------------
+# Process 'changeset' links to ViewCVS pages
+function string_process_changeset_link($string, $fullHTML = true) {
+	if(!config_get('changeset_link_url'))return $string;
+	$re = '/\b(version|revision|rev|changeset)\s+([0-9]+(\.[0-9]+)*)\b/';
+	if($fullHTML)return preg_replace_callback($re,'string_process_changeset_link_callback_fullhtml', $string);
+	return preg_replace_callback($re,'string_process_changeset_link_callback_nohtml', $string);
+}
+
+function string_process_changeset_link_callback_fullhtml($words) {
+	$p_link_text = $words[0];
+	$p_changeset = $words[2];
+	$p_url = config_get('changeset_link_url').$p_changeset;
+	return '<span class="viewcvs-link">'. "<a href=\"$p_url\">$p_link_text</a>". "&nbsp;[<a title=\"open in a new window\" href=\"$p_url\" target=_blank>^</a>]".'</span> ';
+}
+
+function string_process_changeset_link_callback_nohtml($words) {
+	$p_link_text = $words[0];
+	$p_changeset = $words[2];
+	$p_url = config_get('changeset_link_url').$p_url_text;
+	return "$p_link_text ($p_url)";
+}
 
 	#===================================
 	# Tag Processing
Post Reply