summarylogtreecommitdiffstats
path: root/wiki.pl
blob: 07cd74b9180e6eb851d97688a2ed27f6aab7ba5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl

use strict;
use warnings;
use utf8;

use MediaWiki::API;

binmode STDOUT, ":utf8";

my $mw = MediaWiki::API->new({ api_url => 'https://en.wikiquote.org/w/api.php' });

$mw->{config}->{on_error} = \&on_error;

sub on_error {
  print "Error code: $mw->{error}->{code}\n";
  print "$mw->{error}->{stacktrace}\n";
  die;
}

# TODO fetch a specific, stable revision
# TODO add series numbers + episode names to the end?
# TODO actually scrape it into an array?

my $page = $mw->get_page( { title => 'Farscape' } );

if ($#ARGV == 0 && $ARGV[0] eq "revision") {
  print "$page->{revid}";
  exit;
}

if ($#ARGV > -1) {
  die;
}

$_ = $page->{ '*' };

# remove everything after, and including, the cast section
s/== Cast ==.*$//s;

# create seperators from horizontal rules, {,sub}section boundaries, quotes
s/^\s*<hr.+?\/>\s*$/%/gim;

s/^===.+?===$/%/gm;

s/^==.+?==$/%/gm;

s/^\s*\*\s*$/%/gm;

# remove empty lines
s/\n{2,}/\n/g;

# collapse any multiple occurances of '%'
s/%\n(%\n)+/%\n/g;

# remove everything before, and including, the first '%'
s/.*?%\n//s;

# remove leading colons
s/^://gm;

# collapse bold and italic text.
s/'''(.+?)'''/$1/g;

s/''(.+?)''/$1/g;

# replace wiki links with just the text
s/\[\[(w:.+?\|)?(.+?)\]\]/$2/g;

# remove trailing whitespace
s/\s+$//gm;
# remove preceding whitespace
s/^\s+//gm;
# collapse runs of spaces
s/(\s)\s+/$1/gm;

print;