Welcome to Geeklog, Anonymous Sunday, November 24 2024 @ 12:25 pm EST

Geeklog Forums

Easy linking to Static Pages


Status: offline

Euan

Forum User
Full Member
Registered: 04/22/02
Posts: 292
Suggestion:

At the moment, articles are parsed for [imageX]. A useful feature for some might be the ability to include links to static pages. eg:

"Here is an article about our site. If you want more information, please read the [SP:about-en] page."

The [SP:about-en] is replaced by the title of the static page and is made into a link to the page with that ID. If a page with that ID does not exist, an error is returned when saving the article.

This could also be used within the static pages to good effect.

Just an idea.

Cheers,

Euan.
-- Heather Engineering
-- No job too small
 Quote

Status: offline

Euan

Forum User
Full Member
Registered: 04/22/02
Posts: 292
I guess this is now the wrong place to post a follow up. But anyway. Please move if needed.

Text Formatted Code

function StaticPageLinkCheck($page) {
        global $_TABLES, $_CONF, $_USER;
        $count = preg_match_all("/<SP:(.*?)>/",$page,$match);
        $i = 0;
        while ( $i<$count ) {
                $result = DB_query ("SELECT sp_title FROM {$_TABLES['staticpage']} WHERE (sp_id = '{$match[1][$i]}')");
                $A = DB_fetchArray ($result);
                $page = preg_replace($match[0][$i],
  "a href="".$_CONF['site_url']."/staticpages/index.php?page=".$match[1][$i]."">".$A['sp_title']."</a",
  $page);
                $i++;
        }

        return $page;
}


 


Put this somewhere in your staticpages/index.php, and add the call to the function in the middle of the same page (here's a few lines from that area):

Text Formatted Code

        } else {
            $retval .= StaticPageLinkCheck(stripslashes ($A['sp_content']));
        }
    } else {
        if ($A['sp_php'] != 0) {


 

And if you have any non-php static pages, your links of the form
Text Formatted Code
<br />
will be turned into links with the title as the text of the link.

Not perfect, but if anyone wants to take it and run, please do. Thinking about it, this kind of thing would be more useful in the articles than the staticpages (where all users should be at least basically literate). Code appears a mess here so email me for the modified file if you're interested.

Cheers,
Euan.
-- Heather Engineering
-- No job too small
 Quote

Status: offline

James Fryer

Forum User
Junior
Registered: 08/06/02
Posts: 17
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' )

 

 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
May I suggest (to both of you) to use the [CODE] tag (not HTML's &lt;code&gt; tag) when you post code? It even has its own button in the forum's editor ...

bye, Dirk
 Quote

Status: offline

Euan

Forum User
Full Member
Registered: 04/22/02
Posts: 292
May I suggest (to both of you) to use the [ CODE ] tag (not HTML's tag) when you post code? It even has its own button in the forum's editor ...


I thought I did...? maybe the forum doesn't like me...

James, nice extension. Now just needs to be added to articles.php. Mr. Green

Euan.
-- Heather Engineering
-- No job too small
 Quote

Status: offline

James Fryer

Forum User
Junior
Registered: 08/06/02
Posts: 17
Euan: As far as I can see, the code in article.php calls 'COM_article' which is patched. I can't find a way to view the article without my code being called.

Dirk: I think there is a problem with the code tag in the forum. What follows is a colon, parenthesis, space and backslash enclosed in square-bracketed code tags:

Text Formatted Code
smiley <img align=absmiddle src='images/smilies/smile.gif' alt='Smile'> backslash \
 

 Quote

Status: offline

James Fryer

Forum User
Junior
Registered: 08/06/02
Posts: 17
Interesting -- it seems backslashes are stripped by preview but not by submit.
 Quote

All times are EST. The time is now 12:25 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content