hynek has asked for the wisdom of the Perl Monks concerning the following question:
As it's a one-use script, some assumptions are made: The old .html-files from blogger.com are in /home/hynek/old-blog/tmp and the new files are put into /home/hynek/old-blog/new. The date has always the same form and appears only once and the body and the subject are also easy to distinguish so it doesn't pay off to use a full-blown html-parser, however it leads to a rather ugly loop. An example of such a page is at here. Blosxom-files are simple .txt files with html in it and with the first line being the subject.
So here's the code:
What I especially dislike is the loop and the reading of the date consisting of two regexps.#!/usr/bin/perl use strict; use warnings; use diagnostics; use File::Find; use Date::Format; use Date::Parse; sub process { if (-f $_ && /\.html$/) { my $fn = $_; open(F, "<:utf8", $fn) or die("Cannot open ${File::Find::name}: $! +"); my @file = <F>; chomp(@file); close F; (my $date) = grep m%\w+, \w+ \d\d, \d\d\d\d%, @file; $date =~ m%\w+, (\w+ \d\d, \d\d\d\d)%; my $time = str2time($1); my ($in_text, $in_subject) = undef; my ($text, $subject) = ""; READ: foreach (@file) { if (m%<h3 class="post-title">%) { # Subject $in_subject = 1; } elsif ($in_subject && $_ !~ m%</h3>%) { $subject = $_ if /\w/; } elsif ($in_subject && $_ =~ m%</h3>%) { $in_subject = undef; $text = "$subject\n"; } elsif (m%<div class="post-body">%) { # Text $in_text = 1; } elsif ($in_text && $_ !~ m%</div>%) { $text .= $_ . "\n"; } elsif ($in_text && $_ =~ m%</div>%) { last READ; } } $text =~ s/(\r|<p>|<\/p>|^\s*)//gm; $fn =~ s/html$/txt/; open(OUT, ">:encoding(iso-8859-15)", "/home/hynek/old-blog/new/$fn +") or die "Cannot open for write: $!."; print OUT $text; close(OUT); utime $time, $time, ("/home/hynek/old-blog/new/$fn"); } } find(\&process, ("tmp"));
Thanks in advance.
UPDATE: Removed the copy'n'paste error from utime.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hints for getting this perly...
by Anonymous Monk on May 11, 2005 at 08:52 UTC | |
by Animator (Hermit) on May 11, 2005 at 08:55 UTC | |
by hynek (Novice) on May 11, 2005 at 09:10 UTC | |
by hynek (Novice) on May 11, 2005 at 09:00 UTC | |
|
Re: Hints for getting this perly...
by bart (Canon) on May 11, 2005 at 08:56 UTC | |
by hynek (Novice) on May 11, 2005 at 09:16 UTC | |
|
Re: Hints for getting this perly...
by tlm (Prior) on May 11, 2005 at 11:23 UTC | |
by hynek (Novice) on May 11, 2005 at 13:37 UTC | |
by tlm (Prior) on May 11, 2005 at 15:11 UTC | |
|
Re: Hints for getting this perly...
by hynek (Novice) on May 11, 2005 at 10:28 UTC | |
by hynek (Novice) on May 11, 2005 at 13:26 UTC |