[pmwiki-users] How to create markup macros?

Patrick R. Michaud pmichaud at pobox.com
Wed Feb 7 08:11:34 CST 2007


On Tue, Feb 06, 2007 at 09:39:35PM -0600, Harry Forsdick wrote:
>    I have mastered some parts of extending pmWiki markup, but there is one
>    simple case that I can't seem to get to work.
>    ...
>    ## NAVPREVNEXT
>    Markup('navprevnext', 'fulltext', '/\\(:navprevnext +(.*?):\\)/e',
>    "GenerateNavPrevNextCode('$1')");
>    function GenerateNavPrevNextCode($DateStr)
>    {
>      $date = strtotime($DateStr);
>      $prev = $date - 86400;
>      $next = $date + 86400;
> 
>      $result = "||[[Main." . date("DMd",$prev) . "|<- " . date("l, F j",
>    $prev) . "]] ||  [[Main." . date("DMd",$next) . "|" . date("l, F j",
>    $next) . " ->]]||";
> 
>      return Keep($result);
>    }

Dominique Faure already noted that the Keep() call at the end of
the markup is preventing PmWiki from doing any further processing.
Just to add a couple of other useful notes:

1.  To get angle brackets to appear in the output, use &lt; and &gt;
instead of '<' and '>' in the output string.  

2.  Adding or subtracting a time offset of 86400 doesn't always result
in "tomorrow" or "yesterday".  Depending on your system's notion of
the local time zone, some days may be only 23 hours long, while others
are 25 hours long.  (Don't feel too bad, though, long ago I made the 
same mistake in a financial application I wrote for a client, such
that it ended up missing/adding days in April/October.  That's why
I'm a bit sensitive to it.  :-)

For example, consider the script [1]:

    $day = 'Jan 31';
    $today = strtotime($day);
    $tomorrow = $today + 86400;
    $yesterday = $today - 86400;
    echo $day, ":\n";
    echo "  Yesterday: ", date('D M d', $yesterday), "\n";
    echo "  Today    : ", date('D M d', $today), "\n";
    echo "  Tomorrow : ", date('D M d', $tomorrow), "\n";

This works fine, producing the expected

    Jan 31:
      Yesterday: Tue Jan 30
      Today    : Wed Jan 31
      Tomorrow : Thu Feb 01

But see what happens for 'Nov 4' and 'Mar 12':

    Nov 4:
      Yesterday: Sat Nov 03
      Today    : Sun Nov 04
      Tomorrow : Sun Nov 04       # oops!

    Mar 12:
      Yesterday: Sat Mar 10       # oops!
      Today    : Mon Mar 12
      Tomorrow : Tue Mar 13

If you know that the strings given will always be whole days 
at 00:00 hours (midnight), then a workaround is to add 93600 
(26 hours) and subtract 79200 (22 hours) to avoid any effects 
of daylight savings time.

In the general case, the way to reliably compute values for
yesterday/tomorrow in PHP is to do:

    $today = strtotime('Mar 12');
    $tomorrow = strtotime('+1 day', $today);
    $yesterday = strtotime('-1 day', $today);

There's also a mechanism for doing this using localtime/mktime,
similar to what one would do in C, but using PHP's strtotime()
function makes it easy.  (And I learned some new things about
strtotime today -- thanks!)

Hope this helps!

Pm

1.  http://www.pmichaud.com/sandbox/daytest.php



More information about the pmwiki-users mailing list