Text Formatted Code
I have expanded on this idea a bit. I wanted to support URLs and I've also
added support for story links and links within the site. You can specify
a label for the link with the bar character. E.g.:
[static:foobar] links to static page 'foobar' with the label taken from
the page title
[static:foobar|Foo Bar] links to static page 'foobar' with the label 'Foo Bar'.
I gave it a cheezy name, ezlink. I added an option, $_CONF['enable_ezlink'],
to config.php.
The code below may well be corrupted with stupid smiley faces/missing backslashes so
email me if you want a clean version. I've not tested this much so any comments welcome.
I added this code to lib-custom.php:
-------- Cut Here --------
// Expand 'ezlinks' to static pages and external sites
// Supports the following formats:
// [static:page-id] - Links to static page.
// [site:page-id] - Links to site root
// [story:story-id] - Links to story
// [http://example.com] - Links to URL
// Any link can be followed by a bar character '|' and some label text.
function process_ezlinks($text)
{
global $_TABLES, $_CONF, $_USER;
// Get the matches
$count = preg_match_all('{[([^:]*)<img align=absmiddle src='images/smilies/frown.gif' alt='Frown'>.*?)]}',$text,$match);
// Loop through the matches, replacing according to the text before the colon
for ($i = 0; $i < $count; $i++)
{
$command = $match[1][$i];
switch ($command)
{
case 'static':
$result = DB_query ("SELECT sp_title FROM {$_TABLES['staticpage']} WHERE (sp_id = '{$match[2][$i]}')");
$A = DB_fetchArray($result);
$url = $_CONF['site_url'] . '/staticpages/index.php?page=' . $match[2][$i];
_get_label($url, $label, $A['sp_title']);
$text = preg_replace(_normalise_match($match[0][$i]),
'<a href="' . $url . '">' . $label . '</a>',
$text);
break;
case 'story':
$result = DB_query ("SELECT title FROM {$_TABLES['stories']} WHERE (sid = '{$match[2][$i]}')");
$A = DB_fetchArray($result);
$url = $_CONF['site_url'] . '/article.php?story=' . $match[2][$i];
_get_label($url, $label, $A['title']);
$text = preg_replace(_normalise_match($match[0][$i]),
'<a href="' . $url . '">' . $label . '</a>',
$text);
break;
case 'site':
$url = $match[2][$i];
_get_label($url, $label);
$url = $_CONF['site_url'] . '/' . $url;
$text = preg_replace(_normalise_match($match[0][$i]),
'<a href="' . $url . '">' . $label . '</a>',
$text);
break;
case 'http':
$url = $match[2][$i];
_get_label($url, $label, substr($url, 2)); // Remove '//'
$text = preg_replace(_normalise_match($match[0][$i]),
'<a href="' . 'http:' . $url . '">' . $label . '</a>',
$text);
break;
}
}
return $text;
}
// Get the label from the URL and tidy the URL
// Encoded as URL or URL|label
function _get_label(&$url, &$label, $default_label=NULL)
{
$labelpos = strpos($url, '|');
if ($labelpos === FALSE)
$label = is_null($default_label) ? $url : $default_label;
else {
$label = substr($url, $labelpos + 1);
$url = substr($url, 0, $labelpos);
}
}
// Convert a link delimited by square backets into a suitable argument for 'preg_match'
// by backslash-escaping square brackets and wrapping with braces
function _normalise_match($text)
{
$text = str_replace('|', '|', $text);
return '{\[' . substr($text, 1, strlen($text) - 2) . ']\}';
}
-------- Cut Here --------
To implement this fully you need to change code in files staticpages/index.php,
plugins/staticpages/functions.inc and lib-common.php, I have supplied the changes
below with some context:
-------- staticpages/index.php --------
//Check for type (ie html or php)
if ($A['sp_php'] == 1) {
$retval .= eval (stripslashes ($A['sp_content']));
} else {
if (isset($_CONF['enable_ezlink']) && $_CONF['enable_ezlink'])
$retval .= process_ezlinks(stripslashes ($A['sp_content']));
else
$retval .= stripslashes ($A['sp_content']);
}
if ($A['sp_format'] <> 'blankpage') {
-------- plugins/staticpages/functions.inc --------
// Check for type (ie html or php)
if ($spresult['sp_php'] == 1) {
$retval .= eval (stripslashes ($spresult['sp_content']));
} else {
if (isset($_CONF['enable_ezlink']) && $_CONF['enable_ezlink'])
$retval .= process_ezlinks(stripslashes ($spresult['sp_content']));
else
$retval .= stripslashes ($spresult['sp_content']);
}
if (($_SP_CONF['in_block'] == 1) && !empty ($spresult['sp_title'])) {
-------- lib-common.php --------
function COM_article( $A, $index='', $storytpl='storytext.thtml' )
{
global $_TABLES, $mode, $_CONF, $_USER, $LANG01, $LANG05, $LANG11, $_THEME_URL;
$curtime = COM_getUserDateTimeFormat( $A['day'] );
$A['day'] = $curtime[0];
// Handle link expansions
if (isset($_CONF['enable_ezlink']) && $_CONF['enable_ezlink'])
{
$A['introtext'] = process_ezlinks( $A['introtext'] );
$A['bodytext'] = process_ezlinks( $A['bodytext'] );
}
// If plain text then replace newlines with <br> tags
if( $A['postmode'] == 'plaintext' )