-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablegen.pl
executable file
·177 lines (142 loc) · 5.09 KB
/
tablegen.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl
## @file
# This file contains the course timeline table generator script.
#
# This script uses the table and link definition files for a given course
# to generate a course timeline table (showing what happens in each week,
# with links to supporting content as needed).
#
# To generate a table for a course, run this script with the course code
# and academic year:
#
# ./tablegen.pl -c comp37111 -y AY1415
#
# This will print a HTML page containing the course timeline table to
# stdout.
#
# To obtain a list of known courses and supported academic years, supply
# the -l option on the command line:
#
# ./tablegen.pl -l
#
# If -l is set, the script will print the available courses and years,
# and exit.
#
# @author Chris Page <[email protected]>
use utf8;
use v5.12;
use FindBin;
# Work out where the script is, so module and resource loading can work.
my $scriptpath;
BEGIN {
# This handles tainting, horribly permissively
if($FindBin::Bin =~ /^(.*)$/) {
$scriptpath = $1;
}
}
# Add the support modules to the load path
use lib "$scriptpath/webperl";
use lib "$scriptpath/modules";
# Custom modules to support generation
use AcademicYear;
use TableGen;
use Webperl::Template;
use Webperl::Utils qw(path_join save_file);
use CSS::Inliner;
# Standard perl modules
use Getopt::Long;
use Pod::Usage;
# ============================================================================
# Support functions
## @fn void list_courses($acyear)
# Print a list of available courses to stdout and then exit.
#
# @param acyear A reference to an AcademicYear object.
sub list_courses {
my $acyear = shift;
opendir(COURSES, path_join($scriptpath, "courses"))
or die "Unable to open courses directory '".path_join($scriptpath, "courses")."': $!\n";
my @entries = readdir(COURSES);
closedir(COURSES);
print "Available courses:\n";
foreach my $course (@entries) {
next if($course =~ /^\./); # ignore dotfiles.
print "\t$course\n";
}
print "\nAvailable years:\n";
my $years = $acyear -> available_years();
foreach my $year (@{$years}) {
print "\t$year\n";
}
exit;
}
# ============================================================================
# The code that actually Does Stuff.
# default setup variables
my $course = ''; # which course is the user interested in?
my $year = ''; # which year should the timetable be generated for?
my $list = 0; # Show a list of courses and years?
my $outfile = ''; # write the output to a file?
my $inline = 0; # Should css be inlined?
my $help = 0; # Show the help documentation
my $man = 0; # Print the full man page
# Parse the command line options
GetOptions('c|course:s' => \$course,
'y|year=s' => \$year,
'l|list!' => \$list,
'i|inline-css!' => \$inline,
'o|output:s' => \$outfile,
'h|help|?' => \$help,
'm|man' => \$man)
or pod2usage(2);
pod2usage(-verbose => 1) if($help || ((!$course || !$year) && !$list && !$man));
pod2usage(-exitstatus => 0, -verbose => 2) if($man);
# Build the objects needed to generate tables
# AcademicYear handles all things related to years, semesters, and weeks.
my $acyear = AcademicYear -> new(yearfile => path_join($scriptpath, "config", "years.xml"))
or die "Initialisation error: ".$Webperl::SystemModule::errstr."\n";
# Template handle loading and processing of template files
my $template = Webperl::Template -> new(basedir => path_join($scriptpath, "templates"),
langdir => '');
# TableGen does the actual table generation work
my $tablegen = TableGen -> new(acyear => $acyear,
template => $template,
basedir => $scriptpath);
# Handle listing if requested (this will not return)
list_courses($acyear) if($list);
# Explicitly load the course, rather than relying on TableGen's constructor
# to ensure that errors are caught properly.
$tablegen -> load_course($course)
or die "Unable to load course '$course': ".$tablegen -> errstr()."\n";
# Build the table page.
my $table = $tablegen -> generate($year);
# inline if needed
if($inline) {
my $inliner = new CSS::Inliner();
$inliner -> read({ html => $table });
$table = $inliner -> inlinify();
}
# If the user has specified an output file, use that rather
# than printing to stdout.
if($outfile) {
save_file($outfile, $table);
} else {
binmode STDOUT,":utf8";
print $table;
}
__END__
=head1 NAME
tablegen.pl - Generate course timeline tables
=head1 SYNOPSIS
tablegen.pl [options]
Options:
-h, -?, --help Show a brief help message.
-m, --man Show full documentation.
-c, --course The ID of the course to generate a table for.
-y, --year The academic year to generate the table for.
-o, --output The name of the file to write the table to (if not set,
the table is written to sdtout)
-i, --inline-css Inline any css as style attributes.
-l, --list List the available courses, and exit.
=head1 OPTIONS
=cut