Compare commits
4 Commits
8aa9d94acf
...
2c1f6a0490
Author | SHA1 | Date | |
---|---|---|---|
2c1f6a0490
|
|||
06bbaf5f97
|
|||
376333b660
|
|||
1b1d33229d
|
168
src/MDFormatter.php
Normal file
168
src/MDFormatter.php
Normal file
@ -0,0 +1,168 @@
|
||||
<?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.
|
||||
* @param string $delimiter Starting character of a line.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function formatMarkdownUnorderedListItem(string $content, string $delimiter = '-'):string {
|
||||
|
||||
if (\strpos($content, PHP_EOL) === false) {
|
||||
return $delimiter . ' ' . $content . PHP_EOL;
|
||||
}
|
||||
|
||||
$lines = \explode(PHP_EOL, $content);
|
||||
$output = $delimiter . ' ' . $lines[0] . PHP_EOL;
|
||||
|
||||
foreach ($lines as $i => $line) {
|
||||
if ($i === 0) continue;
|
||||
$output .= ' ' . $line . PHP_EOL;
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
}
|
@ -60,7 +60,7 @@ final class MD_STD_CACHE {
|
||||
|
||||
if (($redisResult = $redis->get($redisKey))) {
|
||||
|
||||
if (strlen($redisResult) > 3) {
|
||||
if (strlen($redisResult) > 3 and strpos($redisResult, ' id="errorPage"') === false) {
|
||||
$redis->close();
|
||||
return $redisResult;
|
||||
}
|
@ -5,8 +5,7 @@
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Standard class providing overrides of default PHP functions as static
|
||||
* functions.
|
||||
* Encapsulates functions for handling inputs.
|
||||
*/
|
||||
final class MD_STD_IN {
|
||||
/**
|
@ -5,7 +5,7 @@
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Class providing static functions with basic security operations.
|
||||
* Gathers wrappers for handling basic security operations.
|
||||
*/
|
||||
final class MD_STD_SEC {
|
||||
/**
|
Reference in New Issue
Block a user