Skip to content
Snippets Groups Projects
Commit a08eb37a authored by andi's avatar andi
Browse files

spellchecker: compensate for aspell error and blocking problems

Sometimes Aspell seems to output not enough blank lines to signal
a line change. This patch tries to compensate for this by trying
the next two lines as well.
This patch also fixes a problem were reading from aspell could
produce a deadlock on the socket and would cause the spellchecker
to never return

darcs-hash:20050611121059-9977f-c772aa52ea0e73e7b7e5537afda047fa44a22395.gz
parent ea2eed85
No related branches found
No related tags found
No related merge requests found
......@@ -178,11 +178,14 @@ class Aspell{
foreach($data as $line){
fwrite($pipes[0],"^$line\n"); // aspell uses ^ to escape the line
fflush($pipes[0]);
$line = '';
do{
$r = fgets($pipes[1],8192);
$out .= $r;
$r = fread($pipes[1],1);
$line .= $r;
if(feof($pipes[1])) break;
}while($r != "\n");
if($r == "\n") break;
}while($line != "\n");
$out .= $line;
}
fclose($pipes[0]);
......
......@@ -66,7 +66,18 @@ if(function_exists($call)){
print "The called function does not exist!";
}
/**
* Spellchecker. Called by an AJAX request
*
* Runs the given Text through Aspell and prints XHTML with
* markup. The first char represents the error code:
*
* 0 - No spelling mistakes
* 1 - Spelling mistakes found
* 2 - An error occured error message follows
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function spell_check() {
global $spell;
$string = $_POST['data'];
......@@ -105,39 +116,56 @@ function spell_check() {
if($line[0] == '@') continue; // comment
if($line[0] == '*') continue; // no mistake in this word
if($line[0] == '+') continue; // root of word was found
if($line[0] == '?') continue; // word was guessed
if(empty($line)){
// empty line -> new source line
$lcnt--;
continue;
}
if(preg_match('/^[&\?] ([^ ]+) (\d+) (\d+): (.*)/',$line,$match)){
// now get the misspelled words
if(preg_match('/^& ([^ ]+) (\d+) (\d+): (.*)/',$line,$match)){
// match with suggestions
$word = $match[1];
$off = $match[3]-1;
$sug = split(', ',$match[4]);
$len = utf8_strlen($word);
$misspell = true;
$data[$lcnt] = utf8_substr_replace($data[$lcnt], spell_formatword($word,$sug) , $off, $len);
continue;
}
if(preg_match('/^# ([^ ]+) (\d+)/',$line,$match)){
}elseif(preg_match('/^# ([^ ]+) (\d+)/',$line,$match)){
// match without suggestions
$word = $match[1];
$off = $match[2]-1;
$len = utf8_strlen($word);
$misspell = true;
$sug = null;
}else{
// couldn't parse output
print '2';
print "The spellchecker output couldn't be parsed.\n";
print "Line $i:".$line;
return;
}
$data[$lcnt] = utf8_substr_replace($data[$lcnt], spell_formatword($word) , $off, $len);
continue;
$misspell = true;
$len = utf8_strlen($word);
// try to insert markup
// Aspell sometimes returns too few blank lines, the following loop
// tries to compensate by skipping to next line if Aspell's output
// doesn't match - we're skipping maximal 2 lines before giving up and
// throwing an error
for($x=0; $x<3; $x++){
$lcnt -= $x;
if(utf8_substr($data[$lcnt],$off,$len) == $word){
$data[$lcnt] = utf8_substr_replace($data[$lcnt],
spell_formatword($word,$sug),
$off, $len);
break;
}elseif($x == 2){
print '2';
print "The spellchecker output doesn't match the input data.\n";
print "Offending word: '$word' offset: $off, line $i";
return;
}
}
//still here - couldn't parse output
print '2';
print "The spellchecker output couldn't be parsed.\n";
print "Line $i:".$line;
return;
}
}//end of output parsing
// the first char returns the spell info
if($misspell){
......@@ -153,6 +181,11 @@ function spell_check() {
print $string;
}
/**
* Formats a word with needed markup for the Suggestion Popup
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function spell_formatword($word,$suggestions=null){
static $i = 1;
......@@ -177,6 +210,11 @@ function spell_formatword($word,$suggestions=null){
return $link;
}
/**
* Rewrite markuped XHTML back to plain Text. AJAX callback
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function spell_resume(){
$text = $_POST['data'];
......@@ -200,14 +238,6 @@ function spell_resume(){
print $text;
}
function spell_suggest(){
$id = $_POST['id'];
$word = $_POST['word'];
print $id."\n".$word;
}
/**
* Reverse htmlspecialchars
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment