Skip to content

Commit 8a1ff45

Browse files
committed
Apply Par margin to previous box in some cases in PGML.
Apply the spacing between paragraphs added by the Par block in in HTML to the previous block instead of inserting an empty div with a margin. This adds the margin from the Par block to the previous Indent div (including adding a div around an indent of zero), list, or list item block. Based on the code from @dpvc in #1355.
1 parent cf2cf3b commit 8a1ff45

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

macros/core/PGML.pl

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,19 +1266,22 @@ sub format {
12661266
}
12671267

12681268
sub string {
1269-
my $self = shift;
1270-
my $block = shift;
1269+
my ($self, $block) = @_;
1270+
my $stack = $block->{stack};
1271+
my $n = scalar(@$stack);
12711272
my @strings = ();
12721273
my $string;
1273-
foreach my $item (@{ $block->{stack} }) {
1274+
for my $i (0 .. $n - 1) {
1275+
my $item = $stack->[$i];
1276+
my $next_par = $i + 1 < $n && $stack->[ $i + 1 ]{type} eq 'par' ? $stack->[ $i + 1 ] : undef;
12741277
$self->{item} = $item;
12751278
$self->{nl} = (!defined($strings[-1]) || $strings[-1] =~ m/\n$/ ? "" : "\n");
12761279
for ($item->{type}) {
1277-
/indent/ && do { $string = $self->Indent($item); last };
1280+
/indent/ && do { $string = $self->Indent($item, $next_par); last };
12781281
/align/ && do { $string = $self->Align($item); last };
12791282
/par/ && do { $string = $self->Par($item); last };
1280-
/list/ && do { $string = $self->List($item); last };
1281-
/bullet/ && do { $string = $self->Bullet($item); last };
1283+
/list/ && do { $string = $self->List($item, $next_par); last };
1284+
/bullet/ && do { $string = $self->Bullet($item, $next_par); last };
12821285
/text/ && do { $string = $self->Text($item); last };
12831286
/variable/ && do { $string = $self->Variable($item, $block); last };
12841287
/command/ && do { $string = $self->Command($item); last };
@@ -1523,17 +1526,14 @@ sub Escape {
15231526
}
15241527

15251528
sub Indent {
1526-
my $self = shift;
1527-
my $item = shift;
1528-
return $self->string($item) if $item->{indent} == 0;
1529-
my $em = 2.25 * $item->{indent};
1530-
return
1531-
$self->nl
1532-
. '<div style="margin:0 0 0 '
1533-
. $em . 'em">' . "\n"
1534-
. $self->string($item)
1535-
. $self->nl
1536-
. "</div>\n";
1529+
my ($self, $item, $next_par) = @_;
1530+
my $left = $item->{indent} ? (2.25 * $item->{indent}) . 'em' : '0';
1531+
my $bottom = '0';
1532+
if ($next_par) {
1533+
$next_par->{used_on_prev_item} = 1;
1534+
$bottom = '1em';
1535+
}
1536+
return $self->nl . "<div style=\"margin: 0 0 $bottom $left;\">\n" . $self->string($item) . $self->nl . "</div>\n";
15371537
}
15381538

15391539
sub Align {
@@ -1562,20 +1562,27 @@ sub Align {
15621562
);
15631563

15641564
sub List {
1565-
my $self = shift;
1566-
my $item = shift;
1567-
my $list = $bullet{ $item->{bullet} };
1565+
my ($self, $item, $next_par) = @_;
1566+
my $list = $bullet{ $item->{bullet} };
1567+
my $margin = '0';
1568+
if ($next_par) {
1569+
$next_par->{used_on_prev_item} = 1;
1570+
$margin = '0 0 1em 0';
1571+
}
15681572
return $self->nl
15691573
. main::tag(
15701574
$list->[0],
1571-
style => "margin:0; padding-left:2.25em; $list->[1]",
1575+
style => "margin: $margin; padding-left: 2.25em; $list->[1]",
15721576
$self->string($item) . $self->nl
15731577
);
15741578
}
15751579

15761580
sub Bullet {
1577-
my $self = shift;
1578-
my $item = shift;
1581+
my ($self, $item, $next_par) = @_;
1582+
if ($next_par) {
1583+
$next_par->{used_on_prev_item} = 1;
1584+
return $self->nl . '<li style="margin-bottom: 1em;">' . $self->string($item) . "</li>\n";
1585+
}
15791586
return $self->nl . '<li>' . $self->string($item) . "</li>\n";
15801587
}
15811588

@@ -1603,9 +1610,8 @@ sub Heading {
16031610
}
16041611

16051612
sub Par {
1606-
my $self = shift;
1607-
my $item = shift;
1608-
return $self->nl . '<div style="margin-top:1em"></div>' . "\n";
1613+
my ($self, $item) = @_;
1614+
return $item->{used_on_prev_item} ? '' : $self->nl . '<div style="margin-top:1em"></div>' . "\n";
16091615
}
16101616

16111617
sub Break {"<br />\n"}

t/pg_problems/problem_file.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ is($pg->{post_header_text}, '', 'post_header_text is empty');
2121
is(
2222
$pg->{body_text},
2323
qq{<div class="PGML">\n}
24+
. qq{<div style="margin: 0 0 0 0;">\n}
2425
. qq{Enter a value for <script type="math/tex">\\pi</script>.\n}
2526
. qq{<div style="margin-top:1em"></div>\n}
2627
. qq{<div class="text-nowrap d-inline">}
@@ -29,6 +30,7 @@ is(
2930
. qq{<input id="MaThQuIlL_AnSwEr0001" name="MaThQuIlL_AnSwEr0001" type="hidden" value="">}
3031
. qq{</div>}
3132
. qq{<input name="previous_AnSwEr0001" type="hidden" value="3.14159">\n}
33+
. qq{</div>\n}
3234
. qq{</div>\n},
3335
'body_text has correct content'
3436
);

0 commit comments

Comments
 (0)