1: <?php
2:
3: namespace Alpha\Util\Extension;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Michelf\MarkdownExtra;
7: use Alpha\Util\Code\Highlight\HighlightProviderFactory;
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: class Markdown extends MarkdownExtra
52: {
53: 54: 55: 56: 57: 58:
59: public function _doCodeBlocks_callback($matches)
60: {
61: $config = ConfigProvider::getInstance();
62:
63: $codeblock = $matches[1];
64:
65: $codeblock = $this->outdent($codeblock);
66:
67:
68: $codeblock = preg_replace(array('/\A\n+/', '/\n+\z/'), '', $codeblock);
69:
70:
71: $codeTypeTag = array();
72: preg_match('/codeType=\[.*\]/', $codeblock, $codeTypeTag);
73: $codeblock = preg_replace('/codeType=\[.*\]\n/', '', $codeblock);
74:
75: if (isset($codeTypeTag[0])) {
76: $start = mb_strpos($codeTypeTag[0], '[');
77: $end = mb_strpos($codeTypeTag[0], ']');
78: $language = mb_substr($codeTypeTag[0], $start + 1, $end - ($start + 1));
79: } else {
80:
81: $language = 'php';
82: }
83:
84: if ($config->get('cms.highlight.provider.name') != '') {
85: $highlighter = HighlightProviderFactory::getInstance($config->get('cms.highlight.provider.name'));
86: $codeblock = $highlighter->highlight($codeblock, $language);
87: } else {
88: $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
89:
90:
91: $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
92:
93: $codeblock = "<pre><code>$codeblock\n</code></pre>";
94: }
95:
96: return "\n\n".$this->hashBlock($codeblock)."\n\n";
97: }
98:
99: 100: 101: 102: 103: 104:
105: public function _doAnchors_inline_callback($matches)
106: {
107: $config = ConfigProvider::getInstance();
108:
109: $whole_match = $matches[1];
110: $link_text = $this->runSpanGamut($matches[2]);
111: $url = $matches[3] == '' ? $matches[4] : $matches[3];
112: $title =& $matches[7];
113: $attr = $this->doExtraAttributes("a", $dummy =& $matches[8]);
114:
115: $external = false;
116:
117: $parts = parse_url($url);
118:
119: 120: 121: 122: 123: 124: 125:
126: if (is_array($parts) && isset($parts['host']) && mb_strpos($url, $config->get('app.url')) === false) {
127: $external = true;
128: }
129:
130: $url = $this->encodeAttribute($url);
131:
132: $result = "<a href=\"$url\"";
133: if (isset($title)) {
134: $title = $this->encodeAttribute($title);
135: $result .= " title=\"$title\"";
136: }
137: if ($external) {
138: $result .= " target=\"$url\"";
139: }
140: $result .= $attr;
141:
142: $link_text = $this->runSpanGamut($link_text);
143: $result .= ">$link_text</a>";
144:
145: return $this->hashPart($result);
146: }
147:
148: 149: 150: 151: 152:
153: public function _doTable_callback($matches)
154: {
155: $head = $matches[1];
156: $underline = $matches[2];
157: $content = $matches[3];
158:
159:
160: $head = preg_replace('/[|] *$/m', '', $head);
161: $underline = preg_replace('/[|] *$/m', '', $underline);
162: $content = preg_replace('/[|] *$/m', '', $content);
163:
164:
165: $separators = preg_split('/ *[|] */', $underline);
166: foreach ($separators as $n => $s) {
167: if (preg_match('/^ *-+: *$/', $s))
168: $attr[$n] = $this->_doTable_makeAlignAttr('right');
169: else if (preg_match('/^ *:-+: *$/', $s))
170: $attr[$n] = $this->_doTable_makeAlignAttr('center');
171: else if (preg_match('/^ *:-+ *$/', $s))
172: $attr[$n] = $this->_doTable_makeAlignAttr('left');
173: else
174: $attr[$n] = '';
175: }
176:
177:
178:
179: $head = $this->parseSpan($head);
180: $headers = preg_split('/ *[|] */', $head);
181: $col_count = count($headers);
182: $attr = array_pad($attr, $col_count, '');
183:
184:
185: $text = "<table class=\"table table-bordered\">\n";
186: $text .= "<thead>\n";
187: $text .= "<tr>\n";
188: foreach ($headers as $n => $header)
189: $text .= " <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n";
190: $text .= "</tr>\n";
191: $text .= "</thead>\n";
192:
193:
194: $rows = explode("\n", trim($content, "\n"));
195:
196: $text .= "<tbody>\n";
197: foreach ($rows as $row) {
198:
199:
200: $row = $this->parseSpan($row);
201:
202:
203: $row_cells = preg_split('/ *[|] */', $row, $col_count);
204: $row_cells = array_pad($row_cells, $col_count, '');
205:
206: $text .= "<tr>\n";
207: foreach ($row_cells as $n => $cell)
208: $text .= " <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n";
209: $text .= "</tr>\n";
210: }
211: $text .= "</tbody>\n";
212: $text .= "</table>";
213:
214: return $this->hashBlock($text) . "\n";
215: }
216: }
217: