forked from cebe/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapMarkdown.php
More file actions
118 lines (106 loc) · 3.42 KB
/
BootstrapMarkdown.php
File metadata and controls
118 lines (106 loc) · 3.42 KB
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
110
111
112
113
114
115
116
117
118
<?php
/**
* BootstrapGrid for cebe/markdown
* inspired by https://github.com/dreikanter/markdown-grid
* @author Andreas Neumann, a.neumann@netzweberei.de
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown;
class BootstrapMarkdown extends \cebe\markdown\MarkdownExtra
{
protected function identifyLine($lines, $current)
{
$matches = array();
preg_match('/-- row .*--/', $lines[$current], $matches);
if(count($matches)){
return 'bootstrapGrid';
}
return parent::identifyLine($lines, $current);
}
protected function consumeBootstrapGrid($lines, $current)
{
// create block array
$block = [
'type' => 'bootstrapGrid',
'content' => []
];
$block['content'][] = '<!--mdtb:row-->';
$line = rtrim($lines[$current]);
$test = array();
preg_match('/-- row[ ]*(\d+(?:,[ ]?\d+)*)*[ ]*--/', $line, $test);
if(count($test)>1) $cols = (explode(',',$test[1]));
if(is_array($cols))
{
$block['content'][] = '<!--mdtb:col'.current($cols).'-->';
next($cols);
}
// detect end of grid
$endOfBlock = '-- end --';
$endOfCol = '----';
// consume all lines until $endOfBlock
for($i = $current + 1, $count = count($lines); $i < $count; $i++) {
if (rtrim($line = $lines[$i]) !== $endOfBlock)
{
if (rtrim($line = $lines[$i]) == $endOfCol && is_array($cols))
{
// $block['content'][] = '';
$block['content'][] = '<!--mdtb:endcol-->';
$block['content'][] = '<!--mdtb:col'.current($cols).'-->';
next($cols);
}
else
{
$block['content'][] = $line;
}
}
else
{
// stop consuming when code block is over
break;
}
}
if(is_array($cols))
{
$block['content'][] = '<!--mdtb:endcol-->';
}
$block['content'][] = '<!--mdtb:endrow-->';
return [$block, $i];
}
protected function renderBootstrapGrid($block)
{
return $this->parseBlocks($block['content']);
}
/**
* Consume lines for an HTML block
*/
protected function consumeHtml($lines, $current)
{
$block = [
'type' => 'html',
'content' => [],
];
$matches = array();
preg_match('/^<!--mdtb:([a-z]*)(\d+)?-->/', $lines[$current], $matches);
if (count($matches)>1) // TwitterBootstrap-Comment
{
switch($matches[1])
{
case 'row':
$block['content'][] = '<div class="row-fluid">';
break;
case 'col':
$span = $matches[2] ? $matches[2] : '';
$block['content'][] = '<div class="span'.$span.'">';
break;
case 'endrow':
case 'endcol':
$block['content'][] = '</div>';
break;
}
return [$block, $current];
}
return parent::consumeHtml($lines, $current);
}
}