protected function prepareText(\DOMDocument $doc)
    {
        $text = $this->convertHtmlToText($doc->childNodes);
        $charset = $this->getCharset();
        $textLineMaxLength = $this->getOption(self::OPTION_TEXT_LINE_WIDTH);

        $text = preg_replace_callback('/^([^\n]+)$/m', function($match) use ($charset, $textLineMaxLength) {
                $break = "\n";
                $parts = preg_split('/((?:\(\t[^\t]+\t\))|[^\p{L}\p{N}])/', $match[0], -1, PREG_SPLIT_DELIM_CAPTURE);

                $return = "";
                $brLength = mb_strlen(trim($break, "\r\n"), $charset);

                $lineLength = $brLength;
                foreach ($parts as $part) {
                    // Replace character before/after links with a zero width space,
                    // and mark links as non-breakable
                    $breakLongLines = true;
                    if (strpos($part, "\t")) {
                        $part = str_replace("\t", mb_convert_encoding("\xE2\x80\x8C", $charset, "UTF-8"), $part);
                        $breakLongLines = false;
                    }

                    // Get part length
                    $partLength = mb_strlen($part, $charset);

                    // Ignore trailing space characters if this would cause the line break
                    if (($lineLength + $partLength) === ($textLineMaxLength + 1)) {
                        $lastChar = mb_substr($part, -1, 1, $charset);
                        if ($lastChar === " ") {
                            $part = mb_substr($part, 0, -1, $charset);
                            $partLength--;
                        }
                    }

                    // Check if enough chars left to add the part
                    if (($lineLength + $partLength) <= $textLineMaxLength) {
                        $return .= $part;
                        $lineLength += $partLength;
                    // Check if the part is longer than the line (so that we need to break it)
                    } elseif ($partLength > ($textLineMaxLength - $brLength)) {
                        if ($breakLongLines === true) {
                            $addPart = mb_substr($part, 0, ($textLineMaxLength - $lineLength), $charset);
                            $return .= $addPart;
                            $lineLength = $brLength;

                            for ($i = mb_strlen($addPart, $charset), $j = $partLength; $i < $j; $i+=($textLineMaxLength - $brLength)) {
                                $addPart = $break . mb_substr($part, $i, ($textLineMaxLength - $brLength), $charset);
                                $return .= $addPart;
                                $lineLength = mb_strlen($addPart, $charset) - 1;
                            }
                        } else {
                            $return .= $break . trim($part) . $break;
                            $lineLength = $brLength;
                        }
                    // Add a break to add the part in the next line
                    } else {
                        $return .= $break . rtrim($part);
                        $lineLength = $brLength + $partLength;
                    }
                }
                return $return;
            }, $text);

        $text = preg_replace('/^\s+|\s+$/', '', $text);

        return $text;
    }