陈连福html功能注释网页文件修改perl

193     # 获取页眉HTML和CSS样式
 /var/www/html/Annotation_exhibition/modify_annotations.pl[1]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             [perl] unix utf-8 1:1/264
  1 #!/usr/bin/perl
  2 use strict;
  3 use warnings;
  4 use File::Copy;
  5 use Encode qw(decode encode);
  6
  7 # 定义要处理的文件列表
  8 my @annotation_files = (
  9     "ArAl_functional_annotation.All.html",
 10     "ArHe_functional_annotation.All.html",
 11     "ArHeCHN2_functional_annotation.All.html",
 12     "ArHeBD_functional_annotation.All.html",
 13     "ArHi_functional_annotation.All.html",
 14     "ArNa_functional_annotation.All.html",
 15     "ArCa_functional_annotation.All.html"
 16 );
 17
 18 # 定义物种名称映射
 19 my %species_names = (
 20     "ArAl" => "Artocarpus altilis ICRAFF_11315",
 21     "ArHe" => "Artocarpus heterophyllus ICRAFF_11314",
 22     "ArHeCHN2" => "Artocarpus heterophyllus S10",
 23     "ArHeBD" => "Artocarpus heterophyllus BARI_K3",
 24     "ArHi" => "Artocarpus hirsutus CAMOHE01",
 25     "ArNa" => "Artocarpus nanchuanensis CQ-2022",
 26     "ArCa" => "Artocarpus camansi LNSY01"
 27 );
 28
 29 # 页眉HTML模板
 30 sub get_header_html {
 31     my $species_name = shift;
 32     my $header_template = <<'EOF';
 33 <!-- 页眉 -->
 34 <table width="100%" cellspacing="0" cellpadding="0" bgcolor="#fff">
 35    <tr bgcolor="#78935d">
 36       <td align="center" height="60">
 37         <img src="../Artocarpus/images/logo.gif" hspace="0" vspace="0" border="0" width="1200" height="60">
 38       </td>
 39    </tr>
 40    <tr><td><hr></td></tr>
 41 </table>
 42
 43 <!-- 物种标题 -->
 44 <div class="species-title">
 45     <i>SPECIES_NAME_PLACEHOLDER</i> - Functional Annotation
 46 </div>
 47 EOF
 48
 49     # 替换物种名称占位符
 50     $header_template =~ s/SPECIES_NAME_PLACEHOLDER/$species_name/g;
 51     return $header_template;
 52 }
 53
 54 # CSS样式模板
 55 sub get_css_styles {
 56     return <<'EOF';
 57 <style>
 58 /* 原有样式保持,但调整对齐方式 */
 59 .table1{
 60     position: fixed;
 61     table-layout: fixed;
 62     word-break: break-all;
 63     word-wrap: break-word;
 64     background: #dddddd;
 65     width: 1540px; /* 原来1400px的110% */
 66     margin-left: calc(50% - 770px); /* 770px = 1540px / 2,确保精确居中 */
 67     z-index: 1000;
 68     border-collapse: collapse; /* 确保边框一致 */
 69 }
 70
 71 .table2{
 72     table-layout: fixed;
 73     word-break: break-all;
 74     word-wrap: break-word;
 75     margin: 0 auto;
 76     margin-top: 60px; /* 为固定表头留出空间 */
 77     width: 1540px; /* 原来1400px的110% */
 78     border-collapse: collapse; /* 确保边框一致 */
 79 }
 80
 81 .td1{
 82     white-space: nowrap;
 83     text-overflow: ellipsis;
 84     overflow:hidden;
 85 }
 86
 87 /* 新添加的样式 */
 88 body {
 89     margin: 0;
 90     padding: 0;
 91     font-family: Arial, sans-serif;
 92     background-color: #f9f9f9;
 93     display: flex;
 94     flex-direction: column;
 95     align-items: center;
 96 }
 97
 98 /* 物种标题样式 */
 99 .species-title {
100     font-size: 24px;
101     font-weight: bold;
102     color: #2d3748;
103     margin: 20px 0;
104     text-align: center;
105 }
106
107 /* 内容容器 */
108 .content-container {
109     width: 100%;
110     max-width: 1540px; /* 调整最大宽度为110% */
111     padding: 0 20px;
112     box-sizing: border-box;
113 }
114
115 /* 表格居中 */
116 table {
117     margin: 0 auto !important;
118 }
119
120 /* 页眉table保持100%宽度 */
121 body > table:first-of-type {
122     width: 100% !important;
123     margin: 0 !important;
124 }
125
126 /* 确保表格完全对齐 */
127 .table1, .table2 {
128     box-sizing: border-box;
129     padding: 0;
130 }
131
132 /* 响应式调整 */
133 @media (max-width: 1580px) { /* 调整断点以适应新的1540px宽度 */
134     .table1 {
135         width: 95% !important;
136         margin-left: 2.5% !important; /* (100% - 95%) / 2 */
137     }
138
139     .table2 {
140         width: 95% !important;
141     }
142
143     .content-container {
144         max-width: 95%;
145     }
146
147     /* 页眉logo在小屏幕上调整 */
148     body > table:first-of-type img {
149         width: 95% !important;
150         max-width: 1200px;
151     }
152 }
153
154 @media (max-width: 1200px) {
155     .table1 {
156         width: 98% !important;
157         margin-left: 1% !important; /* (100% - 98%) / 2 */
158     }
159
160     .table2 {
161         width: 98% !important;
162     }
163
164     .content-container {
165         max-width: 98%;
166         padding: 0 10px;
167     }
168 }
169 </style>
170 EOF
171 }
172
173 # 处理单个文件
174 sub process_file {
175     my ($filename, $backup_dir) = @_;
176
177     print "处理文件: $filename\n";
178
179     # 创建备份
180     my $backup_file = "$backup_dir/$filename.backup";
181     copy($filename, $backup_file) or die "无法创建备份文件: $!";
182     print "  已创建备份: $backup_file\n";
183
184     # 读取原文件
185     open(my $fh, '<:encoding(utf8)', $filename) or die "无法打开文件 $filename: $!";
186     my $content = do { local $/; <$fh> };
187     close($fh);
188
189     # 提取物种代码
190     my ($species_code) = $filename =~ /^(\w+)_functional_annotation/;
191     my $species_name = $species_names{$species_code} || "Unknown Species";
192
193     # 获取页眉HTML和CSS样式
194     my $header_html = get_header_html($species_name);
195     my $css_styles = get_css_styles();
196
197     # 修改HTML内容
198     # 1. 替换或添加CSS样式
199     if ($content =~ /<style>.*?<\/style>/s) {
200         # 如果已有style标签,替换它
201         $content =~ s/<style>.*?<\/style>/$css_styles/s;
202     } else {
203         # 如果没有style标签,在head中添加
204         $content =~ s/<\/head>/$css_styles\n<\/head>/;
205     }
206
207     # 2. 修改HTML中现有的table宽度属性(如果存在)
208     $content =~ s/<table([^>]*)\s+width="1400"/<table$1 width="1540"/g;
209     $content =~ s/<table([^>]*)\s+width=1400/<table$1 width=1540/g;
210
211     # 3. 在body开始后添加页眉
212     $content =~ s/(<body[^>]*>)/$1\n$header_html\n<div class="content-container">/;
213
214     # 4. 在body结束前添加容器结束标签
215     $content =~ s/<\/body>/<\/div>\n<\/body>/;
216
217     # 5. 更新页面标题
218     $content =~ s/<title>.*?<\/title>/<title>$species_name - Functional Annotation<\/title>/;
219
220     # 写入修改后的内容
221     open(my $out_fh, '>:encoding(utf8)', $filename) or die "无法写入文件 $filename: $!";
222     print $out_fh $content;
223     close($out_fh);
224
225     print "  文件处理完成: $filename\n";
226 }
227
228 # 主程序
229 sub main {
230     # 创建备份目录
231     my $backup_dir = "backup_" . time();
232     mkdir $backup_dir or die "无法创建备份目录: $!";
233     print "备份目录已创建: $backup_dir\n\n";
234
235     # 处理每个文件
236     foreach my $file (@annotation_files) {
237         if (-f $file) {
238             eval {
239                 process_file($file, $backup_dir);
240             };
241             if ($@) {
242                 print "处理文件 $file 时出错: $@\n";
243             }
244         } else {
245             print "文件不存在: $file\n";
246         }
247         print "\n";
248     }
249
250     print "批量处理完成!\n";
251     print "所有原文件已备份到: $backup_dir\n";
252     print "\n修改内容:\n";
253     print "1. 页眉背景色已改为 #78935d\n";
254     print "2. logo路径已改为 ../Artocarpus/images/logo.gif\n";
255     print "3. 表格宽度已调整为1540px (原来1400px的110%)\n";
256     print "\n注意事项:\n";
257     print "1. 请检查修改后的文件是否正常显示\n";
258     print "2. 如果有问题可以从备份目录恢复原文件\n";
259     print "3. 确保 ../Artocarpus/images/logo.gif 文件路径正确\n";
260     print "4. 表格现在更宽,请确保页面显示效果符合预期\n";
261 }
262
263 # 运行主程序
264 main();
~
~
~
~
~
~
~
~
~
~
~
~