-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootify.pl
executable file
·1475 lines (1300 loc) · 41.5 KB
/
bootify.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -s
#*************************************************************************
#
# Program: bootify
# File: bootify.pl
#
# Version: V1.6
# Date: 21.02.17
# Function: Create a (set of) HTML page(s) using attractive
# Bootstrap layout from very simple HTML meta-markup
# So this beautifies and boostrapifies the pages
#
# Copyright: (c) Dr. Andrew C. R. Martin, UCL, 2015-2017
# Author: Dr. Andrew C. R. Martin
# Address: Institute of Structural and Molecular Biology
# Division of Biosciences
# University College
# Gower Street
# London
# WC1E 6BT
# EMail: [email protected]
#
#*************************************************************************
#
# This program is not in the public domain, but it may be copied
# according to the conditions laid out in the accompanying file
# COPYING.DOC
#
# The code may be modified as required, but any modifications must be
# documented so that the person responsible can be identified. If
# someone else breaks this code, I don't want to be blamed for code
# that does not work!
#
# The code may not be sold commercially or included as part of a
# commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
# Description:
# ============
#
#*************************************************************************
#
# Usage:
# ======
#
#*************************************************************************
#
# Revision History:
# =================
# V1.0 11.11.15 Original By: ACRM
# V1.1 11.11.15 Added quiz support
# V1.2 05.01.16 Added [link], [figure] and [flush/]
# V1.3 16.09.16 Added [home] and -force
# V1.4 17.10.16 Added open='true' option to [ai]
# V1.5 09.01.17 Added support for external style sheets with <link>
# V1.6 21.02.17 Added -local flag
#
#*************************************************************************
# Add the path of the executable to the library path
use FindBin;
use Cwd qw(abs_path);
use lib $FindBin::Bin;
# Or if we have a bin directory and a lib directory
#use FindBin;
#use lib abs_path("$FindBin::Bin/../lib");
use genquiz;
use strict;
#*************************************************************************
# The JQuery and Bootstrap distributions
$::jquery = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js";
$::bootstrapjs = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js";
$::bootstrapcss = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css";
$::bootstrapthemecss = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css";
$::bootstrapeot = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/fonts/glyphicons-halflings-regular.eot";
$::bootstrapsvg = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/fonts/glyphicons-halflings-regular.svg";
$::bootstrapttf = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/fonts/glyphicons-halflings-regular.ttf";
$::bootstrapwoff = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/fonts/glyphicons-halflings-regular.woff";
$::bootstrapwoff2 = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/fonts/glyphicons-halflings-regular.woff2";
#*************************************************************************
$::accordionCount = 0;
$::collapseCount = 0;
%::attribute = ();
$::local = defined($::local)?1:0;
UsageDie() if(defined($::h));
CleanupDie($::force) if(defined($::clean));
if(scalar(@ARGV))
{
WriteCSSandJS($::local); # Write CSS and JavaScript files
my @data = <>; # Read the input HTML file
# The title from the <title> tag
my $title = GetTitle(@data);
print "Title: $title\n" if(defined($::debug));
# Any style information
my $style = GetStyle(@data);
print "Style: $style\n" if(defined($::debug));
# Menu items from [page menu='xxx'] tags
my $aMenu = GetMenuItems(@data);
if(defined($::debug))
{
print "Menu: |";
foreach my $menu (@$aMenu)
{
print " $menu |";
}
print "\n";
}
# Taken from the [bigheading] <h1> or from [home] if specified
my ($homeMenu, $homeURL) = GetHomeMenu(@data);
print "Home menu item: $homeMenu\n" if(defined($::debug));
print "Home menu URL: $homeURL\n" if(defined($::debug));
# Split up the pages and create each one
my $aPages = GetPages(@data);
my $pageNum = 0;
foreach my $aPage (@$aPages)
{
my $lastPage = 0;
$lastPage = 1 if($pageNum == (scalar(@$aPages) - 1));
WritePage($pageNum, $title, $style, $homeMenu, $homeURL, $aMenu, $aPage, $lastPage);
$pageNum++;
}
}
else
{
UsageDie();
}
#*************************************************************************
#> void WriteCSSandJS(void)
# ------------------------
# Writes the supporting CSS and JavaScript files:
# mptheme.css - static menu theme support
# mpcss.css - special extras for makepages
# mpautotooltip.js - activate tooltips
#
# 11.11.15 Original By: ACRM
#
sub WriteCSSandJS
{
my ($local) = @_;
my $share = Cwd::abs_path("$FindBin::Bin/share");
`cp $share/bootify/throbber.gif .`;
`cp $share/bootify/mptheme.css .`;
`cp $share/bootify/mpcss.css .`;
`cp $share/bootify/mpautotooltip.js .`;
# Stash copies of the JQuery and Bootstrap files if they don't exist
if($local)
{
CheckAndGetSharedFile("$share/bootify/jquery.min.js",
$::jquery);
CheckAndGetSharedFile("$share/bootify/bootstrap.min.js",
$::bootstrapjs);
CheckAndGetSharedFile("$share/bootify/bootstrap.min.css",
$::bootstrapcss);
CheckAndGetSharedFile("$share/bootify/bootstrap-theme.min.css",
$::bootstrapthemecss);
CheckAndGetSharedFile("$share/bootify/glyphicons-halflings-regular.eot",
$::bootstrapeot);
CheckAndGetSharedFile("$share/bootify/glyphicons-halflings-regular.svg",
$::bootstrapsvg);
CheckAndGetSharedFile("$share/bootify/glyphicons-halflings-regular.ttf",
$::bootstrapttf);
CheckAndGetSharedFile("$share/bootify/glyphicons-halflings-regular.woff",
$::bootstrapwoff);
CheckAndGetSharedFile("$share/bootify/glyphicons-halflings-regular.woff2",
$::bootstrapwoff2);
if(! -d "./bootifyshare")
{
`mkdir -p ./bootifyshare/js`;
`mkdir -p ./bootifyshare/css`;
`mkdir -p ./bootifyshare/fonts`;
}
CopyFileLocalOrWeb("$share/bootify", "jquery.min.js",
$::jquery, "./bootifyshare/js");
CopyFileLocalOrWeb("$share/bootify", "bootstrap.min.js",
$::bootstrapjs, "./bootifyshare/js");
CopyFileLocalOrWeb("$share/bootify", "bootstrap.min.css",
$::bootstrapcss, "./bootifyshare/css");
CopyFileLocalOrWeb("$share/bootify", "bootstrap-theme.min.css",
$::bootstrapthemecss, "./bootifyshare/css");
CopyFileLocalOrWeb("$share/bootify", "glyphicons-halflings-regular.eot",
$::bootstrapeot, "./bootifyshare/fonts");
CopyFileLocalOrWeb("$share/bootify", "glyphicons-halflings-regular.svg",
$::bootstrapsvg, "./bootifyshare/fonts");
CopyFileLocalOrWeb("$share/bootify", "glyphicons-halflings-regular.ttf",
$::bootstrapttf, "./bootifyshare/fonts");
CopyFileLocalOrWeb("$share/bootify", "glyphicons-halflings-regular.woff",
$::bootstrapwoff, "./bootifyshare/fonts");
CopyFileLocalOrWeb("$share/bootify", "glyphicons-halflings-regular.woff2",
$::bootstrapwoff2, "./bootifyshare/fonts");
}
}
#*************************************************************************
#> sub CopyFileLocalOrWeb($localDir, $filename, $url, $dest)
# ---------------------------------------------------------
# $localDir - the local directory where the file is cached
# $filename - the file name within the directory
# $url - the remote URL for the file
# $dest - the destination directory for the file
# If a local copy of the file exists as $localDir/$filename then this is
# copied to $dest.
# If not then we grab the file over the internet
#
# 21.02.17 Original By: ACRM
sub CopyFileLocalOrWeb
{
my($localDir, $filename, $url, $dest) = @_;
if(-e "$localDir/$filename")
{
`cp $localDir/$filename $dest`
}
else
{
`wget -O $dest/$filename $url`;
}
}
#*************************************************************************
#> void CheckAndGetSharedFile($dest, $url)
# ---------------------------------------
# $dest - destination file
# $url - URL of file
# This routine checks if a file exists. If not, then it checks for
# write permissions in the file's directory and attempts to get the
# file from the internet.
# This is used to populate the bootify share directory with copies
# of jquery and bootstrap
#
# 21.02.17 Original By: ACRM
sub CheckAndGetSharedFile
{
my($dest, $url) = @_;
if(! -e $dest)
{
my $destDir = $dest;
$destDir =~ m/(.*\/)/;
$destDir = $1;
my $checkWrite = $destDir . ".checkWrite";
`touch $checkWrite`;
if(-e $checkWrite)
{
`wget -O $dest $url`;
unlink("$checkWrite");
}
}
}
#*************************************************************************
#> void UsageDie(void)
# -------------------
# Prints a usage message and exits
#
# 11.11.15 Original By: ACRM
# 05.01.16 V1.2 - corrected program name!
# 18.09.16 V1.3 - added -force and new options
# 17.10.16 V1.4 - added open='true' option for [ai]
# 09.01.17 V1.5 - Added support for external style sheets with <link>
# 21.02.17 V1.6 - Added -local option
#
sub UsageDie
{
print <<__EOF;
bootify V1.6 (c) 2015-2017 UCL, Dr. Andrew C.R. Martin
Usage: bootify [-local] file.boot
-or-
bootify -clean [-force]
-local - Do a local install of Bootstrap and JQuery rather than referencing
them over the web
-clean - removes generated files
-force - with -clean does not check if you want to delete HTML files
Note that every piece of meta-HTML must appear on a single line in the input.
__EOF
exit 0;
}
#*************************************************************************
#> my $title = GetTitle(@data)
# ---------------------------
# Extracts the title from the <title> tag in the HTML
#
# 11.11.15 Original By: ACRM
#
sub GetTitle
{
my(@data) = @_;
my $title = '';
my $allData = join(' ', @data);
$allData =~ s/\r//g;
$allData =~ s/\n/<ret>/g;
if($allData=~/<title>(.*?)<\/title>/)
{
$title = $1;
}
$title =~ s/<ret>/ /g;
return($title);
}
#*************************************************************************
#> my $style = GetStyle(@data)
# ---------------------------
# Extracts any <style> information from the HTML. Only reads the
# first style tag - does not pick up external style information
#
# 11.11.15 Original By: ACRM
# 09.01.17 Now does the wrapping around $style and also checks for
# external style sheets
#
sub GetStyle
{
my(@data) = @_;
my $style = '';
my $links = '';
my $allData = join(' ', @data);
$allData =~ s/\r//g;
$allData =~ s/\n/<ret>/g;
if($allData=~/<style.*?>(.*?)<\/style>/)
{
$style = $1;
$style =~ s/<ret>/\n/g;
}
if(length($style))
{
$style = "<style type='text/css'>\n" . $style . "</style>\n";
}
# Add any external style sheets
foreach my $line (@data)
{
if($line =~ /(\<link.*\/\>)/)
{
$links .= "$1\n";
}
}
return("$links$style");
}
#*************************************************************************
#> $aMenu = GetMenuItems(@data)
# -----------------------------
# Extracts menu items from [page menu='xxx'] metatags. Returns
# an array reference
#
# 11.11.15 Original By: ACRM
#
sub GetMenuItems
{
my(@data) = @_;
my @menu = ();
foreach my $line (@data)
{
if($line =~ /<!--\s+\[page\s+menu=['"](.*?)['"]\]\s+--\>/)
{
push @menu, $1;
}
}
return(\@menu);
}
#*************************************************************************
#> $aPages = GetPages(@data)
# -------------------------
# Splits the HTML into separate pages using the [page] metatags.
# Returns an array reference
#
# 11.11.15 Original By: ACRM
#
sub GetPages
{
my(@data) = @_;
my @Pages = ();
my $inPage = 0;
my $pageNum = 0;
foreach my $line (@data)
{
if($inPage)
{
push(@{$Pages[$pageNum]}, $line);
}
if($line =~ /\<!--\s+\[page\s.*\]\s+--\>/)
{
@{$Pages[$pageNum]} = ();
push(@{$Pages[$pageNum]}, $line);
$inPage = 1;
}
elsif($line =~ /<!--\s+\[\/page\]\s+--\>/)
{
$inPage = 0;
$pageNum++;
}
}
return(\@Pages);
}
#*************************************************************************
#> void WritePage($pageNum, $title, $style, $homeMenu, $homeURL,
# $aMenu, $aPage, $lastPage)
# --------------------------------------------------------------
# $pageNum - The page number. 0 gives index.html
# >0 gives pageN.html
# $title - Contents of <title> tag
# $style - Any contents of <style> tag
# $homeMenu - A title-like home menu item taken from [home] or the
# [bigheading]<h1>
# $homeURL - A URL for the home menu item if [home url=''] given
# $aMenu - Reference to array of menu items
# $aPage - Reference to array of lines for this page
# $lastPage - Flag to indicate this is the last page so doesn't
# need a Continue button
#
# Writes an HTML page
#
# 11.11.15 Original By: ACRM
# 16.09.16 Added $homeURL
#
sub WritePage
{
my ($pageNum, $title, $style, $homeMenu, $homeURL, $aMenu, $aPage, $lastPage) = @_;
my $filename = 'index.html';
if($pageNum)
{
$filename = sprintf("page%02d.html", $pageNum);
}
if(open(my $fp, '>', $filename))
{
PrintHTMLHeader($fp, $title, $style);
PrintHTMLMenu($fp, $homeMenu, $homeURL, $aMenu, $pageNum);
PrintHTMLPage($fp, $aPage);
PrintHTMLNextButton($fp, $pageNum) if(!$lastPage);
PrintHTMLFooter($fp);
}
}
#*************************************************************************
#> $homeMenu = GetHomeMenu(@data)
# ------------------------------
# Extracts a home menu title from [home] if specified, otherwise
# from [bigheading]<h1>
#
# 11.11.15 Original By: ACRM
# 16.09.16 Modified to use [home] if specified
#
sub GetHomeMenu
{
my (@data) = @_;
my $inBigHeading = 0;
my $inH1 = 0;
my $h1 = '';
my $url = '';
# First check for [home] tag
foreach my $line (@data)
{
if($line =~ /<!--\s+\[home\s+url=['"](.*?)['"]\].*--\>/)
{
$url = $1;
}
if($line =~ /<!--\s+\[home.*?\](.*)\[\/home\].*--\>/)
{
$h1 = $1;
return($h1, $url);
}
}
# [home] tag not found, use [bigheading]<h1>
foreach my $line (@data)
{
if($inBigHeading)
{
if($line =~ /<h1>/)
{
$inH1 = 1;
$h1 .= $line;
}
if($line =~ /<\/h1>/)
{
$inH1 = 0;
}
}
if($line =~ /\[bigheading\]/)
{
$inBigHeading = 1;
}
if($line =~ /\[\/bigheading\]/)
{
$inBigHeading = 0;
last;
}
}
$h1 =~ s/[\n\r]/ /g;
$h1 =~ /<h1>(.*?)<\/h1>/;
return($1);
}
#*************************************************************************
#> $line = Replace($line, $tag, $new, [$idStem, $sCounter])
# --------------------------------------------------------
# $line - A line of HTML
# $tag - A metatag name to be replaced
# $new - The new HTML to relace the metatag
# $idStem - An ID to be inserted into the new HTML
# $sCounter - Reference to a counter to be appended to the ID
#
# Takes a metatag name and replaces it with the new text. e.g.
# $line = Replace($line, 'foo', '<div class="bar">');
# $line = Replace($line, '/foo', '</div>');
# would replace
# <!-- [foo] -->
# <!-- [/foo] -->
# with
# <div class="bar">
# </div>
#
# Optionally can also build an id from a stem and counter
# and inserts it into the replacement string. If using this
# the replacement string must contain '{}' where the id must
# go. So you could do something like:
# $count = 0;
# $line = Replace($line, 'foo', '<div id="{}">', 'bar', \$count);
# Each call would then replace
# <!-- [foo] -->
# with
# <div id="bar1">
# <div id="bar2">
# etc
#
# 11.11.15 Original By: ACRM
# 06.01.16 Also handles self-closing tags
#
sub Replace
{
my($line, $tag, $new, $idStem, $sCounter) = @_;
# Construct the regex: <!-- [$tag] -->
my $regex = '<!--\s+\[' . $tag . '(\s+\/)?\]\s+--\>';
if(scalar(@_) > 3)
{
if($line =~ /$regex/)
{
$$sCounter++;
my $id = "$idStem$$sCounter";
$new =~ s/\{\}/$id/;
$line =~ s/$regex/$new/;
}
}
else
{
$line =~ s/$regex/$new/;
}
return($line);
}
#*************************************************************************
#> $line = ReplaceParam($line, $tag, $attribute, $replace)
# -------------------------------------------------------
# $line - A line of HTML
# $tag - The metatag name
# $attribute - An attribue name
# $replace - Replacement text
#
# Takes a metatag name with an associated attribute and replaces
# it with the new text inserting the attribute value. e.g.
# $line = ReplaceParam($line, 'foo', 'bar' '<div class="{}">');
# would replace
# <!-- [foo bar='value'] -->
# with
# <div class="value">
#
# 11.11.15 Original By: ACRM
# 05.01.16 {} replacement is now global
#
sub ReplaceParam
{
my($line, $tag, $attribute, $replace) = @_;
my $regex = '<!--\s+\[' . $tag . '\s+' . $attribute . "=['\"](.*)['\"]" . '\s*\]\s+--\>';
if($line =~ $regex)
{
my $value = $1;
$replace =~ s/\{\}/$value/g;
$line =~ s/$regex/$replace/;
}
return($line);
}
#*************************************************************************
#> $line = ReplaceWholeTag($line, $tag, $content)
# ----------------------------------------------
# Replaces a whole [tag]...[/tag] with $content
# If $content contains '{}', then this will be substituted with the
# text between [tag] and [/tag]
#
# 05.01.16 Original By: ACRM
#
sub ReplaceWholeTag
{
my($line, $tag, $newContent) = @_;
# Construct the regex: <!-- [$tag](.*?)[/$tag] -->
my $regex = '<!--\s+\[' . $tag . '\]\s+--\>(.*?)<!--\s+\[/' . $tag . '\]\s+--\>';
if($line =~ /$regex/)
{
my $oldContent = $1;
$newContent =~ s/\{\}/$oldContent/g;
$line =~ s/$regex/$newContent/;
}
return($line);
}
#*************************************************************************
#> $line = ReplaceMultiParams($line, $tag, $aAttributes, $replace)
# ---------------------------------------------------------------
# $line - A line of HTML
# $tag - The metatag name
# $aAttributes - Reference to an array of attribute names
# $replace - Replacement text
#
# Takes a metatag name with an associated set of attributes and replaces
# it with the new text inserting the attribute values. e.g.
# $line = ReplaceParam($line, 'foo', \@('bar1','bar2'), '<div class="{0}" style="{1}">');
# would replace
# <!-- [foo bar1='value' bar2='border: none'] -->
# with
# <div class="value" style="border: none">
#
# The attribute values are also stored in the global %::attribute{} hash
#
# 05.01.16 Original By: ACRM
#
sub ReplaceMultiParams
{
my($line, $tag, $aAttributes, $replace) = @_;
my $regex = '<!--\s+\[' . $tag . '.*?\]\s+--\>'; # Check if it's this tag
my $attrCount = 0;
if($line =~ $regex)
{
foreach my $attribute (@$aAttributes)
{
# Remove the ? if there is one (optional attributes)
my $attr = $attribute;
$attr =~ s/\?//g;
# Construct the resgular expression
my $attrRegex = $attr . "=['\"](.*?)['\"]";
if($line =~ /$attrRegex/)
{
my $value = $1;
$replace =~ s/\{$attrCount\}/$value/g;
$::attribute{$attribute} = $value;
}
elsif($attr eq $attribute)
{
# If attribute wasn't found and was not optional (because the version with
# question marks stripped was the same as before removing them - i.e. there
# was no question mark), then we have an error
printf STDERR "Error (bootify): Attribute '$attribute' missing in line -\n";
printf STDERR " $line\n";
exit 1;
}
$attrCount++;
}
$line =~ s/$regex/$replace/;
}
return($line);
}
#*************************************************************************
#> void PrintHTMLFooter($fp)
# -------------------------
# Prints the footer for an HTML page
#
# 11.11.15 Original By: ACRM
#
sub PrintHTMLFooter
{
my($fp) = @_;
if($::local)
{
print $fp <<__EOF;
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="bootifyshare/js/jquery.min.js"></script>
<script src="bootifyshare/js/bootstrap.min.js"></script>
<script src="mpautotooltip.js"></script>
</body>
</html>
__EOF
}
else
{
print $fp <<__EOF;
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="$::jquery"></script>
<script src="$::bootstrapjs" crossorigin="anonymous"></script>
<script src="mpautotooltip.js"></script>
</body>
</html>
__EOF
}
}
#*************************************************************************
#> void PrintHTMLNextButton($fp, $pageNum)
# ---------------------------------------
# $fp - File handle
# $pageNum - The current page number
#
# Creates an HTML 'Continue' button providing a link to the next
# page.
#
# 11.11.15 Original By: ACRM
#
sub PrintHTMLNextButton
{
my($fp, $pageNum) = @_;
my $filename = sprintf("page%02d.html", $pageNum+1);
print $fp <<__EOF;
<div class='center'>
<a class="btn btn-lg btn-primary" href="$filename">Continue</a>
</div>
__EOF
}
#*************************************************************************
#> void PrintHTMLMenu($fp, $homeMenu, $homeURL, $aMenu, $pageNum)
# --------------------------------------------------------------
# $fp - File handle
# $homeMenu - The 'home menu' item
# $homeURL - A URL for the home menu item if [home url=''] given
# $aMenu - Reference to an array of menu items
# $pageNum - The current page number (to highlight the current
# menu item)
#
# Prints the HTML menu. This is a list formatted with Bootstrap
#
# 11.11.15 Original By: ACRM
# 16.09.16 Added $homeURL
#
sub PrintHTMLMenu
{
my($fp, $homeMenu, $homeURL, $aMenu, $pageNum) = @_;
$homeURL = 'index.html' if($homeURL eq '');
print $fp <<__EOF;
<!-- Fixed navbar -->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="$homeURL">$homeMenu</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
__EOF
for(my $i=0; $i<scalar(@$aMenu); $i++)
{
my $filename = 'index.html';
my $active = '';
$active = " class='active'" if($i == $pageNum);
if($i)
{
$filename = sprintf("page%02d.html", $i);
}
print $fp "<li$active><a href='$filename'>$$aMenu[$i]</a></li>\n";
}
print $fp <<__EOF;
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
__EOF
}
#*************************************************************************
#> void PrintHTMLHeader($fp, $title, $style)
# -----------------------------------------
# $fp - File handle
# $title - The <title> tag content
# $style - Optional style information
#
# Creates an HTML header for a page
#
# 11.11.15 Original By: ACRM
# 09.01.17 No longer does the wrapping around $style as that is done in GetStyle()
#
sub PrintHTMLHeader
{
my($fp, $title, $style) = @_;
print $fp <<__EOF;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="favicon.png">
__EOF
if($::local)
{
print $fp <<__EOF;
<!-- Bootstrap core CSS and theme -->
<link href="bootifyshare/css/bootstrap.min.css" rel="stylesheet">
<link href="bootifyshare/css/bootstrap-theme.min.css" rel="stylesheet">
__EOF
}
else
{
print $fp <<__EOF;
<!-- Bootstrap core CSS and theme -->
<link rel="stylesheet" href="$::bootstrapcss" crossorigin="anonymous">
<link rel="stylesheet" href="$::bootstrapthemecss" crossorigin="anonymous">
__EOF
}
print $fp <<__EOF;
<!-- Custom styles for this template -->
<link href="mptheme.css" rel="stylesheet">
<!-- And my own useful extras -->
<link href="mpcss.css" rel="stylesheet">
<!-- Any style information from the web page -->
$style
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<title>$title</title>
</head>
<body>
__EOF
}
#*************************************************************************
#> void PrintHTMLPage($fp, $aPage)
# -------------------------------
# $fp - File handle
# $aPage - Reference to array of lines for the page
#
# The main routine for printing a page of HTML. Calls the various
# Fixup_*() routines to replace metatags with the relevant HTML.
# Then prints the lines of HTML to the file
#
# 11.11.15 Original By: ACRM
# 05.01.16 Added Fixup_link() and Fixup_figure()
# 06.01.16 Added Fixup_flush()
#
sub PrintHTMLPage
{
my($fp, $aPage) = @_;
print $fp " <div class='container theme-showcase'>\n";
Fixup_quiz($aPage);
Fixup_bigheading($aPage);
Fixup_callout($aPage);
Fixup_warning($aPage);
Fixup_important($aPage);
Fixup_note($aPage);
Fixup_information($aPage);
Fixup_popup($aPage);
Fixup_help($aPage);
Fixup_instruction($aPage);
Fixup_accordion($aPage);
Fixup_ai($aPage);
Fixup_box($aPage);
Fixup_confirm($aPage);
Fixup_link($aPage);
Fixup_figure($aPage);
Fixup_flush($aPage);
foreach my $line (@$aPage)
{
print $fp $line;
}
print $fp " </div> <!-- /container -->\n";
}
#*************************************************************************
#> void Fixup_bigheading($aPage)
# -----------------------------
# Replaces the [bigheading] metatag with a Bootstrap jumbotron
#
# 11.11.15 Original By: ACRM
#
sub Fixup_bigheading
{
my($aPage) = @_;
foreach my $line (@$aPage)
{
$line = Replace($line, 'bigheading','<div class="jumbotron">');
$line = Replace($line, '/bigheading','</div> <!-- jumbotron -->');
}
}
#*************************************************************************
#> void Fixup_callout($aPage)
# --------------------------
# Replaces the [callout] metatag with a Bootstrap info callout
#
# 11.11.15 Original By: ACRM
#
sub Fixup_callout
{
my($aPage) = @_;
foreach my $line (@$aPage)
{
$line = Replace($line, 'callout','<div class="bs-callout bs-callout-info">');
$line = Replace($line, '/callout','</div> <!-- callout -->');
}
}
#*************************************************************************
#> void Fixup_warning($aPage)
# --------------------------
# Replaces the [warning] metatag with a Bootstrap danger alert
#