forked from leftypol/leftypol
Merge branch 'diceroll-2' into 'config'
Add inline dicerolling. Fix #15 Closes #15 See merge request leftypol/leftypol!2
This commit is contained in:
commit
a86c9f8d8f
4 changed files with 65 additions and 5 deletions
|
@ -681,6 +681,9 @@
|
||||||
// with the modifier Z added, with the result displayed at the top of the post body.
|
// with the modifier Z added, with the result displayed at the top of the post body.
|
||||||
$config['allow_roll'] = false;
|
$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
|
// Use semantic URLs for threads, like /b/res/12345/daily-programming-thread.html
|
||||||
$config['slugify'] = false;
|
$config['slugify'] = false;
|
||||||
|
|
||||||
|
@ -740,11 +743,16 @@
|
||||||
* ====================
|
* ====================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// "Wiki" markup syntax ($config['wiki_markup'] in pervious versions):
|
|
||||||
$config['markup'][] = array("/'''(.+?)'''/", "<strong>\$1</strong>");
|
$config['markup'] = [
|
||||||
$config['markup'][] = array("/''(.+?)''/", "<em>\$1</em>");
|
// Dice Roll Markup.
|
||||||
$config['markup'][] = array("/\*\*(.+?)\*\*/", "<span class=\"spoiler\">\$1</span>");
|
[ "/!([-+]?\d+)?([d])([-+]?\d+)([-+]\d+)?/iu", fn($m) => handle_dice_roll_markup($m, 'static/d10.svg') ],
|
||||||
$config['markup'][] = array("/==(.+?)==/", "<span class=\"heading\">\$1</span>");
|
// "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 markup. This should be set to a regular expression, using tags you want to use. Examples:
|
||||||
// "/\[code\](.*?)\[\/code\]/is"
|
// "/\[code\](.*?)\[\/code\]/is"
|
||||||
|
|
|
@ -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) {
|
function slugify($post) {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
|
|
|
@ -1487,6 +1487,12 @@ li.mix {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Dice roll support */
|
||||||
|
.inline-dice {
|
||||||
|
width: 1em;
|
||||||
|
height: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
/* Mona Font */
|
/* Mona Font */
|
||||||
.aa {
|
.aa {
|
||||||
font-family: Mona, "MS PGothic", "MS Pゴシック", sans-serif;
|
font-family: Mona, "MS PGothic", "MS Pゴシック", sans-serif;
|
||||||
|
|
|
@ -77,6 +77,7 @@
|
||||||
<p> ==text== makes it a heading: <span class="heading">text</span></p>
|
<p> ==text== makes it a heading: <span class="heading">text</span></p>
|
||||||
<p> **text** makes it spoiler: <span class="spoiler">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> [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">
|
<h2 id="7">
|
||||||
Why are my posts being changed / word-filtered ?
|
Why are my posts being changed / word-filtered ?
|
||||||
</h2>
|
</h2>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue