From 2b254eaf7679b402b429fde5e4f1400c1eb7b38a Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Sat, 6 Apr 2013 18:32:34 -0400 Subject: [PATCH] refactoring of Hijri.pm cleaned and loosened guard regex. use /x for clarity. added wikipedia links for Hijri and Gregorian calendars. updated output format to match other conversion goodies. --- lib/DDG/Goodie/Hijri.pm | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/DDG/Goodie/Hijri.pm b/lib/DDG/Goodie/Hijri.pm index ed05e24df..61b71dc1c 100644 --- a/lib/DDG/Goodie/Hijri.pm +++ b/lib/DDG/Goodie/Hijri.pm @@ -7,17 +7,38 @@ zci answer_type => "date"; triggers any => 'hijri', 'gregorian'; +my $gregorian_calendar_wiki = + 'Gregorian calendar'; +my $hijri_calendar_wiki = + 'Hijri calendar'; + handle query_lc => sub { - return unless my ($gd, $gm, $gy, $gh) = $_ =~ /^(\d{0,2})(?:\/|\,)(\d{0,2})(?:\/|\,)(\d{3,4})\s+(?:on\s+the|in)\s+(?:gregorian|hijri)\s+(?:calendar|date|time)\s+is\s+(?:on\s+the|in)\s+(gregorian|hijri)\s+(?:calendar|date|time)$/; - last if($gd>31); - last if($gm>12); - my $hd; - my $hm; - my $hy; - ($hd, $hm, $hy) = g2h($gd, $gm, $gy) if ($gh eq 'hijri'); - ($hd, $hm, $hy) = h2g($gd, $gm, $gy) if ($gh eq 'gregorian'); - return $hd . '/' . $hm . '/' . $hy; + return unless my ($gd, $gm, $gy, $requested_calendar) = $_ =~ + /^ + (\d{0,2})(?:\/|,)(\d{0,2})(?:\/|,)(\d{3,4})\s+ + (?: + (?:on\s+the)\s+ + (?:gregorian|hijri)\s+ + (?:calendar|date|time)\s+ + is\s+ + )? + (?: + (?:(?:in|on|to)(?:\s+the|in)?)\s+ + )? + (gregorian|hijri)\s* + (?:calendar|date|time|years|months|days)? + $/x; + return unless ($gd<31 and $gm<12); + my ($hd, $hm, $hy) = $requested_calendar eq 'hijri' ? + g2h($gd, $gm, $gy) : h2g($gd, $gm, $gy); + my $input_date = "$gd/$gm/$gy"; + my $converted_date = "$hd/$hm/$hy"; + return "$input_date on the " + . ($requested_calendar eq 'hijri' ? + "$gregorian_calendar_wiki is $converted_date on the $hijri_calendar_wiki" : + "$hijri_calendar_wiki is $converted_date on the $gregorian_calendar_wiki"); }; + 1;