Add class MDFormatter for formatting contents in markdown

This commit is contained in:
Joshua Ramon Enslin 2021-02-19 00:49:26 +01:00
parent 1b1d33229d
commit 376333b660
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

167
MDFormatter.php Normal file
View File

@ -0,0 +1,167 @@
<?PHP
/**
* Applies plain text formatting.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Applies plain text formatting.
*/
final class MDFormatter {
/** @var boolean */
public static bool $use_underlined_hl = true;
/**
* Applies markdown formatting for main headline.
*
* @param string $input Headline text.
*
* @return string
*/
public static function formatMarkdownHeadline1(string $input):string {
if (self::$use_underlined_hl === false) {
return '# ' . $input . PHP_EOL . PHP_EOL;
}
$hlLength = mb_strlen($input);
$output = $input . PHP_EOL;
for ($i = 0; $i < $hlLength; $i++) {
$output .= '=';
}
$output .= PHP_EOL . PHP_EOL;
return $output;
}
/**
* Applies markdown formatting for secondary headline.
*
* @param string $input Headline text.
*
* @return string
*/
public static function formatMarkdownHeadline2(string $input):string {
if (self::$use_underlined_hl === false) {
return '## ' . $input . PHP_EOL . PHP_EOL;
}
$hlLength = mb_strlen($input);
$output = $input . PHP_EOL;
for ($i = 0; $i < $hlLength; $i++) {
$output .= '-';
}
$output .= PHP_EOL . PHP_EOL;
return $output;
}
/**
* Applies markdown formatting for tertiary headline.
*
* @param string $input Headline text.
*
* @return string
*/
public static function formatMarkdownHeadline3(string $input):string {
return '### ' . $input . PHP_EOL;
}
/**
* Returns a horizontal rule in markdown.
*
* @return string
*/
public static function formatMarkdownHorizontalRule():string {
return '___' . PHP_EOL;
}
/**
* Applies markdown formatting for a named link.
*
* @param string $link_name Link text.
* @param string $link_url Target URL.
*
* @return string
*/
public static function formatMarkdownNamedLink(string $link_name, string $link_url):string {
return "[{$link_name}]({$link_url})";
}
/**
* Applies markdown formatting for a block quote.
*
* @param string $content Content of the block quote.
*
* @return string
*/
public static function formatMarkdownBlockQuote(string $content):string {
$output = '';
$lines = \explode(PHP_EOL, $content);
foreach ($lines as $line) {
$output .= '> ' . $line . PHP_EOL;
}
return $output;
}
/**
* Applies markdown formatting for a code block.
*
* @param string $content Content of the code block.
* @param string $language Progamming language for syntax highlighting. Optional.
*
* @return string
*/
public static function formatMarkdownCodeBlock(string $content, string $language = ''):string {
$output = '```' . $language . '
' . $content . '
```' . PHP_EOL;
return $output;
}
/**
* Applies markdown formatting for an unordered list item.
*
* @param string $content Content of the list item.
*
* @return string
*/
public static function formatMarkdownUnorderedListItem(string $content):string {
if (\strpos($content, PHP_EOL) === false) {
return '- ' . $content . PHP_EOL;
}
$lines = \explode(PHP_EOL, $content);
$output = '- ' . $lines[0] . PHP_EOL;
foreach ($lines as $i => $line) {
if ($i === 0) continue;
$output .= ' ' . $line . PHP_EOL;
}
return $output;
}
}