Projet

Général

Profil

Demande #330 » mediawiki_converter.pl

Anonyme, 17/01/2011 21:24

 
#!/usr/bin/perl

# Mediawiki converter
# Copyright (C) 2010 Francois Boulogne <fboulogne at april dot org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




use strict;
use warnings;
use Encode;
use Sub::Override;
use LWP::Simple; # get web pages
use XHTML::MediaWiki;

#Link detection: the upsteam subroutine is inefficient
#Here, I override it.
my $override = Sub::Override->new(
"XHTML::MediaWiki::find_links" ,sub
{
my $self = shift;
my $text = shift;
return '' unless defined $text;
#original regexp
#$text =~ s/\[\[([^\]]*)\]\]([A-Za-z0-9]*)/$self->link($1, $2, 0)/ge;
#$text =~ s/\[([a-zA-Z]+:[^\]]*)\]/$self->link($1, '', 1)/ge;

$text=~s/\[(http:\/\/\S*)\]/<a href="$1">$1<\/a>/g; # [http://www.toto.org]
$text=~s/\[(http:\/\/\S*)\s([^\]]*)\]/<a href="$1">$2<\/a>/g; # [http://www.toto.org TOTO]

$text =~ s/\[\[([^\|]*)\|([^\]]*)\]\]/<a href="$self->{link_path}.$1">$2<\/a>/g; # [[FOO|BAR]]
$text =~ s/\[\[([^\|]*)\]\]/<a href="$self->{link_path}.$1">$1<\/a>/g; # [[FOO]]

$text =~ s/[^"](http:\/\/\S*\.(html|htm|php))[^"]/<a href="$1">$1<\/a>/g; #Links left alone...
return $text;
});


#wiki page title.
my $title="Modifier_une_page_du_wiki";
$title="BacÀSable";

# April's wiki
my $raw_link="http://wiki.april.org/index.php?title=".$title ."&printable=yes&action=edit";
my $raw = get($raw_link); #Download the page

if (defined $raw)#check if hte page has been correctly downloaded
{
#modify raw data: keep only source code
$raw=~s/(.|\n)*\<textarea.+\n.+\>//;
$raw=~s/\<\/textarea(.|\n)*//;

$raw = Encode::encode( "iso-8859-1", $raw );

my $mediawiki = XHTML::MediaWiki->new( link_path => "http://wiki.april.org/" );

my $xhtm = $mediawiki->format($raw);

open (INDEX,">test.html") or die "cannot open index.html";
print INDEX $xhtm;
close(INDEX);
}
else
{
print "Oops";
}
(1-1/3)