Skip to content

Commit

Permalink
Merge pull request openwebwork#1042 from drgrice1/pgml-tag
Browse files Browse the repository at this point in the history
Add a PGML tag block.
  • Loading branch information
pstaabp authored Apr 10, 2024
2 parents 8816570 + ee33ad2 commit 97fa82a
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions macros/core/PGML.pl
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ sub Rule {
my $self = shift;
my $token = shift;
if ($self->{atLineStart}) {
### check for line end or braces
# check for line end or braces
$self->Item("rule", $token, { options => [ "width", "height", "size" ] });
$self->{ignoreNL} = 1;
} else {
Expand Down Expand Up @@ -690,13 +690,11 @@ sub NOOP {
options => [ "source", "width", "height", "image_options" ]
},
"[<" => {
type => 'link',
parseComments => 1,
parseSubstitutions => 1,
terminator => qr/>\]/,
terminateMethod => 'terminateGetString',
cancelNL => 1,
options => [ "text", "title" ]
type => 'tag',
parseAll => 1,
isContainer => 1,
terminator => qr/>\]/,
options => [qw(html tex ptx)]
},
"[%" => { type => 'comment', parseComments => 1, terminator => qr/%\]/, allowPar => 1 },
"[\@" => {
Expand Down Expand Up @@ -1282,6 +1280,7 @@ sub string {
/forced/ && do { $string = $self->Forced($item); last };
/comment/ && do { $string = $self->Comment($item); last };
/table/ && do { $string = $self->Table($item); last };
/tag/ && do { $string = $self->Tag($item); last };
PGML::Warning "Warning: unknown block type '$item->{type}' in " . ref($self) . "::format\n";
}
push(@strings, $string) unless (!defined $string || $string eq '');
Expand Down Expand Up @@ -1338,6 +1337,11 @@ sub Table {
return ($item->{hasStar} ? main::LayoutTable($table, @options) : main::DataTable($table, @options));
}

sub Tag {
my ($self, $item) = @_;
return $self->string($item);
}

sub Math {
my $self = shift;
my $item = shift;
Expand Down Expand Up @@ -1645,6 +1649,27 @@ sub Math {
return main::general_math_ev3($self->SUPER::Math(@_));
}

sub Tag {
my ($self, $item) = @_;
my %whitelist = (div => 1, span => 1);
my @attributes = ref($item->{html}) eq 'ARRAY' ? @{ $item->{html} } : $item->{html};
my $tag = @attributes % 2 ? (shift @attributes // 'div') : 'div';
unless ($whitelist{$tag}) {
PGML::Warning qq{The tag "$tag" is not allowed};
return $self->string($item);
}
if ($tag eq 'span') {
for my $subblock (@{ $item->{stack} }) {
if ($subblock->{type} =~ /^(indent|align|par|list|bullet|answer|heading|rule|code|pre|verbatim|table|tag)$/)
{
PGML::Warning qq{A "span" tag may not contain a $subblock->{type}};
return $self->string($item);
}
}
}
return main::tag($tag, @attributes, $self->string($item));
}

######################################################################
######################################################################

Expand Down Expand Up @@ -1789,6 +1814,17 @@ sub Math {
return main::general_math_ev3($self->SUPER::Math(@_));
}

sub Tag {
my ($self, $item) = @_;
my ($tex_begin, $tex_end);
if (ref($item->{tex}) eq 'ARRAY') {
($tex_begin, $tex_end) = @{ $item->{tex} };
} elsif ($item->{tex}) {
($tex_begin, $tex_end) = ("\\begin{$item->{tex}}", "\\end{$item->{tex}}");
}
return '{' . ($tex_begin // '') . $self->string($item) . ($tex_end // '') . '}';
}

######################################################################
######################################################################

Expand Down Expand Up @@ -1936,6 +1972,15 @@ sub Math {
return main::general_math_ev3($self->SUPER::Math(@_));
}

sub Tag {
my ($self, $item) = @_;
my @args = ref($item->{ptx}) eq 'ARRAY' ? @{ $item->{ptx} } : $item->{ptx};
if (my $tag = shift @args) {
return NiceTables::tag($self->string($item), $tag, @args);
}
return $self->string($item);
}

######################################################################
######################################################################

Expand Down

0 comments on commit 97fa82a

Please sign in to comment.