-
Notifications
You must be signed in to change notification settings - Fork 5
/
Creole.php
109 lines (100 loc) · 2.84 KB
/
Creole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @copyright Copyright (c) 2015,2023 Nobuo Kihara
* @license https://github.com/softark/creole/blob/master/LICENSE
* @link https://github.com/softark/creole#readme
*/
namespace softark\creole;
/**
* Creole wiki parser for [Creole 1.0 spec](http://www.wikicreole.org/wiki/Creole1.0).
*
* @author Nobuo Kihara <[email protected]>
*/
class Creole extends \cebe\markdown\Parser
{
// include block element parsing using traits
use block\CodeTrait;
use block\HeadlineTrait;
use block\ListTrait;
use block\TableTrait;
use block\RuleTrait;
use block\RawHtmlTrait;
// include inline element parsing using traits
use inline\CodeTrait;
use inline\EmphStrongTrait;
use inline\LinkTrait;
/**
* @var boolean whether to format markup according to HTML5 spec.
* Defaults to `true` which means that markup is formatted as HTML5.
* If you want HTML4, set it to false.
*/
public $html5 = true;
/**
* Consume lines for a paragraph
*
* Allow block elements to break paragraphs
*/
protected function consumeParagraph($lines, $current)
{
// consume until newline
$content = [];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
if ($this->isParagraphEnd($line)) {
break;
}
$content[] = $line;
}
$block = [
'paragraph',
'content' => $this->parseInline(implode("\n", $content)),
];
return [$block, --$i];
}
/**
* Checks if the paragraph ends
* @param $line
* @return bool true if end of paragraph
*/
protected function isParagraphEnd($line)
{
if (empty($line) ||
ltrim($line) === '' ||
$this->identifyHeadline($line) ||
$this->identifyHr($line) ||
$this->identifyUl($line) ||
$this->identifyOl($line) ||
$this->identifyTable($line) ||
$this->identifyCode($line) ||
$this->identifyRawHtml($line)) {
return true;
}
return false;
}
/**
* Parses escaped special characters.
* Creole uses tilde (~) for the escaping marker.
* It should escape the next character whatever it would be.
* @marker ~
*/
protected function parseEscape($text)
{
if (isset($text[1])) {
return [['text', $text[1]], 2];
}
return [['text', $text[0]], 1];
}
/**
* @inheritdocs
*
* Parses a newline indicated by two backslashes, and
* escape '&', '<', and '>'.
*/
protected function renderText($text)
{
return str_replace(
['&', '<', '>', "\\\\"],
['&', '<', '>', $this->html5 ? "<br>" : "<br />"],
$text[1]);
}
}