All scripts in one repository
This commit is contained in:
parent
a0a86492ee
commit
9f9304d6aa
61 changed files with 6668 additions and 681 deletions
73
blogger2rangitaki/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php
vendored
Normal file
73
blogger2rangitaki/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php
vendored
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace League\HTMLToMarkdown\Converter;
|
||||
|
||||
use League\HTMLToMarkdown\ElementInterface;
|
||||
|
||||
class PreformattedConverter implements ConverterInterface
|
||||
{
|
||||
/**
|
||||
* @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, '
'); //trim XML style carriage returns too
|
||||
$last_line = trim($last_line, '
');
|
||||
|
||||
if (empty($first_line)) {
|
||||
array_shift($lines);
|
||||
}
|
||||
|
||||
if (empty($last_line)) {
|
||||
array_pop($lines);
|
||||
}
|
||||
|
||||
$count = 1;
|
||||
foreach ($lines as $line) {
|
||||
$line = str_replace('
', '', $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSupportedTags()
|
||||
{
|
||||
return array('pre', 'code');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue