-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
executable file
·123 lines (111 loc) · 2.75 KB
/
helpers.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
<?php
const ROOT = __DIR__;
/**
* Render code ready for highlighting.
*
* @param string $lang Language for highlighting.
* @param string $code Code itself.
* @param bool|true $wrapWithPre Wrap code with <pre> tag?
* @param string $preClasses Additional <pre> classes.
*
* @return string
*/
function code($lang, $code, $wrapWithPre = true, $preClasses = '') {
$lines = explode("\n", $code);
if (!strlen(trim($lines[0]))) {
array_shift($lines);
}
if (!strlen(trim($lines[count($lines) - 1]))) {
array_pop($lines);
}
$code = implode('', array_map(function($line) use ($lang) {
$escapedLine = strlen($line) ? htmlentities($line) : ' ';
return sprintf('<code class="%s">%s</code>', $lang, $escapedLine);
}, $lines));
return $wrapWithPre ? '<pre class="code ' . $preClasses . '">' . $code . '</pre>' : $code;
}
/**
* Get list of slides full paths.
*
* @param string $root Path to slides root dir.
* @param callable|null $filterCallback Function to filter the list of slides.
*
* @return array
*/
function getSlides($root, callable $filterCallback = null) {
$slides = glob($root . '/*.slide.php');
if ($filterCallback !== null) {
$slides = array_filter($slides, $filterCallback);
}
natsort($slides);
return $slides;
}
/**
* Check if slide name is in provided list.
*
* @param string $slide
* @param array $list
*
* @return bool
*/
function slideInList($slide, $list) {
return array_reduce($list, function($result, $listItem) use ($slide) {
return $result || strpos(basename($slide), (string) $listItem) === 0;
}, false);
}
/**
* Get slides filter based on exclude list.
*
* @param array $excludeList
*
* @return Closure
*/
function excludeSlides($excludeList) {
return function($item) use ($excludeList) {
return !slideInList($item, $excludeList);
};
}
/**
* Get slides filter based on include list.
*
* @param array $includeList
*
* @return Closure
*/
function includeSlides($includeList) {
return function($item) use ($includeList) {
return slideInList($item, $includeList);
};
}
/**
* Output slides.
*
* @param array $slides Slides paths.
*/
function renderSlides($slides) {
foreach ($slides as $slideFile) {
echo PHP_EOL;
include $slideFile;
echo PHP_EOL;
}
}
/**
* Get slide ID from slide name.
*
* @param $filename
*
* @return mixed
*/
function getSlideId($filename) {
return preg_replace('/.*\/[\d-]+\.(.+)\.slide\.php$/', '$1', $filename);
}
/**
* Get href attribute value for <base> tag.
*
* @param string $pathToShow
*
* @return string
*/
function getBaseHref($pathToShow) {
return str_replace(ROOT, '', $pathToShow) . '/';
}