forked from edwinm/Beautify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeautify.php
147 lines (136 loc) · 5.72 KB
/
beautify.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Beautify
* Combines four text tools into one function
* - Markdown: Simpeler and more readable than HTML
* - SmartyPants: Better typography (curly quotes, ellipsis...)
* - GeSHi: Adds syntax highlighting to code
* - dot: Converts Dot diagrams to SVG and VML
*
* See http://www.bitstorm.org/weblog/2012-8/Beautify_Markdown_SmartyPants_GeSHi_and_Dot_combined.html
*
* Git: https://github.com/edwinm/Beautify
*
* Copyright 2012 Edwin Martin <[email protected]>
*
* License: MIT and GPL
*
* Install Graphvis to get Dot support.
*/
/*
* User config
*
* Change the paths below to the actual paths on your OS.
*/
// Download latest version from http://michelf.ca/projects/php-markdown/
require_once __DIR__."/PHP-Markdown-Extra-1.2.5/markdown.php";
// Download latest version from http://sourceforge.net/projects/geshi/files/
require_once __DIR__."/geshi/geshi.php";
// Download latest version from http://daringfireball.net/projects/smartypants/
require_once __DIR__."/SmartyPants/smartypants-typographer.php";
// Full path to dot program and error log
if (substr(strtoupper(PHP_OS),0,3) == "WIN") { // Windows
define('DOTPATH', '"C:/Program Files (x86)/Graphviz2.34/bin/dot.exe"');
define('DOTERRLOGPATH', 'C:/WINDOWS/Temp/dot-errlog.txt');
} else if (PHP_OS == "Darwin") { // Mac OS X
define('DOTPATH', '/opt/local/bin/dot');
define('DOTERRLOGPATH', '/tmp/dot-errorlog.txt');
} else { // Assume Linux or other UNIX-like OS
define('DOTPATH', '/usr/bin/dot');
define('DOTERRLOGPATH', '/tmp/dot-errorlog.txt');
}
/*
* Output VML for IE8 and older so they can see graphs?
* false: for IE8 and older, an error message is shown
* true: for IE8 and older, VML is generated
*/
define('OUTPUTVML', false);
/**
* @param $s string Input string
* @return string HTML string
*/
function beautify($s) {
$offset = 0;
$result = '';
// Split input in code and text parts
$n = preg_match_all('|((\r?\n~~~+)\s*([a-z0-9_-]*)\r?\n)(.*?)\2\r?\n|s', $s, $matches, PREG_OFFSET_CAPTURE);
for($i = 0; $i < $n; $i++) {
$md = substr($s, $offset, $matches[4][$i][1] - $offset - strlen($matches[1][$i][0]));
$result .= SmartyPants(Markdown($md));
$code = html_entity_decode(trim($matches[4][$i][0]));
$language = $matches[3][$i][0];
if ($language == "dot-view") {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", DOTERRLOGPATH, "w")
);
$proc = proc_open(DOTPATH." -Tsvg", $descriptorspec, $pipes);
if (is_resource($proc)) {
fwrite($pipes[0], $code);
fclose($pipes[0]);
$svg = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($proc);
$svg = preg_replace("/.*<svg/s", "<svg", $svg); // Remove <?xml and <!doctype...
$svg = preg_replace("/id=\"(.*?)\"/s", "id=\"$1_$i\"", $svg); // Prevent duplicate id's
if (OUTPUTVML) {
$proc = proc_open(DOTPATH." -Tvml", $descriptorspec, $pipes);
fwrite($pipes[0], $code);
fclose($pipes[0]);
$vml = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($proc);
$vml = preg_replace("/<!--.*?-->/s", "", $vml); // Remove comments in favor of conditional comments
} else {
$vml = "<p>SVG is not supported by your browser</p>";
}
$out = "<div class=\"beautify-graph\"><!--[if gt IE 8]>-->$svg<!--<![endif]--><!--[if lte IE 8]>$vml<![endif]--></div>\r\n";
$result .= $out;
}
}
elseif ($language == "neato-view") {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", DOTERRLOGPATH, "w")
);
$proc = proc_open( str_replace( "dot", "neato", DOTPATH )." -Tsvg", $descriptorspec, $pipes);
if (is_resource($proc)) {
fwrite($pipes[0], $code);
fclose($pipes[0]);
$svg = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($proc);
$svg = preg_replace("/.*<svg/s", "<svg", $svg); // Remove <?xml and <!doctype...
$svg = preg_replace("/id=\"(.*?)\"/s", "id=\"$1_$i\"", $svg); // Prevent duplicate id's
if (OUTPUTVML) {
$proc = proc_open(DOTPATH." -Tvml", $descriptorspec, $pipes);
fwrite($pipes[0], $code);
fclose($pipes[0]);
$vml = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($proc);
$vml = preg_replace("/<!--.*?-->/s", "", $vml); // Remove comments in favor of conditional comments
} else {
$vml = "<p>SVG is not supported by your browser</p>";
}
$out = "<div class=\"beautify-graph\"><!--[if gt IE 8]>-->$svg<!--<![endif]--><!--[if lte IE 8]>$vml<![endif]--></div>\r\n";
$result .= $out;
}
}
else {
if (!$language) {
$language = 'text';
}
$geshi = new GeSHi($code, $language);
// Optionally enable GeSHi-features:
// $geshi->enable_classes();
// $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$result .= $geshi->parse_code();
}
$offset = $matches[4][$i][1] + strlen($matches[4][$i][0]) + strlen($matches[2][$i][0]);
}
$result .= SmartyPants(Markdown(substr($s, $offset)));
return $result;
}