| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # Created on: 2006-05-05 22:44:23 | ||
| 4 | # Create by: ivan | ||
| 5 | # $Id$ | ||
| 6 | # # $Revision$, $HeadURL$, $Date$ | ||
| 7 | # # $Revision$, $Source$, $Date$ | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use version; | ||
| 12 | use Scalar::Util; | ||
| 13 | use Getopt::Long; | ||
| 14 | use Pod::Usage; | ||
| 15 | use Config::Std; | ||
| 16 | use Data::Dumper qw/Dumper/; | ||
| 17 | use SVG::Calendar; | ||
| 18 | use Path::Tiny; | ||
| 19 | use FindBin qw/$Bin/; | ||
| 20 | |||
| 21 | our $VERSION = version->new('0.3.13'); | ||
| 22 | |||
| 23 | my %option = ( | ||
| 24 | moon => {}, | ||
| 25 | date => {}, | ||
| 26 | ical => {}, | ||
| 27 | page => {}, | ||
| 28 | image => {}, | ||
| 29 | height => 0.5, | ||
| 30 | config => "$ENV{HOME}/.svgcal", | ||
| 31 | path => undef, | ||
| 32 | template => undef, | ||
| 33 | verbose => 0, | ||
| 34 | man => 0, | ||
| 35 | help => 0, | ||
| 36 | VERSION => 0, | ||
| 37 | ); | ||
| 38 | |||
| 39 | if ( !@ARGV ) { | ||
| 40 | pod2usage( -verbose => 1 ); | ||
| 41 | } | ||
| 42 | |||
| 43 | main(); | ||
| 44 | exit 0; | ||
| 45 | |||
| 46 | sub main { | ||
| 47 | Getopt::Long::Configure('bundling'); | ||
| 48 | GetOptions( | ||
| 49 | \%option, | ||
| 50 | 'moon|m=s%', | ||
| 51 | 'date|d=s%', | ||
| 52 | 'ical|c=s%', | ||
| 53 | 'page|p=s%', | ||
| 54 | 'image|i=s%', | ||
| 55 | 'calendar_height|height|h=s', | ||
| 56 | 'config|C=s', | ||
| 57 | 'path|P=s', | ||
| 58 | 'template|t=s', | ||
| 59 | 'out|o=s', | ||
| 60 | 'save|s', | ||
| 61 | 'show-template', | ||
| 62 | 'verbose|v!', | ||
| 63 | 'man', | ||
| 64 | 'help', | ||
| 65 | 'VERSION' | ||
| 66 | ) or pod2usage(2); | ||
| 67 | |||
| 68 | if ( $option{VERSION} ) { | ||
| 69 | print "svgcal.pl Version = $VERSION\n"; | ||
| 70 | exit 1; | ||
| 71 | } | ||
| 72 | elsif ( $option{man} ) { | ||
| 73 | pod2usage( -verbose => 2 ); | ||
| 74 | } | ||
| 75 | elsif ( $option{help} ) { | ||
| 76 | pod2usage( -verbose => 1 ); | ||
| 77 | } | ||
| 78 | elsif ( !$option{date} ) { | ||
| 79 | warn "No dates specified, nothing to do!\n"; | ||
| 80 | pod2usage( -verbose => 1 ); | ||
| 81 | } | ||
| 82 | |||
| 83 | # do stuff here | ||
| 84 | my $cal; | ||
| 85 | |||
| 86 | if ( $option{save} && !-f $option{config} ) { | ||
| 87 | open my $fh, '>', $option{config} or warn "Cannot create the configuration file '$option{config}': $!"; ## no critic | ||
| 88 | if ($fh) { | ||
| 89 | print {$fh} "\n" or print {*STDERR} "Cannot print to file '$option{config}': $!\n"; | ||
| 90 | close $fh or print {*STDERR} "Cannot close file '$option{config}': $!\n"; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | if (@ARGV) { | ||
| 95 | for my $img (grep {/=/} @ARGV) { | ||
| 96 | my ($month, $src) = split /=/, $img, 2; | ||
| 97 | $option{image}{$month} = $src; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | if ( | ||
| 102 | $option{image}{dir} | ||
| 103 | && -d $option{image}{dir} | ||
| 104 | && $option{date} | ||
| 105 | && $option{date}{year} | ||
| 106 | ) { | ||
| 107 | my $year = $option{date}{year}; | ||
| 108 | for my $i ( 1 .. 12 ) { | ||
| 109 | my $month = $i < 10 ? "0$i" : $i; | ||
| 110 | $option{image}{"$year-$month"} = "$option{image}{dir}/$month.jpg"; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | if ( $option{'show-template'} ) { | ||
| 115 | show_template(); | ||
| 116 | } | ||
| 117 | |||
| 118 | my %config = get_config(); | ||
| 119 | $cal = SVG::Calendar->new(%config); | ||
| 120 | |||
| 121 | if ( $option{verbose} ) { | ||
| 122 | $cal->{verbose} = $option{verbose}; | ||
| 123 | } | ||
| 124 | |||
| 125 | if ( $option{date}{month} ) { | ||
| 126 | $cal->output_month( $option{date}{month}, $option{out} || q/-/ ); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | die "No page output file name base specified!\n" if !$option{out}; ## no critic | ||
| 130 | |||
| 131 | if ( $option{date}{year} ) { | ||
| 132 | $cal->output_year( $option{date}{year}, $option{out} ); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $cal->output_year( $option{date}{start}, $option{date}{end}, $option{out} ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub show_template { | ||
| 143 | |||
| 144 | require File::ShareDir; | ||
| 145 | my $dir = path( eval { File::ShareDir::dist_dir('SVG-Calendar') } || ( $Bin, '..', 'templates' ) ); | ||
| 146 | print $dir->path('calendar.svg')->slurp; | ||
| 147 | |||
| 148 | return exit 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub get_config { | ||
| 152 | if ( !-f $option{config} ) { | ||
| 153 | template_config($option{config}); | ||
| 154 | } | ||
| 155 | |||
| 156 | read_config $option{config} => my %config; | ||
| 157 | |||
| 158 | OPTION: | ||
| 159 | for my $key ( keys %option ) { | ||
| 160 | next OPTION if !ref $option{$key}; | ||
| 161 | |||
| 162 | for my $subkey ( keys %{ $option{$key} } ) { | ||
| 163 | |||
| 164 | # override the config file settings | ||
| 165 | $config{$key}{$subkey} = $option{$key}{$subkey}; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | if ( $option{page} ) { | ||
| 170 | $config{page} = $option{page}; | ||
| 171 | } | ||
| 172 | if ( $option{path} ) { | ||
| 173 | $config{path} = $option{path}; | ||
| 174 | } | ||
| 175 | if ( $option{template} ) { | ||
| 176 | $config{template} = $option{template}; | ||
| 177 | } | ||
| 178 | if ( $option{image} ) { | ||
| 179 | $config{image} = $option{image}; | ||
| 180 | } | ||
| 181 | |||
| 182 | if ( $option{save} && -f $option{config} ) { | ||
| 183 | write_config %config; | ||
| 184 | } | ||
| 185 | |||
| 186 | return %config; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub template_config { | ||
| 190 | my ($file) = @_; | ||
| 191 | |||
| 192 | open my $fh, '>', $file or return; | ||
| 193 | |||
| 194 | print {$fh} <<"CONFIG"; | ||
| 195 | # this section configures the information about the images | ||
| 196 | [image] | ||
| 197 | |||
| 198 | # src | ||
| 199 | # This allows specifying one image file. | ||
| 200 | # src: image.jpg | ||
| 201 | |||
| 202 | # YYYY-MM | ||
| 203 | # This is the format for specifying a specific month's image | ||
| 204 | #2012-01: car.gif | ||
| 205 | #2012-02: horse.tiff | ||
| 206 | |||
| 207 | # dir | ||
| 208 | # This option allows the specifying of a directory to contain all the images | ||
| 209 | # for the months that calendars are going to be produced. The images should | ||
| 210 | # in the directory be specified as MM.[jpg|png] or month.[jpg|png] eg 01.jpg | ||
| 211 | # or January.png | ||
| 212 | #dir: . | ||
| 213 | |||
| 214 | # The page section sets up the SVG document size | ||
| 215 | [page] | ||
| 216 | |||
| 217 | # page | ||
| 218 | # This allows you to specify specific page sizes eg A4. Known page sizes | ||
| 219 | # include A0, A1, A2, A3, A4, A5, A6 | ||
| 220 | #page: A4 | ||
| 221 | |||
| 222 | # height | ||
| 223 | # If the svg document size is not one of the predefined sizes you can use | ||
| 224 | # this setting to set the height of the document. If no units are specified | ||
| 225 | # the height is in pixels. | ||
| 226 | #height: 200mm | ||
| 227 | |||
| 228 | # width | ||
| 229 | # Like height you set the SVG document size to an arbitart width | ||
| 230 | #width: 10cm | ||
| 231 | |||
| 232 | # This section controlls the displaying of the moon on the produced calendars | ||
| 233 | [moon] | ||
| 234 | |||
| 235 | # display | ||
| 236 | # Turn on displaying of the moon (requires Astro::Coord::ECI::Moon or | ||
| 237 | # Astro::MoonPhase to be installed) | ||
| 238 | #display: 1 | ||
| 239 | display: 0 | ||
| 240 | |||
| 241 | # quarters | ||
| 242 | # Toggles weather to only display the moon for the various quarters or to | ||
| 243 | # show what phase the moon is in for every day. | ||
| 244 | #quarters: 1 | ||
| 245 | quarters: 0 | ||
| 246 | |||
| 247 | # vpos | ||
| 248 | # Specifies weather the moon should be aligned with the top or bottom of the | ||
| 249 | # day's square. | ||
| 250 | #vpos: top | ||
| 251 | |||
| 252 | # hpos | ||
| 253 | # Specifies weather the moon should be aligned with the left or right hand | ||
| 254 | # side of the day's square. | ||
| 255 | #hpos: right | ||
| 256 | |||
| 257 | # xoffset, yoffset | ||
| 258 | # These parameters specify the x and y offsets of the moon, the units are | ||
| 259 | # relative units (ie page size independant) with the size of the dates square | ||
| 260 | # being approximatly 10x16 | ||
| 261 | #xoffset: 10 | ||
| 262 | #yoffset: 10 | ||
| 263 | |||
| 264 | # radius | ||
| 265 | # This specifies the size of the moon in the date's square | ||
| 266 | #radius: 1.0 | ||
| 267 | |||
| 268 | # image | ||
| 269 | # This option specifies an image file that will be used as the background for | ||
| 270 | # the moon. | ||
| 271 | #image: moon.jpg | ||
| 272 | |||
| 273 | CONFIG | ||
| 274 | |||
| 275 | close $fh; | ||
| 276 | |||
| 277 | return; | ||
| 278 | } | ||
| 279 | |||
| 280 | __DATA__ | ||
| 281 | |||
| 282 | =head1 NAME | ||
| 283 | |||
| 284 | svgcal.pl - Creates the pages for a calendar in SVG format | ||
| 285 | |||
| 286 | =head1 VERSION | ||
| 287 | |||
| 288 | This documentation refers to svgcal.pl version 0.3.13. | ||
| 289 | |||
| 290 | =head1 SYNOPSIS | ||
| 291 | |||
| 292 | svgcal.pl [option] --date {see below} | ||
| 293 | svgcal.pl [--verbose | --VERSION | --help | --man] | ||
| 294 | |||
| 295 | OPTIONS: | ||
| 296 | -o --out=str The base file name when out putting multiple months | ||
| 297 | -d --date Parameters that control the months displaied on the | ||
| 298 | calendar | ||
| 299 | start=YYYY-MM Start month | ||
| 300 | end=YYYY-MM End month | ||
| 301 | year=YYYY Year to base the whole calendar on (Default next year) | ||
| 302 | month=YYYY-MM Display only this month | ||
| 303 | -m --moon Moon parameters | ||
| 304 | display=1 Display the moon on individual days | ||
| 305 | quarters=1|0 Show only whole quarters | ||
| 306 | vpos=top|bottom Specifies which quadrent the moon should appear in | ||
| 307 | hpos=left|right as above | ||
| 308 | xoffset=num Precisly set the x position of moon | ||
| 309 | yoffset=num Precisly set the y position of moon | ||
| 310 | radius=n% The radius as a percentage of day box width | ||
| 311 | image=url An image of the moon to use as the fill background of | ||
| 312 | the moon | ||
| 313 | -c --ical ICal parameters | ||
| 314 | -p --page Specify a page type or a height or width of the page | ||
| 315 | page=A0..A6 The page type | ||
| 316 | height=size The page height (with optional units) | ||
| 317 | width=size The page width (with optional units) | ||
| 318 | -i --image Specifies the images to be displayed on the calendar | ||
| 319 | src=file This image will be used for any image with out a specific | ||
| 320 | month image. | ||
| 321 | dir=directory Finds images in the directory named after the months | ||
| 322 | (eg 01-12 or January-December) | ||
| 323 | YYYY-MM=file Use this image for the specified month | ||
| 324 | -h --height The height on the page that the calendar shoud take up. | ||
| 325 | Either a fraction or a percent (Default 50%) | ||
| 326 | -C --config Location of the configuration file (Default ~/.svgcal) | ||
| 327 | -P --path=template path | ||
| 328 | Specify a colon seperated path to find templates in | ||
| 329 | -t --template=template_dir | ||
| 330 | The name of a template directory to use instead of the | ||
| 331 | default templates (expects to find a template there called | ||
| 332 | calendar.svg) | ||
| 333 | -s --save Save any other command line options to your config file | ||
| 334 | --show-template | ||
| 335 | Displays the default template used by SVG::Calendar, | ||
| 336 | this is useful if you want to change the default template | ||
| 337 | |||
| 338 | -v --verbose Show more detailed option | ||
| 339 | --version Prints the version information | ||
| 340 | --help Prints this help information | ||
| 341 | --man Prints the full documentation for svgcal.pl | ||
| 342 | |||
| 343 | =head1 DESCRIPTION | ||
| 344 | |||
| 345 | This script provides a command line interface to the SVG::Calendar library. Most | ||
| 346 | of the functionality is exposed here. | ||
| 347 | |||
| 348 | =head2 Configuration | ||
| 349 | |||
| 350 | To make configuration using this tool options can be saved to thee ~/.svgcal | ||
| 351 | file. The format of the configuration file is similar to INI files. The | ||
| 352 | easiest way to start using the configuration file is once you have set up | ||
| 353 | the options that you like use the --save option which will write the current | ||
| 354 | configuration to the file. If you use --save again the file should be updated | ||
| 355 | leaving comments in place. | ||
| 356 | |||
| 357 | =head1 SUBROUTINES/METHODS | ||
| 358 | |||
| 359 | =head1 DIAGNOSTICS | ||
| 360 | |||
| 361 | =head1 CONFIGURATION AND ENVIRONMENT | ||
| 362 | |||
| 363 | =head1 DEPENDENCIES | ||
| 364 | |||
| 365 | =head1 INCOMPATIBILITIES | ||
| 366 | |||
| 367 | =head1 BUGS AND LIMITATIONS | ||
| 368 | |||
| 369 | There are no known bugs in this module. | ||
| 370 | |||
| 371 | Please report problems to Ivan Wills (ivan.wills@gmail.com). | ||
| 372 | |||
| 373 | Patches are welcome. | ||
| 374 | |||
| 375 | =head1 AUTHOR | ||
| 376 | |||
| 377 | Ivan Wills - (ivan.wills@gmail.com) | ||
| 378 | |||
| 379 | =head1 LICENSE AND COPYRIGHT | ||
| 380 | |||
| 381 | Copyright (c) 2006-2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia 2077) | ||
| 382 | All rights reserved. | ||
| 383 | |||
| 384 | This module is free software; you can redistribute it and/or modify it under | ||
| 385 | the same terms as Perl itself. See L<perlartistic>. This program is | ||
| 386 | distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
| 387 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
| 388 | PARTICULAR PURPOSE. | ||
| 389 | |||
| 390 | =cut |