This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
Mockdown.php
71 lines (61 loc) · 1.81 KB
/
Mockdown.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
<?php
/**
* Mockdown
* By Sam Minnée
*
* Mockdown is an ASCII-art HTML UI mockup language, inspired by Markdown.
* It's licensed under the BSD license.
*
* See the README.md for more info.
*/
/**
* Mockdown rendering function.
* Uses the Mockdown class to do all the real work.
*/
function Mockdown($content) {
$m = new Mockdown;
return $m->render($content);
}
class Mockdown {
function render($content) {
foreach($this->matchers as $rule => $outcome) {
$content = preg_replace_callback($rule, array($this,$outcome), $content);
}
return "<pre class=\"mockdown\">$content</pre>";
}
/**
* Matcher expressions
*/
protected $matchers = array(
'/\[([xo ])\]/i' => 'checkbox',
'/\(([xo ])\)/i' => 'radio',
'/\[ +([^\[\]]{2,}) +\]/i' => 'button',
'/(\[([^\[\]_]*)(_+)\]\n){2,}/i' => 'textarea',
'/\[([^\[\]_]*)(_+)\]/i' => 'textfield',
);
// Matcher implementations
function checkbox($match) {
$checkedClause = ($match[1] != ' ') ? 'checked="checked"' : '';
return "<input type=\"checkbox\" $checkedClause>";
}
function radio($match) {
$checkedClause = ($match[1] != ' ') ? 'checked="checked"' : '';
return "<input type=\"radio\" value=\"foo\" $checkedClause>";
}
function button($match) {
$buttonLabel = trim($match[1]);
return "<input type=\"submit\" value=\"$buttonLabel\">";
}
function textfield($match) {
$size = strlen($match[1]) + strlen($match[2]);
$value = $match[1];
return "<input type=\"text\" size=\"$size\" value=\"$value\">";
}
function textarea($match) {
$width = strlen($match[2]) + strlen($match[3]);
$height = substr_count(trim($match[0]), "\n")+1;
$value = preg_replace('/(^\[)|(\]$)|(\]\n\[)/', "\n", $match[0]);
$value = trim(preg_replace('/_+\n/',"\n", $value));
return "<textarea cols=\"$width\" rows=\"$height\">$value</textarea>\n";
}
}