我們走吧。我在做一些假設,僅保留,因爲我的設計人員使用某種風格:
- 一切都是一個div
- 我只希望看到的文本樣式,所以我會忽略任何事情似乎不與文字有關
不是我最好的東西,但它適用於我的大多數風格。必須手動編輯一些標籤,例如ul,ol,li,並刪除「body」。
#!/usr/bin/perl
use warnings;
use strict;
my $css;
open(FILE, '<', 'styles.css') or die();
while (<FILE>) { $css .= $_; }
close(FILE);
my (@css) = $css =~ m/^([a-zA-Z.#][^\n\r]+{.+?})/gmxs;
my @text_css = grep { /\s(h[1-5]|span|font|color|p|a|ol|ul)\b/ } @css;
foreach my $css(@text_css) {
my ($selector_text) = $css =~ /^([^{]*){/;
my (@selector) = split(/[\s{]/,(split(/[\n\r]+/,$selector_text))[0]);
@selector = grep { !/{/ } @selector;
my $start_html = '';
my $middle_html = join(" ",@selector);
my $end_html = '';
my $result = '';
for (my $i=0; $i< scalar(@selector); $i++) {
$selector[$i] =~ s/:[-\w]+//g;
if (substr($selector[$i],0,1) eq '#') {
$selector[$i] =~ s/^#//g;
$start_html .= qq(<div id="$selector[$i]">);
$end_html = "</div>" . $end_html;
}
elsif (substr($selector[$i],0,1) eq '.') {
$selector[$i] =~ s/^\.//g;
$start_html .= qq(<div class="$selector[$i]">);
$end_html = "</div>" . $end_html;
}
else {
# we have a tag, possibly with an id/class
my($tag,$extra,$type);
if ($selector[$i] =~ m/\./) {
($tag,$extra) = split('.', $selector[$i]);
$extra =~ s/^\.//g;
$type = 'class';
}
elsif ($selector[$i] =~ m/#/) {
($tag,$extra) = split('#', $selector[$i]);
$extra =~ s/^#//g;
$type = 'id';
}
else {
$tag = $selector[$i];
}
if ($extra and $type) {
$start_html .= qq(<$tag $type="$extra">);
}
else {
$start_html .= qq(<$tag>);
}
$end_html = "</$tag>" . $end_html;
}
# is this the last one?
if ($i == scalar(@selector) - 1) {
$result = $start_html . $middle_html . $end_html;
}
print "<div>$result</div>\n" if ($result);
}
}
爲了做到這一點,CSS必須更具體,即div#basicInfoHead而不是#basicInfoHead。儘管不計算,要計算出什麼應該包含虛擬文本將會非常困難? – 2010-05-23 22:24:20