xref: /dokuwiki/inc/Parsing/ParserMode/GfmLinebreak.php (revision d331a8396503a69ec91cd77124b1b8983c251c54)
1c4bcbc2eSAndreas Gohr<?php
2c4bcbc2eSAndreas Gohr
3c4bcbc2eSAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4c4bcbc2eSAndreas Gohr
5c4bcbc2eSAndreas Gohruse dokuwiki\Parsing\Handler;
6c4bcbc2eSAndreas Gohr
7c4bcbc2eSAndreas Gohr/**
8c4bcbc2eSAndreas Gohr * GFM hard line break: two-or-more trailing spaces, or a single
9c4bcbc2eSAndreas Gohr * backslash, immediately before a non-final newline.
10c4bcbc2eSAndreas Gohr *
11c4bcbc2eSAndreas Gohr * Both delimiter forms land in one mode because they share semantics
12c4bcbc2eSAndreas Gohr * (emit linebreak), share the block-boundary rule (no break at the
13c4bcbc2eSAndreas Gohr * end of a paragraph or other block), and share the next-line
14c4bcbc2eSAndreas Gohr * leading-whitespace consumption (GFM strips it). Keeping all hard-
15c4bcbc2eSAndreas Gohr * break logic in one pattern is cheaper than two and matches GFM
16c4bcbc2eSAndreas Gohr * spec section 6.7 directly.
17c4bcbc2eSAndreas Gohr *
18c4bcbc2eSAndreas Gohr * Bypass inside code spans and fenced blocks falls out for free:
19c4bcbc2eSAndreas Gohr * those are whole-span PROTECTED / FORMATTING modes that capture
20*d331a839SAndreas Gohr * their body in one regex match, so SUBSTITUTION patterns never see
21c4bcbc2eSAndreas Gohr * the inner text — same mechanism that exempts GfmEscape from
22c4bcbc2eSAndreas Gohr * code spans.
23c4bcbc2eSAndreas Gohr *
24c4bcbc2eSAndreas Gohr * No collision with the existing DokuWiki Linebreak mode (also at
25c4bcbc2eSAndreas Gohr * sort 140): DW's pattern is a literal double backslash `\\`,
26c4bcbc2eSAndreas Gohr * unrelated to either GFM delimiter form. In mixed syntax settings
27c4bcbc2eSAndreas Gohr * both modes can load and the leftmost match wins position-by-
28c4bcbc2eSAndreas Gohr * position. GfmEscape (sort 5) does not steal the backslash form
29c4bcbc2eSAndreas Gohr * either: its pattern requires the next char to be ASCII
30c4bcbc2eSAndreas Gohr * punctuation, and `\n` is not punctuation.
31c4bcbc2eSAndreas Gohr */
32c4bcbc2eSAndreas Gohrclass GfmLinebreak extends AbstractMode
33c4bcbc2eSAndreas Gohr{
34c4bcbc2eSAndreas Gohr    /** @inheritdoc */
35c4bcbc2eSAndreas Gohr    public function getSort()
36c4bcbc2eSAndreas Gohr    {
37c4bcbc2eSAndreas Gohr        return 140;
38c4bcbc2eSAndreas Gohr    }
39c4bcbc2eSAndreas Gohr
40c4bcbc2eSAndreas Gohr    /** @inheritdoc */
41c4bcbc2eSAndreas Gohr    public function connectTo($mode)
42c4bcbc2eSAndreas Gohr    {
43c4bcbc2eSAndreas Gohr        // (?:[ ]{2,}|\\)            two+ spaces OR one backslash
44c4bcbc2eSAndreas Gohr        // \n                        the line ending
45c4bcbc2eSAndreas Gohr        // (?![ \t]*(?:\n|\z))       not at a paragraph break or EOF
46c4bcbc2eSAndreas Gohr        // [ \t]*                    swallow leading WS of the next line
47c4bcbc2eSAndreas Gohr        $this->Lexer->addSpecialPattern(
48c4bcbc2eSAndreas Gohr            '(?:[ ]{2,}|\\\\)\n(?![ \t]*(?:\n|\z))[ \t]*',
49c4bcbc2eSAndreas Gohr            $mode,
50c4bcbc2eSAndreas Gohr            'gfm_linebreak'
51c4bcbc2eSAndreas Gohr        );
52c4bcbc2eSAndreas Gohr    }
53c4bcbc2eSAndreas Gohr
54c4bcbc2eSAndreas Gohr    /** @inheritdoc */
55c4bcbc2eSAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
56c4bcbc2eSAndreas Gohr    {
57c4bcbc2eSAndreas Gohr        $handler->addCall('linebreak', [], $pos);
58c4bcbc2eSAndreas Gohr        return true;
59c4bcbc2eSAndreas Gohr    }
60c4bcbc2eSAndreas Gohr}
61