Exemplo n.º 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());
 }
 /**
  * @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;
 }
Exemplo n.º 3
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)
 {
     // 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());
 }
Exemplo n.º 5
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;
 }