3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
\r
6 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
\r
9 class PSpellShell extends SpellChecker {
\r
11 * Spellchecks an array of words.
\r
13 * @param {String} $lang Language code like sv or en.
\r
14 * @param {Array} $words Array of words to spellcheck.
\r
15 * @return {Array} Array of misspelled words.
\r
17 function &checkWords($lang, $words) {
\r
18 $cmd = $this->_getCMD($lang);
\r
20 if ($fh = fopen($this->_tmpfile, "w")) {
\r
23 foreach($words as $key => $value)
\r
24 fwrite($fh, "^" . $value . "\n");
\r
28 $this->throwError("PSpell support was not found.");
\r
30 $data = shell_exec($cmd);
\r
31 @unlink($this->_tmpfile);
\r
33 $returnData = array();
\r
34 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
\r
36 foreach ($dataArr as $dstr) {
\r
40 if (strpos($dstr, "@") === 0)
\r
43 preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
\r
45 if (!empty($matches[1]))
\r
46 $returnData[] = utf8_encode(trim($matches[1]));
\r
53 * Returns suggestions of for a specific word.
\r
55 * @param {String} $lang Language code like sv or en.
\r
56 * @param {String} $word Specific word to get suggestions for.
\r
57 * @return {Array} Array of suggestions for the specified word.
\r
59 function &getSuggestions($lang, $word) {
\r
60 $cmd = $this->_getCMD($lang);
\r
62 if (function_exists("mb_convert_encoding"))
\r
63 $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
\r
65 $word = utf8_encode($word);
\r
67 if ($fh = fopen($this->_tmpfile, "w")) {
\r
69 fwrite($fh, "^$word\n");
\r
72 $this->throwError("Error opening tmp file.");
\r
74 $data = shell_exec($cmd);
\r
75 @unlink($this->_tmpfile);
\r
77 $returnData = array();
\r
78 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
\r
80 foreach($dataArr as $dstr) {
\r
84 if (strpos($dstr, "@") === 0)
\r
87 preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
\r
89 if (!empty($matches[1])) {
\r
90 $words = array_slice(explode(',', $matches[1]), 0, 10);
\r
92 for ($i=0; $i<count($words); $i++)
\r
93 $words[$i] = trim($words[$i]);
\r
102 function _getCMD($lang) {
\r
103 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
\r
105 if(preg_match("#win#i", php_uname()))
\r
106 return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
\r
108 return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);
\r