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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
# Copyright (c) 1997-2024
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://polymake.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, or (at your option) any
# later version: http://www.gnu.org/licenses/gpl.txt.
#
# 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.
#-------------------------------------------------------------------------------
use strict;
use namespaces;
use warnings qw(FATAL void syntax misc);
require XML::Writer;
package Polymake::Core::Help::HTML;
declare $xhtmlns="http://www.w3.org/1999/xhtml";
sub no_resolve { '#' }
use Polymake::Struct (
[ new => '' ],
[ '$writer' => 'undef' ],
'$buf',
[ '&resolve_ref' => '\&no_resolve' ],
);
sub new {
my $self=&_new;
$self->writer=new XML::Writer(OUTPUT => \($self->buf), NAMESPACES => 1, PREFIX_MAP => { $xhtmlns=>"" },
DATA_MODE => 0, DATA_INDENT => 0, ENCODING => 'utf-8', UNSAFE => 1);
$self->writer->xmlDecl;
$self->writer->startTag([ $xhtmlns, "html" ]);
$self->writer->startTag([ $xhtmlns, "body" ]);
$self
}
sub text {
my ($self)=@_;
if (defined $self->writer) {
$self->writer->endTag; # </body>
$self->writer->endTag; # </html>
undef $self->writer;
}
$self->buf
}
use Polymake::Struct (
[ 'alt.constructor' => 'new_fragment' ],
[ new => '$$' ],
[ '$writer' => '#1' ],
[ '$resolve_ref' => '#2' ],
);
sub convertDokuWiki {
my ($self, $text)=@_;
while ($text =~ m{\G (.*?) (?: (''|__|\*\*|//) (.*?) \2
| \[\[ (.*?) (?: \| (.*?) )? \]\]
| $ )
}xgs) {
$self->writer->characters($1) if length($1);
if (defined $2) {
my @decor;
if ($2 eq "''") {
@decor=('code');
} elsif ($2 eq "**") {
@decor=('strong');
} elsif ($2 eq "//") {
@decor=('em', class => 'param');
} else {
@decor=('em', class => 'u');
}
$self->writer->startTag([ $xhtmlns, shift @decor ], @decor);
convertDokuWiki($self, $3);
$self->writer->endTag;
} elsif (defined $4) {
my ($ref, $text)=($4, $5);
$ref =~ s/^\s* (.*?) \s*$/$1/x;
$self->writer->startTag([ $xhtmlns, 'a' ], href => $self->resolve_ref->($ref));
if (defined $text) {
convertDokuWiki($self, $text);
} else {
$self->writer->characters($ref);
}
$self->writer->endTag;
}
}
}
sub writeText {
my ($self, $text)=@_;
my $verbatim=0;
foreach (split m{(&$id_re; | </? (?: su[bp] ) > | </span> | < (?: span (?:\s+ $id_re = ".*")?) > )}ox, $text) {
if ($verbatim) {
if (substr($_,0,1) eq "&") {
$self->writer->getOutput->print($_);
} elsif (substr($_,1,1) eq "/") {
$self->writer->endTag;
} else {
$_ =~ /<( $id_re )(?:\s? ( $id_re )="(.*)" )?>/xo;
#/$id_re/o;
if (defined($2)){
$self->writer->startTag([ $xhtmlns, $1 ], $2=>$3);
} else {
$self->writer->startTag([ $xhtmlns, $1 ]);
}
}
} elsif (length) {
convertDokuWiki($self, $_);
}
$verbatim^=1;
}
}
sub writeBlock {
my ($self, $text, $para_tag)=@_;
my $save_mode=$self->writer->getDataMode()
and $self->writer->setDataMode(0);
while ($text =~ /\G(.*?)(?:\n\n|\Z|((?=^ *\t)))/msg) {
if (length($1)>1) {
$self->writer->startTag([ $xhtmlns, $para_tag ]);
writeText($self, $1);
$self->writer->endTag;
}
if ($para_tag eq "p" && defined $2) {
$self->writer->startTag([ $xhtmlns, "blockquote" ]);
while ($text =~ /\G^ *\t(.*)\n/mgc) {
$self->writer->startTag([ $xhtmlns, "div" ]);
writeText($self, $1);
$self->writer->endTag;
}
$self->writer->endTag;
}
}
$save_mode and $self->writer->setDataMode($save_mode);
}
sub add_separator {
my ($self)=@_;
$self->writer->emptyTag([ $xhtmlns, "hr" ]);
}
sub header {
writeBlock(@_, "p");
}
sub description {
writeBlock(@_, "p");
}
sub specialized {
writeBlock(@_, "div");
}
sub depends {
my ($self, $depends)=@_;
writeBlock($self, "Depends on: $depends", "div");
}
sub example {
my ($self, $example)=@_;
my $save_mode=$self->writer->getDataMode()
and $self->writer->setDataMode(0);
my @buffer;
foreach my $line (split /\n/, $example) {
# match > or | at the beginning but check > with lookahead to keep it in $'
if ($line =~ /^\s* (?: \| | (?= > ) ) /x) {
$line = $';
# escapes for html output, we dont want to parse the code sections as descriptions
# due to stuff like [[1,2,3]] and to avoid [[]] around all types,...
$line =~ s/&/&/g;
$line =~ s/</</g;
$line =~ s/>/>/g;
if (@buffer > 0) {
writeText($self, join("\n", @buffer));
@buffer = ();
}
# empty code tags cause display problems
$line = " " if ($line eq "");
$self->writer->startTag([ $xhtmlns, "code" ]);
$self->writer->getOutput->print($line);
$self->writer->endTag;
} else {
push @buffer, $line;
}
}
writeText($self, join("\n", @buffer)) if (@buffer > 0);
$save_mode and $self->writer->setDataMode($save_mode);
}
sub examples {
my ($self, $examples) = @_;
writeBlock($self, @$examples > 1 ? "Examples:" : "Example:", "p");
foreach (@$examples) {
$self->writer->startTag([ $xhtmlns, "p" ]);
example($self, $_->body);
$self->writer->endTag;
}
}
sub function_full {
my ($self, $text)=@_;
writeBlock($self, $text, "p");
$self->writer->startTag([ $xhtmlns, "table" ]);
}
sub function_brief {
my ($self, @options)=@_;
if (@options) {
writeBlock($self, "Options: ".join(" ", @options), "div");
}
}
sub writeTableHeader {
my ($self, $text)=@_;
$self->writer->startTag([ $xhtmlns, "tr" ]);
$self->writer->startTag([ $xhtmlns, "th" ], colspan => 3, align => 'left');
$self->writer->characters($text);
$self->writer->endTag;
$self->writer->endTag;
}
sub writeTableRow {
my ($self, $type, $name, $text, $values)=@_;
$self->writer->startTag([ $xhtmlns, "tr" ], valign => 'top');
$self->writer->dataElement([ $xhtmlns, "td" ], $type);
if (defined($name)) {
$self->writer->dataElement([ $xhtmlns, "td" ], $name);
} else {
$self->writer->emptyTag([ $xhtmlns, "td" ]);
}
$self->writer->startTag([ $xhtmlns, "td" ]);
writeBlock($self, $text, "div");
if (defined($values)) {
write_possible_values($self, $values);
}
$self->writer->endTag;
$self->writer->endTag;
}
sub write_possible_values {
my ($self, $values)=@_;
writeBlock($self, "Possible values:", "div");
my $save_mode=$self->writer->getDataMode()
and $self->writer->setDataMode(0);
$self->writer->startTag([ $xhtmlns, "dl" ]);
foreach my $value (@$values) {
$self->writer->startTag([ $xhtmlns, "dt" ]);
writeText($self, $value->value);
$self->writer->endTag;
$self->writer->startTag([ $xhtmlns, "dd" ]);
writeText($self, $value->text);
$self->writer->endTag;
}
$self->writer->endTag;
$save_mode and $self->writer->setDataMode($save_mode);
}
sub type_params {
my $self = shift;
writeTableHeader($self, "Type Parameters:");
foreach (@_) {
writeTableRow($self, $_->name, "", $_->text);
}
}
sub function_args {
my ($self, $args) = @_;
writeTableHeader($self, "Arguments:");
foreach (@$args) {
writeTableRow($self, $_->type, $_->name, $_->text, $_->values);
}
}
sub function_options {
my ($self, $comment, @options) = @_;
writeTableHeader($self, "Options:");
if (length($comment)) {
$self->writer->startTag([ $xhtmlns, "tr" ]);
$self->writer->startTag([ $xhtmlns, "td" ], colspan => 3, align => 'left');
writeBlock($self, $comment, "div");
$self->writer->endTag;
$self->writer->endTag;
}
foreach (@options) {
writeTableRow($self, $_->name." =>", $_->type, $_->text);
}
}
sub function_return {
my ($self, $return) = @_;
if (defined($return)) {
writeTableHeader($self, "Returns:");
writeTableRow($self, $return->type, "", $return->text // "");
}
$self->writer->endTag; # close the table with arguments
}
sub topics_keys {
my ($self) = @_;
$self->writer->startTag([ $xhtmlns, "table" ]);
foreach (@_) {
writeTableRow($self, $_->name." =>", $_->type, $_->text);
}
$self->writer->endTag;
}
1
# Local Variables:
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|