forked from crosswalk-project/crosswalk-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-match.inc
executable file
·51 lines (49 loc) · 1.79 KB
/
smart-match.inc
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
<?php
function file_smart_match ($filepath) {
if (file_exists ($filepath) && !is_dir ($filepath))
return $filepath;
$dir = dir_smart_match (dirname ($filepath));
if (!$dir)
return false;
$lower = strtolower (basename ($filepath));
$files = glob ($dir.'/*');
foreach ($files as $f) {
if (is_dir ($f))
continue;
/* Check if this globbed filename matches a markdown file, return
* the base name */
if (preg_match (
'/(([0-9]*-)?(.*))((\.md)|(\.mediawiki)|(\.org)|(\.php))$/i',
basename ($f), $matches)) {
if (strtolower ($matches[3]) == $lower)
return $f;
}
/* Check if this globbed filename matches a markdown file that has been
* converted to .html already. Return it IFF the original markdown
* does not exist, OR if it is a pure PHP file */
if (preg_match (
'/(([0-9]*-)?(.*))((\.md)|(\.mediawiki)|(\.org))((\.html)|(\.php))$/i',
basename ($f), $matches)) {
if (strtolower ($matches[3]) == $lower &&
!file_exists (dirname ($filepath).'/'.$matches[1].$matches[3]))
return $f;
}
}
return false;
}
function dir_smart_match ($filepath) {
if (file_exists ($filepath) && is_dir ($filepath))
return $filepath;
$lower = strtolower (basename ($filepath));
$files = glob (dirname ($filepath).'/*');
foreach ($files as $f) {
if (!is_dir ($f))
continue;
/* Check if this globbed filename matches a directory */
if (preg_match ('/(([0-9]*-)?(.*))$/i', basename ($f), $matches)) {
if (strtolower ($matches[3]) == $lower)
return $f;
}
}
return false;
}