From 16b70202bdce33a1e28a1ba9ac196f679e37a893 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Sat, 12 Oct 2024 16:16:37 +0200
Subject: [PATCH 1/6] style.css: add diceroll styling
---
stylesheets/style.css | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/stylesheets/style.css b/stylesheets/style.css
index b96755e8..5d8cb2d4 100644
--- a/stylesheets/style.css
+++ b/stylesheets/style.css
@@ -1486,6 +1486,12 @@ li.mix {
display: inline-block;
}
+/* Dice roll support */
+.inline-dice {
+ width: 1em;
+ height: 1em;
+}
+
/* Mona Font */
.aa {
font-family: Mona, "MS PGothic", "MS Pゴシック", sans-serif;
From dcede2e175de3b2ef4dbc4bf8f980eba11065785 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Sat, 12 Oct 2024 16:20:01 +0200
Subject: [PATCH 2/6] config.php: limit the number of dicerolls
---
inc/config.php | 3 +++
1 file changed, 3 insertions(+)
diff --git a/inc/config.php b/inc/config.php
index 746fcad6..ee1b9086 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -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;
From 56ad753b769bdef838dcee42ddf0d5d007203ed9 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Sat, 12 Oct 2024 16:16:08 +0200
Subject: [PATCH 3/6] functions.php: add new dice function
---
inc/functions.php | 45 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/inc/functions.php b/inc/functions.php
index 644cefbe..bca8d1d1 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -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 = "$text $res";
+ } else {
+ $fmt = "
$res";
+ }
+ if (empty($acc)) {
+ $acc = $fmt;
+ } else {
+ $acc .= " $fmt";
+ }
+ }
+
+ return $acc;
+}
+
function slugify($post) {
global $config;
From f0a33d26c69579a9d7481226ea80e1945ee213e2 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Sat, 12 Oct 2024 16:15:53 +0200
Subject: [PATCH 4/6] config.php: refactor dice markup
---
inc/config.php | 3 +++
1 file changed, 3 insertions(+)
diff --git a/inc/config.php b/inc/config.php
index ee1b9086..16b14413 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -743,6 +743,9 @@
* ====================
*/
+ // Dice Roll Markup.
+ $config['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):
$config['markup'][] = array("/'''(.+?)'''/", "\$1");
$config['markup'][] = array("/''(.+?)''/", "\$1");
From f86f1c344bc349c837639824c81405881290ca09 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Sat, 12 Oct 2024 16:25:01 +0200
Subject: [PATCH 5/6] faq theme: add the dice rolling documentation to the
site's FAQs
---
templates/themes/faq/index.html | 1 +
1 file changed, 1 insertion(+)
diff --git a/templates/themes/faq/index.html b/templates/themes/faq/index.html
index 6423bd1f..6a7a3436 100644
--- a/templates/themes/faq/index.html
+++ b/templates/themes/faq/index.html
@@ -77,6 +77,7 @@
==text== makes it a heading: text
**text** makes it spoiler: text
[code]text[/code] makes it code:
text
+ [dice:XdY+Z] rolls a X dices with Y faces, adding Z to the result: 
Why are my posts being changed / word-filtered ?
From acf358b157a69b6dae0dda7cedbaa1c641995643 Mon Sep 17 00:00:00 2001
From: Zankaria
Date: Mon, 14 Oct 2024 00:26:59 +0200
Subject: [PATCH 6/6] config.php: use modern array syntax for markup
---
inc/config.php | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/inc/config.php b/inc/config.php
index 16b14413..ec73226d 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -743,14 +743,16 @@
* ====================
*/
- // Dice Roll Markup.
- $config['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):
- $config['markup'][] = array("/'''(.+?)'''/", "\$1");
- $config['markup'][] = array("/''(.+?)''/", "\$1");
- $config['markup'][] = array("/\*\*(.+?)\*\*/", "\$1");
- $config['markup'][] = array("/==(.+?)==/", "\$1");
+ $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):
+ [ "/'''(.+?)'''/", "\$1" ],
+ [ "/''(.+?)''/", "\$1" ],
+ [ "/\*\*(.+?)\*\*/", "\$1" ],
+ [ "/==(.+?)==/", "\$1" ]
+ ];
// Code markup. This should be set to a regular expression, using tags you want to use. Examples:
// "/\[code\](.*?)\[\/code\]/is"