Forked from
Stud.IP / Stud.IP
3429 commits behind the upstream repository.
-
Moritz Strohm authored
Merge request studip/studip!642
Moritz Strohm authoredMerge request studip/studip!642
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
SmileyFormat.php 1.43 KiB
<?php
/**
* SmileyFormat.php
*
* Provides a formatting object for smileys.
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @category Stud.IP
* @package smiley
* @since 2.3
* @uses Smiley
*/
class SmileyFormat extends TextFormat
{
const REGEXP = '(\>|^|\s):([_a-zA-Z][_a-z0-9A-Z-]*):(?=$|\<|\s)';
function __construct()
{
$rules = [];
// Smiley rule
$rules['smileys'] = [
'start' => self::REGEXP,
'callback' => 'SmileyFormat::smiley'
];
// Smiley short notation rule
$needles = array_keys(Smiley::getShort());
$needles = array_map('preg_quote', $needles);
$rules['smileys_short'] = [
'start' => '(>|^|\s)(' . implode('|', $needles) . ')(?=$|<|\s)',
'callback' => 'SmileyFormat::short'
];
parent::__construct($rules);
}
/**
* Smiley notation defined by name (:name:)
*/
static function smiley($markup, $matches)
{
return $matches[1] . Smiley::getByName($matches[2])->getImageTag() . $matches[3];
}
/**
* Smiley short notation as defined in database
*/
static function short($markup, $matches)
{
$smileys = Smiley::getShort();
$name = $smileys[$matches[2]] ?? '';
return $name
? $matches[1] . Smiley::getByName($name)->getImageTag() . $matches[3]
: $matches[0];
}
}