<?php

// $Date: 2004/02/03 03:34:13 $
// $Revision: 1.5 $
// $Author: jcrocholl $

function cmp_length($a, $b) {
    
$la = strlen($a);
    
$lb = strlen($b);
    
// print "$a $la $b $lb<br/>\n";
    
if ($la < $lb) return 1;
    if (
$la > $lb) return -1;
    if (
$a < $b) return -1;
    if (
$a > $b) return 1;
    return 
0;
}

function 
load_links(&$links, $path) {
    
$filename = $path . 'Links';
    
// print "$filename<br/>\n";
    
if (file_exists($filename)) {
        
$input = file($filename);
        foreach (
$input as $line) {
            if (
preg_match('/^\s*(\S+)\s+(\S.*\S)\s*$/', $line, $m)) {
                
// print "$m[2] => $m[1]<br/>\n";
                
$links[$m[2]] = $m[1];
            }
        }
    }
    if (!
$links) return $text;
    
uksort($links, "cmp_length");
}

function 
links($text, $links) {
    if (!
$links) return $text;
    foreach (
$links as $caption => $link) {
        if (
$link != '-') {
            
// print "link $caption => $link<br/>\n";
            
$replace = "<a href=\"$link\">$caption</a>";
            
// only available with PHP >= 5
            // $text = str_ireplace($caption, $replace, $text);
            
$caption = str_replace('/', '\/', $caption);
            if (
preg_match("/^(.*)$caption(.*)$/si", $text, $m))
                return 
                    
links($m[1], $links) . 
                    
$replace . 
                    
links($m[2], $links);
        }
    }
    return 
$text;
}

function 
include_files($path, $text) {
    if (
preg_match("/^(.*?)\[((.+?)(\..+?))\](.*)$/s", $text, $m)) {
        if (
file_exists("$path$m[2]")) {
            if (
$m[4] == '.png') {
                return 
$m[1] . 
                    
"<img class=\"inline\" align=\"left\" src=\"$m[2]\" />" . 
                    
include_files($path, $m[5]);
            } else {
                
$result = '';
                foreach (
file("$path$m[2]") as $line) {
                    if (
$result) $result .= "<br/>\n";
                    
$result .= rtrim($line);
                }
                
$result = $m[1] . "<code>$result</code>" .
                    
include_files($path, $m[5]);
                return 
$result;
            }
        } else {
            return 
$m[1] . 
                    
"[<span class=\"red\">$m[2]</span>]" . 
                    
include_files($path, $m[5]);
        }
    }
    return 
$text;
}

function 
bold($text) {
    if (
preg_match("/^(.*?)\*(.+?)\*(.*)$/s", $text, $m)) {
        return 
$m[1] . "<b>$m[2]</b>" . bold($m[3]);
    }
    return 
$text;
}

function 
h($level, &$text, $link = '') {
    global 
$previous_par, $links; 
    
$previous_par = false;
    if (
$text) {
        
// print "link $link<br/>\n";
        
if ($link) {
            
$text = "<a href=\"$link\">$text</a>";
        } elseif (
$level == 2) {
            
$text = links($text, $links);
        }
        
$result = "<h$level>$text</h$level>\n";
    }
    
$text = '';
    return 
$result;
}

function 
wrap($path, $text, $links, $class) {
    global 
$previous_par; 
    if (
$text) {
        
$aclass = 'justify';
        if (
$previous_par) $aclass .= ' margin-top';
        
$result  = "<p class=\"$aclass";
        if (
$class) $result .= " $class";
        
$result .= "\">$text</p>\n";
        
$previous_par = true;
        return 
wordwrap(bold(include_files($path, links($result, $links))));
    }
    return 
"";
}

function 
par($path, &$text, $links, $class) {
    
$result = wrap($path, $text, $links, $class);
    
$text = '';
    return 
$result;
}

function 
format_input($path, $input, $links, $pp, $class = '') {
    global 
$previous_par; 
    
$previous_par = $pp;
    foreach (
$input as $line) {
        
$line = trim($line);
        if (!
$line) {
            
$result .= par($path, $text, $links, $class);
        } elseif (
preg_match('/^=+$/', $line)) {
            
$result .= h(1, $text);
        } elseif (
preg_match('/^-+$/', $line)) {
            
$result .= h(2, $text);
        } elseif (
preg_match('/^-\s+(.+)$/', $line, $m)) {
            if (
$text) $text .= "<br/>\n";
            
$text .= "&bull;&nbsp;$m[1]";
        } else {
            if (
$text) $text .= ' ';
            
$text .= $line;
        }
    }
    
$result .= par($path, $text, $links, $class);
    return 
$result;
}

function 
format_file($path, $filename, $links, $pp) {
    
$input = file("$path$filename");
    return 
format_input($path, $input, $links, $pp);
}

?>