source: trunk/gsdl/bin/script/convert_toc.pl@ 3093

Last change on this file since 3093 was 3093, checked in by nzdl, 22 years ago

* empty log message *

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1#! /usr/bin/perl -w
2
3# convert_toc.pl converts old <<TOC>> marked up files to use the new
4# <Section> syntax (suitable for processing by HTMLPlug -description_tags
5
6use File::Basename;
7
8my $level = 0;
9
10sub main {
11 open STDIN, "<$ARGV[0]" or die "$ARGV[0]: $!\n";
12 open STDOUT, ">$ARGV[1]" or die "$ARGV[1]: $!\n";
13
14 my $dirname = &File::Basename::dirname($ARGV[0]);
15
16 my $content = "";
17
18 while (<STDIN>) {
19 $_ =~ s/[\cJ\cM]+$/\n/;
20 $content .= $_;
21 }
22
23 # fix images
24 $content =~ s/&lt;&lt;I&gt;&gt;\s*(\w+\.(?:png|jpe?g|gif))\s*(.*?)<\/p>/rename_image_old($1, $2, $dirname)/iegs;
25 $content =~ s/<img\s+src=\"?(\w+\.(?:png|jpe?g|gif))\"?/rename_image($1, $dirname)/iegs;
26
27
28 # process section title
29 $content =~ s/(?:<P[^>]*>|<BR>)(?:\s*<\/?[^>]*>)*?&lt;&lt;TOC(\d+)&gt;&gt;(.*?)(?=<\/?P[^>]*>|<BR>)/
30 process_toc ($1, $2)/sige;
31
32 # close the remaining sections
33 my $section_ending = "<!--\n";
34 for (my $j = 0; $j < $level; $j++) {
35 $section_ending .= "</Section>\n";
36 }
37 $section_ending .= "-->\n";
38 $content =~ s/(<\/body>)/$section_ending.$1/sie;
39
40 print STDOUT $content;
41 close STDOUT; close STDIN;
42}
43
44sub process_toc {
45 my ($thislevel, $title) = @_;
46 my $toc = '';
47
48 $title =~ s/<[^>]+>//sg;
49 $title =~ s/^\s+//s;
50 $title =~ s/\s+$//s;
51 $title =~ s/\n/ /sg;
52
53 $toc .= "\n<!--\n";
54
55 if ($thislevel <= $level) {
56 for (my $i = 0; $i < ($level-$thislevel+1); $i++) {
57 $toc .= "</Section>\n";
58 }
59 }
60 $toc .= "<Section>\n".
61 " <Description>\n".
62 " <Metadata name=\"Title\">$title</Metadata>\n".
63 " </Description>\n".
64 "-->\n";
65
66 $level = $thislevel;
67
68 return $toc;
69}
70
71sub rename_image_old {
72 my ($image_name, $following_text, $dirname) = @_;
73
74 &rename_image($image_name, $dirname);
75
76 return "<center><img src=\"$image_name\"><br>$following_text<\/center><\/p>\n";
77}
78
79
80# may need to rename image files if case isn't consistent
81# (i.e. collections prepared on windows may have images named
82# AAA.GIF which are linked into the HTML as aaa.gif)
83sub rename_image {
84 my ($image_name, $dirname) = @_;
85
86 if (!-e "$dirname/$image_name") {
87
88 my ($pre, $ext) = $image_name =~ /^([^\.]+)\.(.*)$/;
89 my $image_name_uc = uc($image_name);
90 my $image_name_lc = lc($image_name);
91 my $image_name_ul = uc($pre) . "." . lc($ext);
92 my $image_name_lu = lc($pre) . "." . uc($ext);
93
94 if (-e "$dirname/$image_name_uc") {
95 print STDERR "renaming $dirname/$image_name_uc --> $dirname/$image_name\n";
96 rename ("$dirname/$image_name_uc", "$dirname/$image_name");
97
98 } elsif (-e "$dirname/$image_name_lc") {
99 print STDERR "renaming $dirname/$image_name_lc --> $dirname/$image_name\n";
100 rename ("$dirname/$image_name_lc", "$dirname/$image_name");
101
102 } elsif (-e "$dirname/$image_name_ul") {
103 print STDERR "renaming $dirname/$image_name_ul --> $dirname/$image_name\n";
104 rename ("$dirname/$image_name_ul", "$dirname/$image_name");
105
106 } elsif (-e "$dirname/$image_name_lu") {
107 print STDERR "renaming $dirname/$image_name_lu --> $dirname/$image_name\n";
108 rename ("$dirname/$image_name_lu", "$dirname/$image_name");
109
110 } else {
111 print STDERR "ERROR**** $dirname/$image_name could not be found\n";
112 }
113 }
114 return "<img src=\"$image_name\"";
115}
116
117
118
119&main;
Note: See TracBrowser for help on using the repository browser.