]> git.mjollnir.org Git - moodle.git/commitdiff
Added dreadful spell checker option
authorthepurpleblob <thepurpleblob>
Wed, 18 Jun 2003 14:33:51 +0000 (14:33 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 18 Jun 2003 14:33:51 +0000 (14:33 +0000)
Fixed bug where formatting at end of line failed
Removed substitution for single quotes bug (ended up as double)

lib/wiki.php

index 5a0661d7127de8818b2b0a8172cb10940515b0bd..4345b595c043f80c883e1f8f51b9607568cedf85 100644 (file)
@@ -51,6 +51,7 @@ class Wiki {
   var $block_state;\r
   var $list_state;\r
   var $list_depth;\r
+  var $spelling_on;\r
   var $list_backtrack;\r
   var $output; // output buffer\r
 \r
@@ -92,7 +93,7 @@ class Wiki {
     $bodge = chr(1);\r
     $line = eregi_replace( '([[:alnum:]])'.$mark.'([[:alnum:]])', '\\1'.$bodge.'\\2',$line );\r
 \r
-    $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]])';\r
+    $regex = '(^| |[(.,])'.$mark.'([^'.$mark.']*)'.$mark.'([^[:alnum:]]|$)';\r
     $replace = '\\1<'.$tag.'>\\2</'.$tag.'>\\3';\r
     $line = eregi_replace( $regex, $replace, $line );\r
 \r
@@ -210,7 +211,7 @@ class Wiki {
     $line = str_replace( "(TM)", "&#8482;", $line );\r
     $line = str_replace( "(tm)", "&#8482;", $line );\r
     $line = str_replace( "(C)", "&#169;", $line );\r
-    $line = str_replace( "(c)", "&#169;", $line );\r
+    // $line = str_replace( "(c)", "&#169;", $line );\r
     $line = str_replace( "1/4", "&#188;", $line );\r
     $line = str_replace( "1/2", "&#189;", $line );\r
     $line = str_replace( "3/4", "&#190;", $line );\r
@@ -227,7 +228,7 @@ class Wiki {
     $line = $this->do_replace_sub( $line, "~", "sub" );\r
     $line = $this->do_replace_sub( $line, "\^", "sup" );\r
     $line = $this->do_replace( $line, "\"", "q" );\r
-    $line = $this->do_replace( $line, "'", "q" );\r
+    // $line = $this->do_replace( $line, "'", "q" );\r
     $line = $this->do_replace( $line, "%", "code" );\r
     $line = $this->do_replace( $line, "@", "cite" );\r
    \r
@@ -273,6 +274,37 @@ class Wiki {
     return $line;\r
   }\r
 \r
+\r
+  function spellcheck( $line,$pspell_link ) {\r
+\r
+    // split line into words\r
+    $words = preg_split( "/[\s,-.]/ ", $line );\r
+\r
+    // run through words\r
+    $newline = "";\r
+    foreach($words as $word) {\r
+      $check_word = eregi_replace( "[,;:./&()* ?\"]", "", $word );\r
+      $check_word = eregi_replace( "^'|'$","",$check_word );\r
+\r
+      // words not to check\r
+      $docheck = true;\r
+      if (eregi("[0-9]",$check_word)) { $docheck=false; }\r
+\r
+      if ( $docheck && (!pspell_check( $pspell_link, $check_word)) ) {\r
+        $suggests = pspell_suggest( $pspell_link,$check_word );\r
+        $suggest_line = "";\r
+        foreach($suggests as $suggest) {\r
+          $suggest_line = $suggest_line . " " . $suggest;\r
+        }\r
+        $word = "<span class=\"spellcheck\"><acronym title=\"$suggest_line\">$word</acronym></span>";\r
+      }\r
+      $newline = $newline . " " . $word;\r
+    }\r
+\r
+    return $newline;\r
+  }\r
+\r
+\r
   function format( $content ) {\r
     // main entry point for processing TikiText\r
     // $content is string containing text with Tiki formatting\r
@@ -284,6 +316,7 @@ class Wiki {
     $this->list_state = LIST_NONE;\r
     $this->list_depth = 0;\r
     $this->list_backtrack = array();\r
+    $this->spelling_on = false;\r
 \r
     // split content into array of single lines\r
     $lines = explode( "\n",$content );\r
@@ -303,6 +336,20 @@ class Wiki {
         $this->block_state = STATE_NONE;\r
         continue;\r
       }\r
+\r
+      // is this a spelling line\r
+      $spell_parms = array();\r
+      $spelling = eregi( "^!SPELL:([a-z]+):?(american|british|canadian)?(\r| |$)", $line,$spell_parms );\r
+      if ($spelling) {\r
+        $this->spelling_on = true;\r
+        $pspell_link = pspell_new( $spell_parms[1], $spell_parms[2] );\r
+        $line = "";\r
+      }\r
+\r
+      // spellcheck\r
+      if ($this->spelling_on) {\r
+        $line = $this->spellcheck( $line, $pspell_link );\r
+      }\r
       \r
       // act now depending on current block state\r
       if ($this->block_state == STATE_NONE) {\r
@@ -328,14 +375,14 @@ class Wiki {
                $this->block_state = STATE_NOTIKI;\r
         }      \r
         else\r
-        if (eregi("^Q. ",$line) ) {\r
+        if (eregi("^Q\. ",$line) ) {\r
           // Question - para with a question class\r
           $buffer = $buffer . "<p class=\"question\">\n";\r
           $buffer = $buffer . eregi_replace( "^Q. ","",$line) . "\n";\r
           $this->block_state = STATE_PARAGRAPH;\r
         }\r
         else\r
-        if (eregi("^A. ",$line) ) {\r
+        if (eregi("^A\. ",$line) ) {\r
           // Answer - para with an answer class\r
           $buffer = $buffer . "<p class=\"answer\">\n";\r
           $buffer = $buffer . eregi_replace( "^A. ","",$line ) . "\n";\r