1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm\Helpers; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Wrapper |
17
|
|
|
* @property Wrapper $md Get markdown wrapped code |
18
|
|
|
* @property Wrapper $html Get html `pre` wrapped code |
19
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
20
|
|
|
*/ |
21
|
|
|
class InlineWrapper |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
protected $setup = false; |
25
|
|
|
private $text = ''; |
26
|
|
|
private $link = ''; |
27
|
|
|
private $isMd = false; |
28
|
|
|
private $isHtml = false; |
29
|
|
|
private $isRaw = false; |
30
|
|
|
private static $def = []; |
31
|
|
|
|
32
|
|
|
public function __construct($text = '', $link = '') |
33
|
|
|
{ |
34
|
|
|
$this->text = $text; |
35
|
|
|
$this->link = $link; |
36
|
|
|
foreach (self::$def as $flag => $value) |
37
|
|
|
{ |
38
|
|
|
$this->$flag = $value; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function defaults() |
43
|
|
|
{ |
44
|
|
|
// TODO Allow settings defaults |
45
|
|
|
// See: https://github.com/Maslosoft/Zamm/issues/4 |
46
|
|
|
$wrapper = new static; |
47
|
|
|
$wrapper->setup = true; |
48
|
|
|
return $wrapper; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __get($name) |
52
|
|
|
{ |
53
|
|
|
return $this->$name(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function md($value = true) |
57
|
|
|
{ |
58
|
|
|
$this->setFlag('isMd', $value); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function html($value = true) |
63
|
|
|
{ |
64
|
|
|
$this->setFlag('isHtml', $value); |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Strip leading $ from variables and trailing () from methods |
70
|
|
|
* @return InlineWrapper |
71
|
|
|
*/ |
72
|
|
|
public function raw($value = true) |
73
|
|
|
{ |
74
|
|
|
$this->setFlag('isRaw', $value); |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function __toString() |
79
|
|
|
{ |
80
|
|
|
if ($this->isRaw) |
81
|
|
|
{ |
82
|
|
|
$text = trim($this->text, '$()'); |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
|
|
$text = $this->text; |
87
|
|
|
} |
88
|
|
|
if ($this->isMd) |
89
|
|
|
{ |
90
|
|
|
return $this->wrap("`$text`"); |
91
|
|
|
} |
92
|
|
|
if ($this->isHtml) |
93
|
|
|
{ |
94
|
|
|
return $this->wrap("<code>$text</code>"); |
95
|
|
|
} |
96
|
|
|
return $this->wrap($this->text); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function wrap($text) |
100
|
|
|
{ |
101
|
|
|
if ($this->link) |
102
|
|
|
{ |
103
|
|
|
if ($this->isMd) |
104
|
|
|
{ |
105
|
|
|
return sprintf('[%s](%s)', $text, $this->link); |
106
|
|
|
} |
107
|
|
|
// if ($this->isHtml) |
108
|
|
|
// { |
109
|
|
|
return sprintf('<a href="%s" class="api-link">%s</a>', $this->link, $text); |
110
|
|
|
// } |
111
|
|
|
} |
112
|
|
|
return $text; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function setFlag($flag, $value = true) |
116
|
|
|
{ |
117
|
|
|
$this->$flag = $value; |
118
|
|
|
if ($this->setup) |
119
|
|
|
{ |
120
|
|
|
self::$def[$flag] = $value; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|