Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
spell_parser.inc
1 <?php
29 Class Spell_Parser
30 {
31 
42  function openHandler(&$parser, $elem, $attribs)
43  {
44  echo '<'.$elem.' ';
45  foreach ($attribs as $key => $val) {
46  echo $key.'="'.str_replace('"', '\\"', $val).'" ';
47  }
48  echo ' >';
49  return true;
50 
51  }//end openHandler()
52 
53 
63  function closeHandler(&$parser, $elem)
64  {
65  echo '</'.$elem.'>';
66  return true;
67 
68  }//end closeHandler()
69 
70 
79  function spellCallback($match)
80  {
81  $check_spelling = true;
82  $word = $match[1];
83 
84  // ignore numbers
85  if ($check_spelling) {
86  $converted_word = (int)$word;
87  if ((string)$converted_word == $word) $check_spelling = false;
88  }
89 
90  // ugnore words in all uppercase
91  if ($check_spelling) {
92  $converted_word = strtoupper($word);
93  if ($converted_word == $word) $check_spelling = false;
94  }
95 
96  if (!$check_spelling || pspell_check($GLOBALS['spellerid'], $word)) {
97  return htmlspecialchars($match[1]).$match[2];
98  } else {
99  $retv = '<span class="HA-spellcheck-error">'.$word.'</span>';
100  $suggestions = pspell_suggest($GLOBALS['spellerid'], $word);
101  $retv.= '<span class="HA-spellcheck-suggestions">';
102 
103  for ($i = 0; $i < count($suggestions); $i++) {
104 
105  $retv .= htmlspecialchars($suggestions[$i]);
106  if ($i != count($suggestions)) {
107  $retv .= ",";
108  }
109  }
110 
111  $retv .= '</span>'.$match[2];
112  return $retv;
113  }
114 
115  }//end spellCallback()
116 
117 
127  function dataHandler(&$parser, $data)
128  {
129  $matches = preg_match('|[^\w]|', $data);
130  if ($matches) {
131  // we add an extra character to the end (which we strip out later)
132  // to make sure the regular expression matches the last word in the line
133  $data .= '-';
134  ob_start();
135  echo preg_replace_callback('|(\w+)([^;\w])|', Array($this, 'spellCallback'), $data);
136  $contents = ob_get_contents();
137  ob_end_clean();
138  echo substr($contents, 0, -1);
139  } else {
140  echo $this->spellCallback(Array($data, $data, ''));
141  }
142  return true;
143 
144  }//end dataHandler()
145 
146 
155  function setLanguage($dict='en')
156  {
157  $pspell_config = pspell_config_create($dict);
158  pspell_config_mode($pspell_config, PSPELL_FAST);
159  $GLOBALS['spellerid'] = pspell_new_config($pspell_config);
160  return true;
161 
162  }//end setLanguage
163 
164 }//end class
165 
166 ?>