예제 #1
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     if ($this->config->getOption('strip_tags', false)) {
         return $element->getValue() . PHP_EOL . PHP_EOL;
     }
     return html_entity_decode($element->getChildrenAsString());
 }
예제 #2
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $language = null;
     // Checking for language class on the code block
     $classes = $element->getAttribute('class');
     if ($classes) {
         // Since tags can have more than one class, we need to find the one that starts with 'language-'
         $classes = explode(' ', $classes);
         foreach ($classes as $class) {
             if (strpos($class, 'language-') !== false) {
                 // Found one, save it as the selected language and stop looping over the classes.
                 // The space after the language avoids gluing the actual code with the language tag
                 $language = str_replace('language-', '', $class) . ' ';
                 break;
             }
         }
     }
     $markdown = '';
     $code = html_entity_decode($element->getChildrenAsString());
     // In order to remove the code tags we need to search for them and, in the case of the opening tag
     // use a regular expression to find the tag and the other attributes it might have
     $code = preg_replace('/<code\\b[^>]*>/', '', $code);
     $code = str_replace('</code>', '', $code);
     // Checking if the code has multiple lines
     $lines = preg_split('/\\r\\n|\\r|\\n/', $code);
     if (count($lines) > 1) {
         // Multiple lines detected, adding three backticks and newlines
         $markdown .= '```' . $language . "\n" . $code . "\n" . '```';
     } else {
         // One line of code, wrapping it on one backtick.
         $markdown .= '`' . $language . $code . '`';
     }
     return $markdown;
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $markdown = '';
     $pre_content = html_entity_decode($element->getChildrenAsString());
     $pre_content = str_replace(array('<pre>', '</pre>'), '', $pre_content);
     /*
      * Checking for the code tag.
      * Usually pre tags are used along with code tags. This conditional will check for already converted code tags,
      * which use backticks, and if those backticks are at the beginning and at the end of the string it means
      * there's no more information to convert.
      */
     $firstBacktick = strpos(trim($pre_content), '`');
     $lastBacktick = strrpos(trim($pre_content), '`');
     if ($firstBacktick === 0 && $lastBacktick === strlen(trim($pre_content)) - 1) {
         return $pre_content;
     }
     // If the execution reaches this point it means it's just a pre tag, with no code tag nested
     // Normalizing new lines
     $pre_content = preg_replace('/\\r\\n|\\r|\\n/', PHP_EOL, $pre_content);
     // Checking if the string has multiple lines
     $lines = preg_split('/\\r\\n|\\r|\\n/', $pre_content);
     if (count($lines) > 1) {
         // Multiple lines detected, adding three backticks and newlines
         $markdown .= '```' . "\n" . $pre_content . "\n" . '```';
     } else {
         // One line of code, wrapping it on one backtick.
         $markdown .= '`' . $pre_content . '`';
     }
     return $markdown;
 }
예제 #4
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $level = (int) substr($element->getTagName(), 1, 1);
     $style = $this->config->getOption('header_style', self::STYLE_SETEXT);
     if (($level === 1 || $level === 2) && !$element->isDescendantOf('blockquote') && $style === self::STYLE_SETEXT) {
         return $this->createSetextHeader($level, $element->getValue());
     } else {
         return $this->createAtxHeader($level, $element->getValue());
     }
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     // If strip_tags is false (the default), preserve tags that don't have Markdown equivalents,
     // such as <span> nodes on their own. C14N() canonicalizes the node to a string.
     // See: http://www.php.net/manual/en/domnode.c14n.php
     if ($this->config->getOption('strip_tags', false)) {
         return $element->getValue();
     }
     return html_entity_decode($element->getChildrenAsString());
 }
예제 #6
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $tag = $element->getTagName();
     $value = $element->getValue();
     if ($tag === 'i' || $tag === 'em') {
         $style = $this->config->getOption('italic_style');
     } else {
         $style = $this->config->getOption('bold_style');
     }
     return $style . $value . $style;
 }
예제 #7
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $tag = $element->getTagName();
     $value = $element->getValue();
     if ($tag === 'i' || $tag === 'em') {
         $style = $this->config->getOption('italic_style');
     } else {
         $style = $this->config->getOption('bold_style');
     }
     $prefix = ltrim($value) !== $value ? ' ' : '';
     $suffix = rtrim($value) !== $value ? ' ' : '';
     return $prefix . $style . trim($value) . $style . $suffix;
 }
예제 #8
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $value = $element->getValue();
     $markdown = preg_replace('~\\s+~', ' ', $value);
     $markdown = preg_replace('~^#~', '\\\\#', $markdown);
     if ($markdown === ' ') {
         $next = $element->getNext();
         if (!$next || $next->isBlock()) {
             $markdown = '';
         }
     }
     return $markdown;
 }
예제 #9
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $src = $element->getAttribute('src');
     $alt = $element->getAttribute('alt');
     $title = $element->getAttribute('title');
     if ($title !== '') {
         // No newlines added. <img> should be in a block-level element.
         $markdown = '![' . $alt . '](' . $src . ' "' . $title . '")';
     } else {
         $markdown = '![' . $alt . '](' . $src . ')';
     }
     return $markdown;
 }
