-
Notifications
You must be signed in to change notification settings - Fork 1
/
themr.php
170 lines (124 loc) · 3.39 KB
/
themr.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
//
// Themr for Tumblr
//
// Created by Prateek Rungta.
//
// determine the output mode
// false -> HTML
// true -> Tumblr theme
define('THEMR_MODE', isset($_GET['tumblr']));
// constants
define('THEMR_DATA', 'default_data.json'); // default data file
// Themr
//
// Class for translating template tags.
// Supports conditional blocks (contexts)
// and variables (data)
class Themr {
var $data = array();
var $contexts = array();
var $context_stack = array();
function __construct($data) {
$this->data = $data;
$this->context_stack = array($data);
}
private function push($key) {
$context = $this->get($key);
if ($context !== false && is_array($context)) {
array_unshift($this->context_stack, $context);
$this->contexts[$key] = true;
} else {
$this->contexts[$key] = null;
}
return $this->contexts[$key];
}
private function pop($key) {
if (isset($this->contexts[$key])) array_shift($this->context_stack);
}
private function get($key) {
$key = strtolower($key);
foreach ($this->context_stack as $context) {
if (isset($context[$key])) return $context[$key];
}
return false;
}
public function process($tag) {
// determine if it is a block tag
$block_tag = strpos($tag, ':') !== false;
if ($block_tag) {
$tag = explode(':', $tag);
if ($tag[0] == 'block') {
// push new context
$this->push($tag[1]);
} elseif ($tag[0] == '/block') {
// pop old context
$this->pop($tag[1]);
}
return;
} else {
// not a block tag, so look for value
// in current contexts and print that
$value = $this->get($tag);
return $value === false ? "{".$tag."}" : $value;
}
}
// End of class
}
function styles($css) {
if (THEMR_MODE) {
// concat the CSS files
echo "<style>\n";
foreach ($css as $href => $media) {
if ($media) { echo "@media $media {\n"; }
echo "\n/***** $href *****/\n\n";
readfile($href);
echo "\n";
if ($media) { echo "}\n\n"; }
}
echo "</style>";
} else {
// external <link> references
foreach ($css as $href => $media) {
echo '<link rel="stylesheet" href="'.$href.'"'.($media ? ' media="'.$media.'"' : '').">\n";
}
}
}
function scripts($js) {
if (THEMR_MODE) {
// concat the JS files
echo "<script>\n//<![CDATA[\n";
foreach ($js as $href) {
readfile($href);
echo "\n";
}
echo "//]]>\n</script>";
} else {
// external <script> references
foreach ($js as $href) {
echo "<script src=\"$href\"></script>\n";
}
}
}
// main
if (THEMR_MODE) {
function _($tag) {
echo "{".$tag."}". // spit out Tumblr tags
(strpos($tag, ':') === false ? '' : "\n"); // newline character after {block:} tags
return;
}
} else {
// try parsing the specified JSON file for use as theme data
$json = null;
if (isset($data_file)) $json = json_decode(file_get_contents($data_file), true);
if (!$json) {
// could not parse, so fallback to default data file
$file = file_get_contents(THEMR_DATA);
$json = json_decode($file, true);
}
$theme = new Themr($json);
function _($tag) {
global $theme;
return $theme->process($tag);
}
}