| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | timeline - Program to draw an SVG timeline diagram from events defined in a file | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | $ timeline list_of_events.txt > timeline.svg | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | A program to produce SVG timeline diagrams from events defined in a file. | ||||||||||
| 14 | |||||||||||
| 15 | This program reads data from a file given on the command line and writes an | ||||||||||
| 16 | SVG document to C<STDOUT>. | ||||||||||
| 17 | |||||||||||
| 18 | =head2 Input Data | ||||||||||
| 19 | |||||||||||
| 20 | The program reads input data from a file where each line is whitespace-separated | ||||||||||
| 21 | (spaces or tabs). Each line in the file represents a single event in the | ||||||||||
| 22 | timeline - which will be represented by a coloured rectangle in the diagram. | ||||||||||
| 23 | |||||||||||
| 24 | Each line requires three pieces of data: a text string (to be used as the label | ||||||||||
| 25 | for the event), a start year and an end year. The start and end years can be in | ||||||||||
| 26 | C<YYYY>, C<YYYY-MM>, or C<YYYY-MM-DD> format. There can also be an optional | ||||||||||
| 27 | fourth value which is a colour for the background of the rectangle representing | ||||||||||
| 28 | the event. This colour can be a colour name (e.g. C<red>) or an RGB string as | ||||||||||
| 29 | recognised by SVG (e.g. C<rgb(255,0,0)>). | ||||||||||
| 30 | |||||||||||
| 31 | An example input file might be: | ||||||||||
| 32 | |||||||||||
| 33 | World War I 1914 1918 | ||||||||||
| 34 | World War II 1939 1945 | ||||||||||
| 35 | |||||||||||
| 36 | Or, with colours included: | ||||||||||
| 37 | |||||||||||
| 38 | World War I 1914 1918 red | ||||||||||
| 39 | World War II 1939 1940 blue | ||||||||||
| 40 | |||||||||||
| 41 | =head2 Command Line Options | ||||||||||
| 42 | |||||||||||
| 43 | The program takes three command line options. This are all options. | ||||||||||
| 44 | |||||||||||
| 45 | =over 4 | ||||||||||
| 46 | |||||||||||
| 47 | =item years_per_grid | ||||||||||
| 48 | |||||||||||
| 49 | The number of years between grid lines in the output. The default value | ||||||||||
| 50 | is ten, which gives a line every decade. | ||||||||||
| 51 | |||||||||||
| 52 | =item width | ||||||||||
| 53 | |||||||||||
| 54 | This will be inserted as the C<width> attribute of the top-level SVG | ||||||||||
| 55 | element in the output. The default is C<100%>. | ||||||||||
| 56 | |||||||||||
| 57 | =item height | ||||||||||
| 58 | |||||||||||
| 59 | This will be inserted as the C<height> attribute of the top-level SVG | ||||||||||
| 60 | element in the output. The default is C<100%>. | ||||||||||
| 61 | |||||||||||
| 62 | =back | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 5 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | |||||||||||
| 70 | 5 | ▶ | use List::Util qw[min max]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 5 | ▶ | use Getopt::Long; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 5 | ▶ | use SVG::Timeline; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 5 | my %opts; | |||||||||
| 75 | |||||||||||
| 76 | 5 | GetOptions( | |||||||||
| 77 | \%opts, | ||||||||||
| 78 | 'years_per_grid=i', | ||||||||||
| 79 | 'width=s', | ||||||||||
| 80 | 'height=s', | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | 5 | my $timeline = SVG::Timeline->new(%opts); | |||||||||
| 84 | |||||||||||
| 85 | 5 | ▶ | while (<>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 10 | chomp; | |||||||||
| 87 | 10 | s/^\s+|\s+$//g; | |||||||||
| 88 | 10 | ▶ | next unless length; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | 8 | my %rec; | |||||||||
| 91 | 8 | ▶ | if (/^(.+?)\s+(\d{4}(?:-\d\d(?:-\d\d)?)?)\s+(\d{4}(?:-\d\d(?:-\d\d)?)?)\s*(\S+)?\s*$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 8 | @rec{qw[text start end colour]} = ($1, $2, $3, $4); | |||||||||
| 93 | } else { | ||||||||||
| 94 | 0 | warn "Skipping unrecognised line: $_\n"; | |||||||||
| 95 | 0 | next; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 8 | $timeline->add_event(\%rec); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 5 | say $timeline->draw; | |||||||||
| 102 | |||||||||||
| 103 | =head1 AUTHOR | ||||||||||
| 104 | |||||||||||
| 105 | Dave Cross <dave@perlhacks.com> | ||||||||||
| 106 | |||||||||||
| 107 | =head1 COPYRIGHT AND LICENCE | ||||||||||
| 108 | |||||||||||
| 109 | Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved. | ||||||||||
| 110 | |||||||||||
| 111 | This library is free software; you can redistribute it and/or modify it | ||||||||||
| 112 | under the same terms as Perl itself. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||