-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-almanac-builder.php
126 lines (81 loc) · 3.73 KB
/
cli-almanac-builder.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
<?php
function logging( string $msg ) {
echo '[' . date( 'm/d/Y H:i:s' ) . '] ' . $msg . PHP_EOL;
}
function run( int $year ) {
logging( 'load names' );
$names = json_decode( file_get_contents( __DIR__ . '/config/names.json' ), true );
logging( 'load holiday data' );
$free = json_decode( file_get_contents( __DIR__ . '/config/free.json' ), true );
$holidays = [];
foreach( $free['holidays'] as $range ) {
for( $i = strtotime( $range['from'] ); $i <= strtotime( $range['to'] ); $i += 86400 ) {
$holidays[] = date( 'Y-m-d', $i );
}
}
logging( 'load moon data' );
$moon = json_decode( file_get_contents( __DIR__ . '/config/moon.json' ), true );
$moons = [];
foreach( $moon as $phase => $dates ) {
foreach( $dates as $date ) {
$moons[ $date ] = $phase;
}
}
logging( 'build almanac for year ' . $year );
for( $page = 0; $page <= 1; $page++ ) {
$pageno = $page + 1;
logging( 'build page ' . $pageno . ' of 2' );
$title = $year . ' – page ' . $pageno . '/2';
$content = '';
for( $month = $page * 6 + 1; $month <= $page * 6 + 6; $month++ ) {
$the_month = mktime( 0, 0, 0, $month, 1, $year );
$month_name = $names['month'][ $month - 1 ];
logging( 'build month ' . $month . ' of ' . $year );
$content .= '<month ' . strtolower( $month_name ) . '>' .
'<h2>' . $month_name . '</h2>';
for( $day = 1; $day <= date( 't', $the_month ); $day++ ) {
$the_day = mktime( 0, 0, 0, $month, $day, $year );
$week_day = date( 'l', $the_day );
$day_date = date( 'Y-m-d', $the_day );
$content .= '<day ' . strtolower( $week_day ) . (
in_array( $day_date, $holidays )
? ' holiday' : ''
) . (
array_key_exists( $day_date, $free['events'] )
? ' event' : ''
) . '>' .
'<number>' . date( 'd', $the_day ) . '</number>' .
'<name>' . $names['week'][ date( 'w', $the_day ) ] . '</name>' .
(
array_key_exists( $day_date, $free['events'] )
? '<event>' . $free['events'][ $day_date ] . '</event>' : ''
) .
'<space></space>' .
(
array_key_exists( $day_date, $moons )
? '<moon><img src="./../config/' . $moons[ $day_date ] . '.svg" width="10" height="10" /></moon>' : ''
) .
(
$week_day == 'Monday'
? '<week>' . date( 'W', $the_day ) . '</week>' : ''
) .
'</day>';
}
$content .= '</month>';
}
logging( 'output page ' . $pageno );
$output = str_replace(
[ '[TITLE]', '[CONTENT]' ],
[ $title, $content ],
file_get_contents( 'template.html' )
);
file_put_contents(
__DIR__ . '/output/' . $year . '-' . $pageno . '.html',
$output
);
}
}
if( isset( $argv[1] ) && preg_match( '/^([0-9]{4})$/', $argv[1] ) )
run( $argv[1] );
else die( 'Requires parameter 1 as numieric year, e.g. 2010' );
?>