This page is dedicated to discussions on the development of a better link formatter, i.e. a formatter supporting (optional) link titles, anchors, url parameters etc.

I start posting some code that I'm using to test regex for parsing forced links.
It should correctly parse links like the following

[[HomePage | (!Hello world;p=1&q=2) This is the home page with title]]

giving something like this: This is the home page with title.

The parentheses are used to select ;-separated URL options, like title or URL parameters. The space before the parenthesis is optional.
!Hello world corresponds to the title.
p=1&q=2 corresponds to the URL parameters.

<?php

//$pattern = "/^\[\[(\S*)(\s\(\!(.+)\))?(\s+(.+))?\]\]$/s";
//$pattern = "/\[\[(.*)\]\]/";
$pattern = "/\[\[([^\(\)\s]+)(\s*\((.+)\))*(\s+.+)*\]\]/";

if ($q) {
    print 'String: '.$q;   
    preg_match($pattern, $q, $matches);
    if (is_array($matches)) {
        $url = $matches[1];
        $desc = $matches[4];
        $optionlist = $matches[3];
        if ($optionlist) {
            $options = explode(";", $optionlist);  
            if (is_array($options)) {
                echo '<br />Options: ';
                print_r($options);
                foreach($options as $option){
                    switch(true){
                        case (preg_match("/!(.+)/", $option, $istitle)):
                        $title = $istitle[1];
                        break;

                        case (preg_match("=", $option, $isparameter)):
                        $parameters = $isparameter[0];
                        break;
                    }
                }
            } else {
                echo '<br />Specified options are not valid';
            }
        } else {
            print '<br />No options';
        }
        echo '<br />Link chunks: ';
        print_r($matches);
        echo '<br />Rendered link: '.$this->Link($url, '', $desc, '', '', $title).'<br />';
    } else {
        echo 'String does not match pattern';
    }
} else {
    print 'Please enter a string';
}
?>



CategoryDevelopmentFormatters
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki