| 1 | package Alvis::Wikipedia::Variables; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | $Alvis::Wikipedia::Variables::VERSION='0.1'; | ||
| 7 | |||
| 8 | use Alvis::Wikipedia::WikitextParser; | ||
| 9 | use Alvis::Wikipedia::Templates; | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | # | ||
| 13 | # Global variables | ||
| 14 | # | ||
| 15 | ####################################################################### | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | ############################################################################ | ||
| 20 | # | ||
| 21 | # Error message stuff | ||
| 22 | # | ||
| 23 | ############################################################################ | ||
| 24 | |||
| 25 | my ($ERR_OK, | ||
| 26 | $ERR_PARSER, | ||
| 27 | $ERR_TEMPL, | ||
| 28 | $ERR_SEP, | ||
| 29 | $ERR_TEMPL_ADD, | ||
| 30 | $ERR_EXP, | ||
| 31 | $ERR_TEMPL_DUMP, | ||
| 32 | $ERR_TEMPL_LOAD | ||
| 33 | )=(0..7); | ||
| 34 | my %ErrMsgs=($ERR_OK=>"", | ||
| 35 | $ERR_PARSER=>"Unable to instantiate WikiLiki::WikitextParser.", | ||
| 36 | $ERR_TEMPL=>"Unable to instantiate WikiLiki::Templates.", | ||
| 37 | $ERR_SEP=>"Separation of markup failed.", | ||
| 38 | $ERR_TEMPL_ADD=>"Adding the definition of a template failed.", | ||
| 39 | $ERR_EXP=>"Variable expansion failed.", | ||
| 40 | $ERR_TEMPL_DUMP=>"Dumping the templates failed.", | ||
| 41 | $ERR_TEMPL_LOAD=>"Loading the templates failed." | ||
| 42 | ); | ||
| 43 | |||
| 44 | sub _set_err_state | ||
| 45 | { | ||
| 46 | my $self=shift; | ||
| 47 | my $errcode=shift; | ||
| 48 | my $errmsg=shift; | ||
| 49 | |||
| 50 | if (!defined($errcode)) | ||
| 51 | { | ||
| 52 | confess("set_err_state() called with an undefined argument."); | ||
| 53 | } | ||
| 54 | |||
| 55 | if (exists($ErrMsgs{$errcode})) | ||
| 56 | { | ||
| 57 | if ($errcode==$ERR_OK) | ||
| 58 | { | ||
| 59 | $self->{errstr}=""; | ||
| 60 | } | ||
| 61 | else | ||
| 62 | { | ||
| 63 | $self->{errstr}.=" " . $ErrMsgs{$errcode}; | ||
| 64 | if (defined($errmsg)) | ||
| 65 | { | ||
| 66 | $self->{errstr}.=" " . $errmsg; | ||
| 67 | } | ||
| 68 | |||
| 69 | } | ||
| 70 | } | ||
| 71 | else | ||
| 72 | { | ||
| 73 | confess("Internal error: set_err_state() called with an " . | ||
| 74 | "unrecognized argument ($errcode).") | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub clearerr | ||
| 79 | { | ||
| 80 | my $self=shift; | ||
| 81 | |||
| 82 | $self->{errstr}=""; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub errmsg | ||
| 86 | { | ||
| 87 | my $self=shift; | ||
| 88 | |||
| 89 | return $self->{errstr}; | ||
| 90 | } | ||
| 91 | |||
| 92 | ########################################################################## | ||
| 93 | # | ||
| 94 | # Public methods | ||
| 95 | # | ||
| 96 | ########################################################################## | ||
| 97 | |||
| 98 | sub new | ||
| 99 | { | ||
| 100 | my $proto=shift; | ||
| 101 | |||
| 102 | my $class=ref($proto)||$proto; | ||
| 103 | my $parent=ref($proto)&&$proto; | ||
| 104 | my $self={}; | ||
| 105 | bless($self,$class); | ||
| 106 | |||
| 107 | $self->_set_err_state($ERR_OK); | ||
| 108 | |||
| 109 | $self->_init(@_); | ||
| 110 | |||
| 111 | $self->{parser}=Alvis::Wikipedia::WikitextParser->new(); | ||
| 112 | if (!defined($self->{parser})) | ||
| 113 | { | ||
| 114 | $self->_set_err_state($ERR_PARSER); | ||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | $self->{templates}=Alvis::Wikipedia::Templates->new(); | ||
| 118 | if (!defined($self->{templates})) | ||
| 119 | { | ||
| 120 | $self->_set_err_state($ERR_TEMPL); | ||
| 121 | return undef; | ||
| 122 | } | ||
| 123 | |||
| 124 | return $self; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _init | ||
| 128 | { | ||
| 129 | my $self=shift; | ||
| 130 | |||
| 131 | if (defined(@_)) | ||
| 132 | { | ||
| 133 | my %args=@_; | ||
| 134 | @$self{ keys %args }=values(%args); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | sub add_template | ||
| 139 | { | ||
| 140 | my $self=shift; | ||
| 141 | my $name=shift; | ||
| 142 | my $def=shift; | ||
| 143 | |||
| 144 | if (!$self->{templates}->add($name,$def)) | ||
| 145 | { | ||
| 146 | $self->_set_err_state($ERR_TEMPL_ADD, | ||
| 147 | "Name:\"$name\", Definition:\"$def\"."); | ||
| 148 | return 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub dump_templates | ||
| 155 | { | ||
| 156 | my $self=shift; | ||
| 157 | my $f=shift; | ||
| 158 | |||
| 159 | if (!$self->{templates}->dump($f)) | ||
| 160 | { | ||
| 161 | $self->_set_err_state($ERR_TEMPL_DUMP,$self->{templates}->errmsg()); | ||
| 162 | return 0; | ||
| 163 | } | ||
| 164 | |||
| 165 | return 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub load_templates | ||
| 169 | { | ||
| 170 | my $self=shift; | ||
| 171 | my $f=shift; | ||
| 172 | |||
| 173 | if (!$self->{templates}->load($f)) | ||
| 174 | { | ||
| 175 | $self->_set_err_state($ERR_TEMPL_LOAD,$self->{templates}->errmsg()); | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub expand | ||
| 183 | { | ||
| 184 | my $self=shift; | ||
| 185 | my $namespace=shift; | ||
| 186 | my $title=shift; | ||
| 187 | my $text=shift; | ||
| 188 | my $expand_templates_for_real=shift; # do we expand the templates fully? | ||
| 189 | |||
| 190 | # | ||
| 191 | # Problems: <math>,<nowiki>...safeguard them | ||
| 192 | # | ||
| 193 | my $sep_text=$self->{parser}->separate_markup($text); | ||
| 194 | if (!defined($sep_text)) | ||
| 195 | { | ||
| 196 | $self->_set_err_state($ERR_SEP,"Text:\"$text\""); | ||
| 197 | return ""; | ||
| 198 | } | ||
| 199 | |||
| 200 | my $exp_text=""; | ||
| 201 | |||
| 202 | for my $s (@$sep_text) | ||
| 203 | { | ||
| 204 | my ($type,$t)=@$s; | ||
| 205 | |||
| 206 | if ($type eq $Alvis::Wikipedia::WikitextParser::MARKUP) | ||
| 207 | { | ||
| 208 | # warn "MARKUP TO EXPAND:$t\n"; | ||
| 209 | |||
| 210 | my $exp_t=$self->{templates}->expand($namespace,$title,$t, | ||
| 211 | $expand_templates_for_real); | ||
| 212 | if (!defined($exp_t)) | ||
| 213 | { | ||
| 214 | $self->_set_err_state($ERR_EXP,"Text:\"$t\""); | ||
| 215 | return undef; | ||
| 216 | } | ||
| 217 | else | ||
| 218 | { | ||
| 219 | $exp_text.=$exp_t; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | else | ||
| 223 | { | ||
| 224 | $exp_text.=$t; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | return $exp_text; | ||
| 229 | } | ||
| 230 | |||
| 231 | 1; |