-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
168 lines (120 loc) · 4.74 KB
/
functions.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
168
<?php
function get_posts($dir,$files = array()) {
if(empty($files)) $files = array_diff(scandir($dir), array('..', '.'));
$posts = array();
foreach ($files as $entry) {
$parts = explode('.', $entry);
if($parts[1] == 'md') {
$post = new Post;
if($dir == CONTENT_DIR) $post->isPage = false;
else $post->isPage = true;
if(file_exists($dir.$entry)) {
$content = array();
$file = file_get_contents($dir.$entry);
$headers = array(
'title' => 'Title'
// eventually one can add more fields, for istance:
//'date' => 'Date'
);
foreach ($headers as $field => $regex){
if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $file, $match) && $match[1]) {
$content[ $field ] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
} else {
$content[ $field ] = '';
}
}
$content['body'] = preg_replace('#/\*.+?\*/#s', '', $file); // Remove comments and meta
/* New post creation */
// file and title
$post->file = str_replace('.md','',$entry);
$post->title = $content['title'];
// only a blog entry has a date
if(!$post->isPage) {
// date
$format = 'Y-m-d-H:i'; // 2015-01-01-18:00
// firstly I will try with hour and minute, if it fails, try only with Y-m-d
if(($date = date_create_from_format($format, $post->file)) == false) {
$format = 'Y-m-d';
$date = date_create_from_format($format, $post->file);
}
$post->date = $date;
}
//body
$post->body = $content['body'];
array_push($posts, $post);
// compare files dates and order from last to first
usort($posts, function($a, $b) {
return $a->date < $b->date;
});
} else {
// not found, nearly impossible
$post->title = "404: Not found";
$post->date = NULL;
$post->body = NULL;
array_push($posts, $post);
}
}
}
return $posts;
}
function list_archive() {
$files = array_diff(scandir(CONTENT_DIR), array('..', '.'));
$archive = array();
foreach ($files as $entry) {
$parts = explode('.', $entry);
if($parts[1] == 'md') {
if(file_exists(CONTENT_DIR.$entry)) {
// date
$format = 'Y-m-d-H:i'; // 2015-01-01-18:00
$entry = str_replace('.md','',$entry);
// firstly I will try with hour and minute, if it fails, try only with Y-m-d
if(!($date = date_create_from_format($format, $entry))) {
$format = 'Y-m-d';
$date = date_create_from_format($format, $entry);
}
if($date) {
$label = $date->format("F Y");
array_push($archive,$label);
}
}
}
}
// ok, so delete duplicates and reverse order (we want nearest months first)
return array_unique(array_reverse($archive));
}
function get_archive($month) {
// $month is a label in format F Y
$format = 'F Y';
$date = date_create_from_format($format, $month);
$yearNumber = $date->format('Y');
$monthNumber = $date->format('m');
$files = array_diff(scandir(CONTENT_DIR), array('..', '.'));
$filter = $yearNumber."-".$monthNumber;
$filteredFiles = array_filter($files, function ($v) use($filter) {
return substr($v, 0, 7) == $filter;
});
return get_posts(CONTENT_DIR,$filteredFiles);
}
function get_single_post($dir,$entry) {
$entry = $entry.".md";
$files = array();
array_push($files,$entry);
return get_posts($dir,$files);
}
function get_page_posts($initial) {
$files = array_diff(scandir(CONTENT_DIR), array('..', '.'));
// reverse order
rsort($files);
$page = array_slice($files, $initial, POST_PER_PAGE);
return get_posts(CONTENT_DIR,$page);
}
function post_count() {
$files = array_diff(scandir(CONTENT_DIR), array('..', '.'));
return count($files);
}
function get_pages_list() {
$files = array_diff(scandir(PAGES_DIR), array('..', '.'));
$files = array_map(function ($entry) { return str_replace('.md','',$entry); }, $files);
return $files;
}
?>