--- pearwiki_filter.module.cvs	2008-07-26 14:12:55.000000000 +0200
+++ pearwiki_filter.module	2008-07-26 13:54:32.000000000 +0200
@@ -467,6 +467,12 @@ function pearwiki_filter_process ($text,
     return '<pre>'.check_plain($text).'</pre>';
   }
 
+  // set error handling to catch all errors thrown by Text_Wiki
+  if (! class_exists('PEAR_Error')) {
+      include_once 'PEAR.php';
+  }
+  PEAR::setErrorHandling (PEAR_ERROR_CALLBACK, 'pearwiki_pear_error_callback');
+
   $formatname = pearwiki_filter_syntax($format);
   $wiki =& Text_Wiki::singleton($formatname);
 
@@ -549,14 +555,19 @@ function pearwiki_filter_page_path($page
     $path = 'wiki/' . urlencode($page);
   }
   else {
-    // Try to find the node and link to it directly.
-    $node = db_fetch_object(db_query("SELECT nid FROM {node} WHERE LOWER(title)=LOWER('%s')", $page));
+    // Try to find the node and link to it directly. 
+    // At this time spaces in $page are replaced by $space_replacement. For this query replace spaces in the node title on-the-fly too.
+    // Note: If PEAR Wiki Filter Format is set to 'Mediawiki' $space_replacement is always an underscore.
+    $space_replacement = (pearwiki_filter_syntax($pearwiki_current_format) == 'Mediawiki') ? '_' : $space_replacement;
+    $node = db_fetch_object(db_query("SELECT nid FROM {node} WHERE LOWER(REPLACE(title, ' ', '%s'))=LOWER('%s')", $space_replacement, $page));
     if ($node) {
-      $path = 'node/'.$node->nid;
+      // Get the URL alias for this node if any
+      $path = drupal_get_path_alias('node/' . $node->nid);
     }
     else {
       // The page was not found.
-      $path = pearwiki_filter_wikilink_base($pearwiki_current_format) . urlencode($page);
+      // urlencode() changes slashes to %2F which results in invalide paths. Rechange %2F to slashes using str_replace()
+      $path = pearwiki_filter_wikilink_base($pearwiki_current_format) . str_replace("%2F", "/", urlencode($page));
     }
   }
   return (variable_get('clean_url', 0) ? '' : '?q=') . $path;
@@ -739,3 +750,24 @@ function pearwiki_filter_default_help($o
 function pearwiki_filter_default_config(&$wiki) {
 }
 
+
+/**
+ * Callback function used as PEAR_Error callback to display Text_Wiki errors to the user
+ *
+ * @param $pear_error PEAR_Error object
+ * @return void
+ * @author Bernhard Fürst <bernhard.fuerst@fuerstnet.de>
+ **/
+function pearwiki_pear_error_callback($pear_error) {
+  // general message
+  $message = t('Error when rendering the document (it may not be displayed correctly). Please check the syntax of your body text. There may be unclosed Wiki tags or other Wiki markup problems.');
+  
+  // check if $pear_error is an object and contains a message: append it to the general message
+  // That way we avoid using PEAR::isError to check for PEAR error object
+  if (is_object($pear_error) AND isset($pear_error->message)) {
+    $message .= '<br /><small>' . t('(Internal Text_Wiki error is: !pear_message)', array('!pear_message' => $pear_error->message)) . '</small>';
+  }
+  
+  // Show the error on the web page
+  drupal_set_message($message, 'error');
+}
\ No newline at end of file
