patch_vqwiki.pl

#!/usr/bin/perl

#
# Author: Vincenzo D'Amore
# Date: 24 May 2011
# Convert text pages from VQWiki format to MediaWiki (used with version 1.6.15)
# v (DOT) damore (AT) gmail (DOT) com
#

use strict;
use warnings;
use File::Basename;

if (@ARGV==0)
{
  # The number of commandline parameters is 0,
  # so print an Usage message.
  #
  print "usage: patch_wiki.pl file.txt destinationdir sourcedir\n";
  exit();   # When usage() has completed execution,
            # exit the program.
}

my $out_file = "$ARGV[1]" . basename($ARGV[0]);
my $tableFound = 0;
my $htmlFound = 0;
my $javaFound = 0;

opendir(DIR, "$ARGV[2]");
my @FILES=readdir(DIR);
my @SORTED_FILES = reverse sort { $a cmp $b } @FILES;
print $out_file;
print "\n";

open INPUT, "$ARGV[0]" or die "Cannot open file for reading : $!";
open OUTPUT, ">$out_file" or die "Cannot open file for writing : $!";

my @file = <INPUT>;

sub replaceVQWikiMarks {
	 my($line) = @_;
        $line =~ s/\n//g;
        $line =~ s/\r//g;
        $line =~ s/attach:"?([0-9a-z\-\._ ]+\.png)"?/[[Image:$1]]/gi;
        $line =~ s/attach:"?([0-9a-z\-\._ ]+\.jpg)"?/[[Image:$1]]/gi;
        $line =~ s/attach:"?([0-9a-z\-\._ ]+\.jpeg)"?/[[Image:$1]]/gi;
        $line =~ s/attach:"?([0-9a-z\-\._ ]+\.gif)"?/[[Image:$1]]/gi;
        $line =~ s/attach:"?([0-9a-z\-\._ ]+)"?/[[file:$1]]/gi;
        $line =~ s/!!!/===/g;
        $line =~ s/===([\w ]+)===/<u>$1<\/u>/g;
        $line =~ s/___//g;
        $line =~ s/!!/==/g;
        $line =~ s/^[\t ]+\*/*/gi;
        $line =~ s/^[\t ]+o/**/gi;
        $line =~ s/^   //gi;
        $line =~ s/\{\{\{/<code>/g;
        $line =~ s/\}\}\}/<\/code>/g;
        $line =~ s/@@/<\/ br>/g;
	return $line;
}

foreach my $line (@file) {
	$line = &replaceVQWikiMarks($line);

	if ($line =~ /\[<html>\]/)
	{
		$htmlFound = 1;
        	$line =~ s/\[<html>\]/\n<addhtml>/g;
	}
	if ($line =~ /\[<\/html>\]/)
	{
		$htmlFound = 0;
        	$line =~ s/\[<\/html>\]/\n<\/addhtml>/g;
	}
	if ($line =~ /\[<java>\]/)
	{
		$javaFound = 1;
        	$line =~ s/\[<java>\]/<syntaxhighlight lang="java" line start="100" highlight="5">/g;
	}
	if ($line =~ /\[<\/java>\]/)
	{
		$javaFound = 0;
        	$line =~ s/\[<\/java>\]/<\/syntaxhighlight>/g;
	}
	if ($line =~ /####/)
	{
		$tableFound = !$tableFound;
		if ($tableFound) {
			$line =~ s/####/\n\{\|/g
		} else {
			$line =~ s/####/\|\}\n/g
		}
	} elsif ($tableFound) {
               $line =~ s/^/\|/g;
               $line =~ s/##$/\n|-/g;
               $line =~ s/##/\|\|/g ;
        #       print $line."\n";
        }
	if ((!$htmlFound) && (!$javaFound) &&(!$tableFound)) {
		$line =~ s/(__|\[\[)([A-Z][A-Za-z]+)(__|\]\])/$2/g;
		foreach my $filename (@SORTED_FILES) {
			next if $filename=~/^\.\.?/;
			$filename =~ s/\.txt//g;
                        $line =~ s/\b$filename\b/[[$filename]]/g;
		}
		while ($line =~ /(\[\[)(.*)(\[\[)(.*)(\]\])(.*)(\]\])/) {
			$line =~ s/(\[\[)(.*)(\]\])(.*)(\]\])/$1$2$4$5/g;
			$line =~ s/(\[\[)(.*)(\[\[)(.*)(\]\])/$1$2$4$5/g;
		}
		if ($line =~ /==/) {
			print OUTPUT $line."\n";
		} else {
			print OUTPUT $line."<br />\n";
		}
	}
	else
	{
		print OUTPUT $line."\n";
	}
}
  1. Non c'è ancora nessun commento.
  1. No trackbacks yet.

Lascia un commento

Effettua il login con uno di questi metodi per inviare il tuo commento:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: