allow markdown in timesheet descriptions (#296)

This commit is contained in:
Kevin Papst
2018-09-02 12:10:49 +02:00
committed by GitHub
parent 9f6079ad8a
commit 4c277fe1b5
13 changed files with 77 additions and 8 deletions

View File

@@ -75,6 +75,7 @@ class AppExtension extends Extension implements PrependExtensionInterface
$container->setParameter('kimai.timesheet.rates', $config['timesheet']['rates']);
$container->setParameter('kimai.timesheet.rounding', $config['timesheet']['rounding']);
$container->setParameter('kimai.timesheet.duration_only', $config['timesheet']['duration_only']);
$container->setParameter('kimai.timesheet.markdown', $config['timesheet']['markdown_content']);
}
/**

View File

@@ -55,6 +55,9 @@ class Configuration implements ConfigurationInterface
->booleanNode('duration_only')
->defaultValue(false)
->end()
->booleanNode('markdown_content')
->defaultValue(false)
->end()
->arrayNode('rounding')
->requiresAtLeastOneElement()
->useAttributeAsKey('key')

View File

@@ -21,14 +21,19 @@ class MarkdownExtension extends \Twig_Extension
* @var Markdown
*/
private $markdown;
/**
* @var bool
*/
private $timesheetIsMarkdown = false;
/**
* MarkdownExtension constructor.
* @param Markdown $parser
*/
public function __construct(Markdown $parser)
public function __construct(Markdown $parser, bool $timesheetAsMarkdown = false)
{
$this->markdown = $parser;
$this->timesheetIsMarkdown = $timesheetAsMarkdown;
}
/**
@@ -38,9 +43,25 @@ class MarkdownExtension extends \Twig_Extension
{
return [
new TwigFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
new TwigFilter('desc2html', [$this, 'timesheetContent'], ['is_safe' => ['html']]),
];
}
/**
* Transforms the timesheet description content into HTML.
*
* @param string $content
* @return string
*/
public function timesheetContent(string $content): string
{
if ($this->timesheetIsMarkdown) {
return $this->markdown->toHtml($content, false);
}
return nl2br($content);
}
/**
* Transforms the given Markdown content into HTML
*
@@ -49,6 +70,6 @@ class MarkdownExtension extends \Twig_Extension
*/
public function markdownToHtml(string $content): string
{
return $this->markdown->toHtml($content);
return $this->markdown->toHtml($content, true);
}
}

View File

@@ -31,10 +31,12 @@ class Markdown
/**
* @param string $text
* @param bool $safe
* @return string
*/
public function toHtml(string $text): string
public function toHtml(string $text, bool $safe = true): string
{
$this->parser->setSafeMode($safe);
return $this->parser->text($text);
}
}