Format slide texts in WhatsApp style
Slide descriptions support *bold*, _italic_, ~strikethrough~ and lists with "- " or "* " at the start of a line. FormatHelper renders them for the participant page and the PDF (escaping everything else), Format in plenario.js builds the same DOM for presenter and embed. Participant input stays plain text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3465f69bfc
commit
8d3d8d4341
12 files changed
+215
-8
No files matched your search
@@ -37,5 +37,6 @@ class AppView extends View
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
$this->addHelper('Format');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\View\Helper;
|
||||
|
||||
use Cake\View\Helper;
|
||||
|
||||
/**
|
||||
* Light formatting of slide texts in WhatsApp style: *bold*, _italic_, ~strikethrough~ and
|
||||
* lists with "- " or "* " at the start of a line. Everything else is plain, escaped text.
|
||||
* Only for texts of the poll owner (slide descriptions), never for participant input.
|
||||
* The same rules are implemented in webroot/js/plenario.js (Format) for the presentation.
|
||||
*/
|
||||
class FormatHelper extends Helper
|
||||
{
|
||||
/**
|
||||
* A marker at a word boundary, text without leading/trailing space inside, within one line.
|
||||
* Group 1: character before the marker, 2: marker, 3: formatted text.
|
||||
*/
|
||||
public const INLINE = '/(^|[^\p{L}\p{N}])([*_~])(\S|\S[^\n]*?\S)\2(?=$|[^\p{L}\p{N}])/u';
|
||||
|
||||
public const LIST_ITEM = '/^\s*[-*•]\s+(.*)$/u';
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected const TAGS = ['*' => 'strong', '_' => 'em', '~' => 's'];
|
||||
|
||||
/**
|
||||
* Formats a text as HTML: lines become <br>, consecutive list lines a <ul>.
|
||||
*
|
||||
* @param string|null $text Text as entered
|
||||
* @return string Safe HTML
|
||||
*/
|
||||
public function text(?string $text): string
|
||||
{
|
||||
$lines = preg_split('/\R/u', trim((string)$text)) ?: [];
|
||||
$html = '';
|
||||
$paragraph = [];
|
||||
$items = [];
|
||||
$flushText = function () use (&$html, &$paragraph): void {
|
||||
if ($paragraph) {
|
||||
$html .= implode('<br>', array_map($this->inline(...), $paragraph));
|
||||
$paragraph = [];
|
||||
}
|
||||
};
|
||||
$flushList = function () use (&$html, &$items): void {
|
||||
if ($items) {
|
||||
$html .= '<ul>' . implode('', array_map(fn($item) => '<li>' . $this->inline($item) . '</li>', $items)) . '</ul>';
|
||||
$items = [];
|
||||
}
|
||||
};
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match(self::LIST_ITEM, $line, $match)) {
|
||||
$flushText();
|
||||
$items[] = $match[1];
|
||||
} else {
|
||||
$flushList();
|
||||
$paragraph[] = $line;
|
||||
}
|
||||
}
|
||||
$flushText();
|
||||
$flushList();
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inline markers of one line, nested by recursion (e.g. *_bold italic_*).
|
||||
*
|
||||
* @param string $text Line as entered
|
||||
* @return string Safe HTML
|
||||
*/
|
||||
public function inline(string $text): string
|
||||
{
|
||||
if (!preg_match(self::INLINE, $text, $match, PREG_OFFSET_CAPTURE)) {
|
||||
return h($text);
|
||||
}
|
||||
$start = $match[0][1];
|
||||
$tag = self::TAGS[$match[2][0]];
|
||||
|
||||
return h(substr($text, 0, $start)) . h($match[1][0])
|
||||
. '<' . $tag . '>' . $this->inline($match[3][0]) . '</' . $tag . '>'
|
||||
. $this->inline(substr($text, $start + strlen($match[0][0])));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user