-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathparse-problem-doc.pl
More file actions
executable file
·184 lines (149 loc) · 5.69 KB
/
Copy pathparse-problem-doc.pl
File metadata and controls
executable file
·184 lines (149 loc) · 5.69 KB
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
178
179
180
181
182
183
184
#!/usr/bin/env perl
=head1 NAME
parse-problem-doc.pl - Parse sample problem documentation.
=head1 SYNOPSIS
parse-problem-doc.pl [options]
Options:
-d|--problem-dir Directory containing sample problems to be parsed.
This defaults to the tutorial/sample-problems directory
in the PG root directory if not given.
-o|--out-dir Directory to save the output files to. (required)
-p|--pod-base-url Base URL location for POD on server. (required)
-s|--sample-problem-base-url
Base URL location for sample problems on server. (required)
-v|--verbose Give verbose feedback.
=head1 DESCRIPTION
Parse sample problem documentation.
=cut
use strict;
use warnings;
use experimental 'signatures';
use feature 'say';
my $pg_root;
BEGIN {
use Mojo::File qw(curfile);
$pg_root = curfile->dirname->dirname;
}
use lib "$pg_root/lib";
use Mojo::Template;
use File::Basename qw(basename);
use Getopt::Long;
use Pod::Usage;
use File::Copy qw(copy);
use Pod::Simple::Search;
use WeBWorK::PG::SampleProblemParser qw(parseSampleProblem generateMetadata);
my $problem_dir = "$pg_root/tutorial/sample-problems";
my ($out_dir, $pod_base_url, $sample_problem_base_url);
my $verbose = 0;
GetOptions(
"d|problem-dir=s" => \$problem_dir,
"o|out-dir=s" => \$out_dir,
"p|pod-base-url=s" => \$pod_base_url,
"s|sample-problem-base-url=s" => \$sample_problem_base_url,
"v|verbose" => \$verbose
);
pod2usage(2) unless $out_dir && $pod_base_url && $sample_problem_base_url;
my $mt = Mojo::Template->new(vars => 1);
my $template_dir = "$pg_root/tutorial/templates";
(undef, my $macro_files) = Pod::Simple::Search->new->inc(0)->survey("$pg_root/macros");
my $macro_locations = { map { basename($_) => ($_ =~ s!$pg_root/macros/!!r) =~ s/\.pl/.html/r } keys %$macro_files };
my @problem_types = qw(sample technique snippet);
$pod_base_url .= '/macros';
mkdir $out_dir unless -d $out_dir;
# Build a hash of all PG files for linking.
my $index_table = generateMetadata($problem_dir, macro_locations => $macro_locations, verbose => $verbose);
for (keys %$index_table) {
renderSampleProblem(
$_ =~ s/.pg$//r,
metadata => $index_table,
macro_locations => $macro_locations,
pod_base_url => $pod_base_url,
sample_problem_base_url => $sample_problem_base_url,
url_extension => '.html',
problem_dir => $problem_dir,
out_dir => $out_dir,
template_dir => $template_dir,
mt => $mt,
verbose => $verbose
);
}
sub renderSampleProblem ($filename, %global) {
my $relative_dir = $global{metadata}{"$filename.pg"}{dir};
my $path = "$global{problem_dir}/$relative_dir/$filename.pg";
say "Processing file: $path" if $global{verbose};
my $parsed_file = parseSampleProblem($path, %global);
mkdir "$global{out_dir}/$relative_dir" unless -d "$global{out_dir}/$relative_dir";
say "Printing to '$global{out_dir}/$relative_dir/$filename.html'" if $global{verbose};
open(my $html_fh, '>:encoding(UTF-8)', "$global{out_dir}/$relative_dir/$filename.html")
or die qq{Could not open output file "$global{out_dir}/$relative_dir/$filename.html": $!};
print $html_fh $global{mt}->render_file("$global{template_dir}/problem-template.mt",
{ %$parsed_file, %global, filename => "$filename.pg" });
close $html_fh;
# Write the code to a separate file
open(my $pg_fh, '>:encoding(UTF-8)', "$global{out_dir}/$relative_dir/$filename.pg")
or die qq{Could not open output file "$global{out_dir}/$relative_dir/$filename.pg": $!};
print $pg_fh $parsed_file->{code};
close $pg_fh;
say "Printing pg file to '$global{out_dir}/$relative_dir/$filename.pg'" if $global{verbose};
return;
}
# Output index files.
for (qw(categories subjects macros techniques)) {
my $options = {
metadata => $index_table,
template_dir => $template_dir,
out_dir => $out_dir,
mt => $mt,
verbose => $verbose,
};
my $params = buildIndex($_, %$options);
writeIndex($params, %$options);
}
sub buildIndex ($type, %options) {
my %labels = (
categories => 'Categories',
subjects => 'Subject Areas',
macros => 'Problems by Macro',
techniques => 'Problem Techniques'
);
my $list = {};
if ($type =~ /^(categories|subjects|macros)$/) {
for my $sample_file (keys %{ $options{metadata} }) {
for my $category (@{ $options{metadata}{$sample_file}{$type} }) {
$list->{$category}{ $options{metadata}{$sample_file}{name} } =
"$options{metadata}{$sample_file}{dir}/" . ($sample_file =~ s/\.pg$/.html/r);
}
}
} elsif ($type eq 'techniques') {
for my $sample_file (keys %{ $options{metadata} }) {
if (grep { $_ eq 'technique' } @{ $options{metadata}{$sample_file}{types} }) {
$list->{ $options{metadata}{$sample_file}{name} } =
"$options{metadata}{$sample_file}{dir}/" . ($sample_file =~ s/\.pg$/.html/r);
}
}
}
return {
label => $labels{$type},
list => $list,
type => $type,
output => "$options{out_dir}/$type.html"
};
}
sub writeIndex ($params, %options) {
say "Creating $params->{label} index" if $options{verbose};
if (open my $FH, '>:encoding(UTF-8)', $params->{output}) {
print $FH $options{mt}->render_file(
"$options{template_dir}/general-layout.mt",
{
sidebar => $options{mt}->render_file("$options{template_dir}/general-sidebar.mt", $params),
main_content => $options{mt}->render_file("$options{template_dir}/general-main.mt", $params),
active => $params->{type}
}
);
close $FH;
}
return;
}
# Copy the CSS file into the output directory.
copy("$pg_root/tutorial/css/sample-problem.css", $out_dir);
1;