Jump to content

User:Derbeth/Print Version Creator

From Wikibooks, open books for an open world

Save this script to 'pvc.pl' file. You will need Perl to run it.

#!/usr/bin/perl
#
# Derbeth's Fast Wikibook Print Version Creator
#   Perl script creating wikicode of print version of a book from the book's TOC wikicode.
#
# Version:
#   0.5.2 (6 January 2007)
#
# Author:
#   Derbeth <http://en.wikibooks.org/wiki/User:Derbeth>
#
# License:
#   2-clause BSD, http://www.opensource.org/licenses/bsd-license.php
#
# Usage:
#   1. Copy & paste wikicode of the book table of contents into 'in.txt' file
#   1.1 You can optionally specify book name adding first line like: '!bookname somename' to 'in.txt'
#       or changing value of variable marked by BOOKNAME HERE in this file
#   1.2 You can optionally set language version by changing $language variable
#   2. Run the script ('perl pvc.pl' or './pvc.pl')
#   3. Read 'out.txt' file 

use strict;

open INPUT, "<in.txt";
open OUTPUT, ">out.txt";

my $bookname='somename'; # BOOKNAME HERE
my $printed_intro=0;

my $language='en'; # 'en' or 'pl'

while( <INPUT> ) {
	chomp;
	
	if( /!bookname\s+(.+)/ ) {
		$bookname = $1;
		$bookname =~ s/\s+$//;
	}
	
	if( /\[\[(.+)\]\]/ ) {
		my $match = $1;
		my ($fulllink,$title);
		
		# ignore images, categories etc.
		if( $match =~ /^:?(Image|Grafika|Bild|Media|Category|Kategoria|Kategorie)/ ) { next; }
		
		unless( $printed_intro ) { &print_intro; $printed_intro = 1; }
		
		if( $match =~ /^\/(.+)\/$/ ) {  # [[/Chapter/]]
			($fulllink,$title) = ("$bookname/$1", $1);
		} else { # [[Book/Chapter|Chapter]] or [[Book:Chapter|Chapter]]
			($fulllink,$title) = split /\|/,$match,2;
			unless($title) { $title = $fulllink; }
		}

		if( $fulllink eq '' ) { next; }
		print OUTPUT "=$title=\n{{:$fulllink}}\n\n";
	}
}

&print_outro;
print OUTPUT "<!-- Created with http://en.wikibooks.org/wiki/User:Derbeth/Print_Version_Creator -->\n";

sub print_intro {
	SWITCH: {
		if( $language eq 'pl' ) { &print_intro_pl; last; }
		&print_intro_en;
	}
}

sub print_outro {
	SWITCH: {
		if( $language eq 'pl' ) { &print_outro_pl; last; }
		&print_outro_en;
	}
}

sub print_intro_en {
	my $url_title = $bookname;
	$url_title =~ s/ /_/g;
	print OUTPUT "{{print version notice|$bookname}}\n\n";
	print OUTPUT '<div style="font-size: 400%; font-weight: bold; text-align: center; margin-bottom: 2em;">';
	print OUTPUT "$bookname</div>\n\n<div style=\"text-align: center;\">";
	print OUTPUT 'Current, editable version of this book is available in Wikibooks, ';
	print OUTPUT "collection of open-content textbooks at URL:<br>\n";
	print OUTPUT "http://en.wikibooks.org/wiki/$url_title\n\n";
	print OUTPUT "The whole text is available under GNU Free Documentation License.\n";
	print OUTPUT "</div>\n\n----\n\n";
}

sub print_outro_en {
	print OUTPUT "=GNU Free Documentation License=\n{{:Wikibooks:GNU Free Documentation License}}\n\n";
	print OUTPUT "__NOTOC__\n";
}

sub print_outro_pl {
	print OUTPUT "=GNU Free Documentation License=\n{{:GNU Free Documentation License}}\n\n";
	print OUTPUT "__NOTOC__\n";
}

sub print_intro_pl {
	my $url_title = $bookname;
	$url_title =~ s/ /_/g;
	print OUTPUT "{{drukowanie porada|$bookname}}\n\n";
	print OUTPUT '<div style="font-size: 400%; font-weight: bold; text-align: center; margin-bottom: 2em;">';
	print OUTPUT "$bookname</div>\n\n<div style=\"text-align: center;\">";
	print OUTPUT 'Aktualna, edytowalna wersja tego podręcznika jest dostępna w Wikibooks, ';
	print OUTPUT "bibliotece wolnych podręczników pod adresem<br>\n";
	print OUTPUT "http://pl.wikibooks.org/wiki/$url_title\n\n";
	print OUTPUT "Całość tekstu jest objęta licencją GNU Free Documentation License.\n";
	print OUTPUT "</div>\n\n----\n\n";
}

See also

[edit | edit source]