예제 #10
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     // If parent is an ol, use numbers, otherwise, use dashes
     $list_type = $element->getParent()->getTagName();
     $value = $element->getValue();
     if ($list_type === 'ul') {
         $markdown = '- ' . trim($value) . "\n";
     } else {
         $number = $element->getSiblingPosition();
         $markdown = $number . '. ' . trim($value) . "\n";
     }
     return $markdown;
 }
예제 #11
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $value = $element->getValue();
     $markdown = preg_replace('~\\s+~', ' ', $value);
     //escape the following characters: '*', '_' and '\'
     $markdown = preg_replace('~([*_\\\\])~', '\\\\$1', $markdown);
     $markdown = preg_replace('~^#~', '\\\\#', $markdown);
     if ($markdown === ' ') {
         $next = $element->getNext();
         if (!$next || $next->isBlock()) {
             $markdown = '';
         }
     }
     return $markdown;
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $value = $element->getValue();
     $markdown = '';
     $lines = preg_split('/\\r\\n|\\r|\\n/', $value);
     foreach ($lines as $line) {
         /*
          * Some special characters need to be escaped based on the position that they appear
          * The following function will deal with those special cases.
          */
         $markdown .= $this->escapeSpecialCharacters($line);
         $markdown .= "\n";
     }
     return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
 }
예제 #13
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     // Contents should have already been converted to Markdown by this point,
     // so we just need to add '>' symbols to each line.
     $markdown = '';
     $quote_content = trim($element->getValue());
     $lines = preg_split('/\\r\\n|\\r|\\n/', $quote_content);
     $total_lines = count($lines);
     foreach ($lines as $i => $line) {
         $markdown .= '> ' . $line . "\n";
         if ($i + 1 === $total_lines) {
             $markdown .= "\n";
         }
     }
     return $markdown;
 }
예제 #14
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $href = $element->getAttribute('href');
     $title = $element->getAttribute('title');
     $text = $element->getValue();
     if ($title != '') {
         $markdown = '[' . $text . '](' . $href . ' "' . $title . '")';
     } elseif ($href === $text) {
         $markdown = '<' . $href . '>';
     } else {
         $markdown = '[' . $text . '](' . $href . ')';
     }
     if (!$href) {
         $markdown = html_entity_decode($element->getChildrenAsString());
     }
     return $markdown;
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     // Store the content of the code block in an array, one entry for each line
     $markdown = '';
     $code_content = html_entity_decode($element->getChildrenAsString());
     $code_content = str_replace(array('<code>', '</code>'), '', $code_content);
     $code_content = str_replace(array('<pre>', '</pre>'), '', $code_content);
     $lines = preg_split('/\\r\\n|\\r|\\n/', $code_content);
     $total = count($lines);
     // If there's more than one line of code, prepend each line with four spaces and no backticks.
     if ($total > 1 || $element->getTagName() === 'pre') {
         // Remove the first and last line if they're empty
         $first_line = trim($lines[0]);
         $last_line = trim($lines[$total - 1]);
         $first_line = trim($first_line, '&#xD;');
         //trim XML style carriage returns too
         $last_line = trim($last_line, '&#xD;');
         if (empty($first_line)) {
             array_shift($lines);
         }
         if (empty($last_line)) {
             array_pop($lines);
         }
         $count = 1;
         foreach ($lines as $line) {
             $line = str_replace('&#xD;', '', $line);
             $markdown .= '    ' . $line;
             // Add newlines, except final line of the code
             if ($count !== $total) {
                 $markdown .= "\n";
             }
             $count++;
         }
         $markdown .= "\n";
     } else {
         // There's only one line of code. It's a code span, not a block. Just wrap it with backticks.
         $markdown .= '`' . $lines[0] . '`';
     }
     if ($element->getTagName() === 'pre') {
         $markdown = "\n" . $markdown . "\n";
     }
     return $markdown;
 }
예제 #16
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     // If parent is an ol, use numbers, otherwise, use dashes
     $list_type = $element->getParent()->getTagName();
     $value = $element->getValue();
     // Add spaces to start for nested list items
     $level = $element->getListItemLevel($element);
     $prefix = str_repeat('  ', $level);
     // If list item is the first in a nested list, add a newline before it
     if ($level > 0 && $element->getSiblingPosition() === 1) {
         $prefix = "\n" . $prefix;
     }
     if ($list_type === 'ul') {
         $markdown = $prefix . '- ' . trim($value) . "\n";
     } else {
         $number = $element->getSiblingPosition();
         $markdown = $prefix . $number . '. ' . trim($value) . "\n";
     }
     return $markdown;
 }
예제 #17
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $value = $element->getValue();
     return trim($value) ? rtrim($value) . PHP_EOL . PHP_EOL : '';
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     return $element->getValue() . "\n";
 }
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 public function convert(ElementInterface $element)
 {
     $value = $element->getValue();
     return trim($value) !== '' ? rtrim($value) . "\n\n" : '';
 }