Skip to content

Commit

Permalink
Simple calendar on the index page with link to the article would be nice
Browse files Browse the repository at this point in the history
  • Loading branch information
dseg committed Jan 29, 2015
1 parent f70f599 commit 42ac61a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
67 changes: 67 additions & 0 deletions diary/themes/default/index.cst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,73 @@
<?cs /each ?>
</ul>
</li>
<li>
<?cs # [calendar] ?>
<?cs # -------------------------------------------------------------- ?>
<?cs # macros ?>
<?cs # -------------------------------------------------------------- ?>
<?cs def:decorate_day(html, base_uri, entries, y, m, d) ?><?cs
set:entry_name = y + "-" + m + "-" ?><?cs
if:d < 10 ?><?cs
set:entry_name = entry_name + "0" + d ?><?cs
else ?><?cs
set:entry_name = entry_name + d ?><?cs
/if ?><?cs
set:html = "" ?><?cs
each:entry = entries ?><?cs
var:entry
?><?cs
if:entry.name == entry_name ?><?cs
set:html = "<a href='" + base_uri + "/" + entry_name + "'>" + d + "</a>" ?><?cs
/if ?><?cs
/each ?><?cs
if:!html ?><?cs
set:html = d ?><?cs
/if ?><?cs
/def ?>

<?cs def:output_day(lastdayofmon, dow_1stdayofmon, idx, offset, y, m, entries, color) ?><?cs
set:html = "" ?><?cs
if:(idx - dow_1stdayofmon + offset > 0) && (idx <= lastdayofmon) ?><?cs
call:decorate_day(html, diary.uri, entries, y, m, (idx - dow_1stdayofmon + offset)) ?><?cs
/if ?>
<td><span style='color:<?cs alt:color ?>inherit<?cs /alt ?>'><?cs var:html ?></span></td><?cs
/def ?>
<?cs # -------------------------------------------------------------- ?>
<?cs # markups ?>
<?cs # -------------------------------------------------------------- ?>
<table style="float:left; width:150px; margin-left:1em; text-align:center">
<tbody>
<tr style="background:#aaaaff">
<th><?cs if:?cal.prev_month_url ?><a href="<?cs var:cal.prev_month_url ?>">«</a><?cs else ?>&nbsp;<?cs /if ?></th>
<th colspan="5"><?cs var:cal.year ?>年<?cs var:cal.month+0 ?>月</th>
<th><?cs if:?cal.next_month_url ?><a href="<?cs var:cal.next_month_url ?>">»</a><?cs else ?>&nbsp;<?cs /if ?></th>
</tr>
<tr style="background:#eeeeff">
<td><span style="color:red">日</span></td><td>月</td><td>火</td><td>水</td><td>木</td><td>金</td><td><span style="color:deepskyblue">土</span></td>
</tr><?cs
loop:i = #0, #41, #7 ?><?cs
if:i == #0 ?><tr><?cs
/if ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #1, cal.year, cal.month, index, "red") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #2, cal.year, cal.month, index, "") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #3, cal.year, cal.month, index, "") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #4, cal.year, cal.month, index, "") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #5, cal.year, cal.month, index, "") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #6, cal.year, cal.month, index, "") ?><?cs
call:output_day(cal.lastdayofmonth, cal.dayofweek_1stdayofmonth, i, #7, cal.year, cal.month, index, "deepskyblue") ?><?cs
if:(i%#7==0) && (i!=#35) ?>
</tr>
<tr><?cs
elif:(i%#7==0) && (i==#35) ?>
</tr><?cs
/if ?><?cs
/loop ?>
</tbody>
</table>
<?cs # [/calendar] ?>

</li>
</ul>
</div>
<hr />
Expand Down
42 changes: 41 additions & 1 deletion mod_diary.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "ClearSilver.h"

#include "diary.h"
#include <time.h>

#define INDEX_HDF "index.hdf"

Expand All @@ -77,25 +78,64 @@ typedef struct {
int github_flavoured;
} diary_conf;

typedef struct {
int year;
char month[3];
char day[3];
int dayofweek_1stdayofmonth;
int lastdayofmonth;
} calendar_info;

static NEOERR *diary_cs_render_cb(void *ctx, char *s)
{
ap_rputs(s, (request_rec *)ctx);
return NULL;
}

static void diary_set_calendar_info(calendar_info *cal)
{
time_t now;
struct tm *tm;
char buf[4];
const int dayofmonthes[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

/* Setup the calendar data */
now = time(NULL);
tm = localtime(&now);

cal->year = tm->tm_year + 1900;
cal->lastdayofmonth = dayofmonthes[tm->tm_mon];
if(tm->tm_mon == 1 && cal->year%4 == 0 && cal->year%100 != 0 || cal->year%400 == 0)
++cal->lastdayofmonth;
sprintf(cal->month, "%02d", tm->tm_mon + 1);
sprintf(cal->day, "%02d", tm->tm_mday);
/* Get the day of week of the 1st day of this month */
tm->tm_mday = 1;
strftime(buf, 4, "%w", tm); /* The day of week as a decimal (sunday is 0) */
cal->dayofweek_1stdayofmonth = atoi(buf);
}

static int diary_handle_index(request_rec *r, diary_conf *conf)
{
HDF *hdf;
CSPARSE *cs;
NEOERR *cs_err;
STRING cs_err_str;
calendar_info cal;

hdf_init(&hdf);
hdf_set_int_value(hdf, "index", 1);
hdf_set_value(hdf, "hdf.loadpaths.1", conf->path);
hdf_set_value(hdf, "diary.title", conf->title);
hdf_set_value(hdf, "diary.uri", conf->uri);

diary_set_calendar_info(&cal);
hdf_set_int_value(hdf, "cal.year", cal.year);
hdf_set_value(hdf, "cal.month", cal.month);
hdf_set_value(hdf, "cal.today", cal.day);
hdf_set_int_value(hdf, "cal.lastdayofmonth", cal.lastdayofmonth);
hdf_set_int_value(hdf, "cal.dayofweek_1stdayofmonth", cal.dayofweek_1stdayofmonth);

cs_err = hdf_read_file(hdf, INDEX_HDF);
if(cs_err){
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "cannot read index.hdf.");
Expand Down Expand Up @@ -178,7 +218,7 @@ static int diary_handle_feed_rss(request_rec *r, diary_conf *conf)
string_init(&cs_err_str);
nerr_error_string(cs_err, &cs_err_str);
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"error in cs_parse_file(): %s", cs_err_str.buf);
"error in cs_parse_string(): %s", cs_err_str.buf);
cs_destroy(&cs);
hdf_destroy(&hdf);
return HTTP_INTERNAL_SERVER_ERROR;
Expand Down

0 comments on commit 42ac61a

Please sign in to comment.