/** * Splits sentences at '.', '?' and '!', only when a dot is not one, two or three positions to the * left or to the right of the character. */ public function addMarkingsToText(InlineEditorText &$inlineEditorText, $class, $block, $bar) { foreach ($this->wikiTexts as $wikiText) { $sentences = preg_split("/(?<!\\..|\\...|\\....)([\\?\\!\\.]+)\\s(?!.\\.|..\\.|...\\.)/u", $wikiText['text'], -1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE); foreach ($sentences as $index => $sentence) { if ($index % 2 == 0) { if (isset($sentences[$index + 1])) { $sentence[0] .= $sentences[$index + 1][0]; } $start = $wikiText['offset'] + $sentence[1]; $end = $start + strlen($sentence[0]); $inlineEditorText->addMarking(new InlineEditorMarking($start, $end, $class, $block, $bar)); } } } }
/** * Pass JSON into an InlineEditorText object and return combined JSON (HTML + sentence representation) * @param $json string * @return string */ public function preview($json) { // decode the JSON $request = FormatJson::decode($json, true); // load the JSON to a text object and perform the edit $text = InlineEditorText::restoreObject($request, $this->article); $text->doEdit($request['lastEdit']['id'], $request['lastEdit']['text']); // get the next state $subseq = InlineEditorText::subsequentState($text); // send back the JSON return FormatJson::encode($subseq); }
/** * Subsequent state usable for the editor or API. * @param $text InlineEditorText * @return array Array containing the serialized object, an array of texts, and * an array describing what html should be replaced */ public static function subsequentState(InlineEditorText $text) { return array('texts' => $text->getTexts(), 'partialHtml' => $text->getPartialParserOutput(), 'object' => self::toSession($text)); }