xref: /dokuwiki/inc/Parsing/Handler/GfmLists.php (revision 685560eb3044321b3bdd0be40985871ced5f1d05)
1*685560ebSAndreas Gohr<?php
2*685560ebSAndreas Gohr
3*685560ebSAndreas Gohrnamespace dokuwiki\Parsing\Handler;
4*685560ebSAndreas Gohr
5*685560ebSAndreas Gohr/**
6*685560ebSAndreas Gohr * CallWriter rewriter for GFM lists.
7*685560ebSAndreas Gohr *
8*685560ebSAndreas Gohr * GFM accepts `-`, `*`, or `+` for unordered markers and `\d{1,9}[.)]` for
9*685560ebSAndreas Gohr * ordered markers, with the first ordered item's number carried through as
10*685560ebSAndreas Gohr * the `start` attribute. Indentation is a multiple of 2 starting at 0;
11*685560ebSAndreas Gohr * depth = (spaces / 2) + 1, odd indents round down, tabs become two spaces.
12*685560ebSAndreas Gohr *
13*685560ebSAndreas Gohr * The state machine lives in {@see AbstractListsRewriter}; this class
14*685560ebSAndreas Gohr * supplies only the marker parser.
15*685560ebSAndreas Gohr */
16*685560ebSAndreas Gohrclass GfmLists extends AbstractListsRewriter
17*685560ebSAndreas Gohr{
18*685560ebSAndreas Gohr    /** @inheritdoc */
19*685560ebSAndreas Gohr    protected function interpretSyntax(string $match): array
20*685560ebSAndreas Gohr    {
21*685560ebSAndreas Gohr        $stripped = str_replace("\t", '  ', ltrim($match, "\n"));
22*685560ebSAndreas Gohr        $indent = strspn($stripped, ' ');
23*685560ebSAndreas Gohr        $digitLen = strspn($stripped, '0123456789', $indent);
24*685560ebSAndreas Gohr
25*685560ebSAndreas Gohr        return [
26*685560ebSAndreas Gohr            'depth' => intdiv($indent, 2) + 1,
27*685560ebSAndreas Gohr            'type' => $digitLen > 0 ? 'o' : 'u',
28*685560ebSAndreas Gohr            'start' => $digitLen > 0 ? (int) substr($stripped, $indent, $digitLen) : 1,
29*685560ebSAndreas Gohr        ];
30*685560ebSAndreas Gohr    }
31*685560ebSAndreas Gohr}
32