Merge branch 'diceroll-2' into 'config'

Add inline dicerolling. Fix #15

Closes #15

See merge request leftypol/leftypol!2
This commit is contained in:
Zankaria Auxa 2024-10-13 22:32:30 +00:00
commit a86c9f8d8f
4 changed files with 65 additions and 5 deletions

View file

@ -681,6 +681,9 @@
// with the modifier Z added, with the result displayed at the top of the post body.
$config['allow_roll'] = false;
// Maximum number of dice rolls per post.
$config['max_roll_count'] = 24;
// Use semantic URLs for threads, like /b/res/12345/daily-programming-thread.html
$config['slugify'] = false;
@ -740,11 +743,16 @@
* ====================
*/
// "Wiki" markup syntax ($config['wiki_markup'] in pervious versions):
$config['markup'][] = array("/'''(.+?)'''/", "<strong>\$1</strong>");
$config['markup'][] = array("/''(.+?)''/", "<em>\$1</em>");
$config['markup'][] = array("/\*\*(.+?)\*\*/", "<span class=\"spoiler\">\$1</span>");
$config['markup'][] = array("/==(.+?)==/", "<span class=\"heading\">\$1</span>");
$config['markup'] = [
// Dice Roll Markup.
[ "/!([-+]?\d+)?([d])([-+]?\d+)([-+]\d+)?/iu", fn($m) => handle_dice_roll_markup($m, 'static/d10.svg') ],
// "Wiki" markup syntax ($config['wiki_markup'] in pervious versions):
[ "/'''(.+?)'''/", "<strong>\$1</strong>" ],
[ "/''(.+?)''/", "<em>\$1</em>" ],
[ "/\*\*(.+?)\*\*/", "<span class=\"spoiler\">\$1</span>" ],
[ "/==(.+?)==/", "<span class=\"heading\">\$1</span>" ]
];
// Code markup. This should be set to a regular expression, using tags you want to use. Examples:
// "/\[code\](.*?)\[\/code\]/is"

View file

@ -2786,6 +2786,51 @@ function diceRoller($post) {
}
}
/**
* Rolls a dice and generates the appropiate html from the markup.
* @param array $matches The array of the matches according to the default configuration.
* 1 -> The number of dices to roll.
* 3 -> The number faces of the dices.
* 4 -> The offset to apply to the dice.
* @param string $img_path Path to the image to use. Null if none.
* @return string The html to replace the original markup with.
*/
function handle_dice_roll_markup(array $matches, ?string $img_path): string {
function get_or_default(array $arr, int $index, int $default) {
return (isset($arr[$index]) && is_numeric($arr[$index])) ? (int)$arr[$index] : $default;
}
global $config;
$dice_count = get_or_default($matches, 1, 1);
$dice_faces = get_or_default($matches, 3, 6);
$dice_offset = get_or_default($matches, 4, 0);
// Clamp between 1 and max_roll_count.
$dice_count = max(min($dice_count, $config['max_roll_count']), 1);
// Must be at least 2.
if ($dice_faces < 2) {
$dice_faces = 6;
}
$acc = '';
$text = "$dice_count d $dice_faces + $dice_offset";
for ($i = 0; $i < $dice_count; $i++) {
$res = $dice_offset + mt_rand(0, $dice_faces);
if ($img_path === null) {
$fmt = "<span>$text </span> <b>$res</b>";
} else {
$fmt = "<img src='$img_path' alt='$text' title='$text' class=\"inline-dice\"/> <b>$res</b>";
}
if (empty($acc)) {
$acc = $fmt;
} else {
$acc .= " $fmt";
}
}
return $acc;
}
function slugify($post) {
global $config;

View file

@ -1487,6 +1487,12 @@ li.mix {
display: inline-block;
}
/* Dice roll support */
.inline-dice {
width: 1em;
height: 1em;
}
/* Mona Font */
.aa {
font-family: Mona, "MS PGothic", " Pゴシック", sans-serif;

View file

@ -77,6 +77,7 @@
<p> ==text== makes it a heading: <span class="heading">text</span></p>
<p> **text** makes it spoiler: <span class="spoiler">text</span></p>
<p> [code]text[/code] makes it code: <pre class="code lang-">text</pre></p>
<p> [dice:XdY+Z] rolls a X dices with Y faces, adding Z to the result: <img src="static/d10.svg" alt="1 d 2 + 0" title="1 d 2 + 0" class="inline-dice"></p>
<h2 id="7">
Why are my posts being changed / word-filtered ?
</h2>