Skip to content

Commit a013860

Browse files
committed
Refactor the PGML::format string loop.
As suggested by @dvpc, refactor the PGML::format::string loop to call the appropriate method if it exists. Also all methods now have access to the block and current state of the loop to use as needed.
1 parent fc008f1 commit a013860

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

macros/core/PGML.pl

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,45 +1267,21 @@ sub format {
12671267

12681268
sub string {
12691269
my ($self, $block) = @_;
1270-
my $stack = $block->{stack};
1271-
my @strings = ();
1272-
my $string;
1273-
for my $i (0 .. $#$stack) {
1274-
my $item = $stack->[$i];
1275-
my $next_par = $i < $#$stack && $stack->[ $i + 1 ]{type} eq 'par' ? $stack->[ $i + 1 ] : undef;
1276-
$self->{item} = $item;
1277-
$self->{nl} = (!defined($strings[-1]) || $strings[-1] =~ m/\n$/ ? "" : "\n");
1278-
for ($item->{type}) {
1279-
/indent/ && do { $string = $self->Indent($item, $next_par); last };
1280-
/align/ && do { $string = $self->Align($item); last };
1281-
/par/ && do { $string = $self->Par($item); last };
1282-
/list/ && do { $string = $self->List($item, $next_par); last };
1283-
/bullet/ && do { $string = $self->Bullet($item, $next_par); last };
1284-
/text/ && do { $string = $self->Text($item); last };
1285-
/variable/ && do { $string = $self->Variable($item, $block); last };
1286-
/command/ && do { $string = $self->Command($item); last };
1287-
/math/ && do { $string = $self->Math($item); last };
1288-
/answer/ && do { $string = $self->Answer($item); last };
1289-
/bold/ && do { $string = $self->Bold($item); last };
1290-
/italic/ && do { $string = $self->Italic($item); last };
1291-
/heading/ && do { $string = $self->Heading($item); last };
1292-
/quote/ && do { $string = $self->Quote($item, $strings[-1] || ''); last };
1293-
/rule/ && do { $string = $self->Rule($item); last };
1294-
/code/ && do { $string = $self->Code($item); last };
1295-
/pre/ && do { $string = $self->Pre($item); last };
1296-
/verbatim/ && do { $string = $self->Verbatim($item); last };
1297-
/image/ && do { $string = $self->Image($item); last };
1298-
/break/ && do { $string = $self->Break($item); last };
1299-
/forced/ && do { $string = $self->Forced($item); last };
1300-
/comment/ && do { $string = $self->Comment($item); last };
1301-
/table/ && do { $string = $self->Table($item); last };
1302-
/tag/ && do { $string = $self->Tag($item); last };
1270+
my $stack = $block->{stack};
1271+
my $state = { strings => [], i => 0 };
1272+
while ($state->{i} <= $#$stack) {
1273+
my $item = $self->{item} = $stack->[ $state->{i}++ ];
1274+
my $method = ucfirst($item->{type});
1275+
$self->{nl} = (!defined($state->{strings}[-1]) || $state->{strings}[-1] =~ m/\n$/ ? '' : "\n");
1276+
if (Value::can($self, $method)) {
1277+
my $string = $self->$method($item, $block, $state);
1278+
push(@{ $state->{strings} }, $string) unless !defined $string || $string eq '';
1279+
} else {
13031280
PGML::Warning "Warning: unknown block type '$item->{type}' in " . ref($self) . "::format\n";
13041281
}
1305-
push(@strings, $string) unless (!defined $string || $string eq '');
13061282
}
1307-
$self->{nl} = (!defined($strings[-1]) || $strings[-1] =~ m/\n$/ ? "" : "\n");
1308-
return join('', @strings);
1283+
$self->{nl} = (!defined($state->{strings}[-1]) || $state->{strings}[-1] =~ m/\n$/ ? '' : "\n");
1284+
return join('', @{ $state->{strings} });
13091285
}
13101286

13111287
sub nl {
@@ -1525,13 +1501,13 @@ sub Escape {
15251501
}
15261502

15271503
sub Indent {
1528-
my ($self, $item, $next_par) = @_;
1504+
my ($self, $item, $block, $state) = @_;
15291505
return $self->string($item) if $item->{indent} == 0;
15301506
my $em = 2.25 * $item->{indent};
15311507
my $bottom = '0';
1532-
if ($next_par) {
1533-
$next_par->{used_on_prev_item} = 1;
1508+
if (defined $block->{stack}[ $state->{i} ] && $block->{stack}[ $state->{i} ]{type} eq 'par') {
15341509
$bottom = '1em';
1510+
$state->{i}++;
15351511
}
15361512
return $self->nl . "<div style=\"margin: 0 0 $bottom ${em}em;\">\n" . $self->string($item) . $self->nl . "</div>\n";
15371513
}
@@ -1562,12 +1538,12 @@ sub Align {
15621538
);
15631539

15641540
sub List {
1565-
my ($self, $item, $next_par) = @_;
1541+
my ($self, $item, $block, $state) = @_;
15661542
my $list = $bullet{ $item->{bullet} };
15671543
my $margin = '0';
1568-
if ($next_par) {
1569-
$next_par->{used_on_prev_item} = 1;
1544+
if (defined $block->{stack}[ $state->{i} ] && $block->{stack}[ $state->{i} ]{type} eq 'par') {
15701545
$margin = '0 0 1em 0';
1546+
$state->{i}++;
15711547
}
15721548
return $self->nl
15731549
. main::tag(
@@ -1578,9 +1554,9 @@ sub List {
15781554
}
15791555

15801556
sub Bullet {
1581-
my ($self, $item, $next_par) = @_;
1582-
if ($next_par) {
1583-
$next_par->{used_on_prev_item} = 1;
1557+
my ($self, $item, $block, $state) = @_;
1558+
if (defined $block->{stack}[ $state->{i} ] && $block->{stack}[ $state->{i} ]{type} eq 'par') {
1559+
$state->{i}++;
15841560
return $self->nl . '<li style="margin-bottom: 1em;">' . $self->string($item) . "</li>\n";
15851561
}
15861562
return $self->nl . '<li>' . $self->string($item) . "</li>\n";
@@ -1610,8 +1586,9 @@ sub Heading {
16101586
}
16111587

16121588
sub Par {
1613-
my ($self, $item) = @_;
1614-
return $item->{used_on_prev_item} ? '' : $self->nl . '<div style="margin-top:1em"></div>' . "\n";
1589+
my $self = shift;
1590+
my $item = shift;
1591+
return $self->nl . '<div style="margin-top:1em"></div>' . "\n";
16151592
}
16161593

16171594
sub Break {"<br />\n"}
@@ -1632,10 +1609,9 @@ sub Italic {
16321609
our %closeQuote = ('"' => "&#x201D;", "'" => "&#x2019;");
16331610

16341611
sub Quote {
1635-
my $self = shift;
1636-
my $item = shift;
1637-
my $string = shift;
1638-
return $openQuote{ $item->{token} } if $string eq "" || $string =~ m/(^|[ ({\[\s])$/;
1612+
my ($self, $item, $block, $state) = @_;
1613+
my $string = $state->{strings}[-1] // '';
1614+
return $openQuote{ $item->{token} } if $string eq '' || $string =~ m/(^|[ ({\[\s])$/;
16391615
return $closeQuote{ $item->{token} };
16401616
}
16411617

@@ -1811,9 +1787,8 @@ sub Italic {
18111787
our %closeQuote = ('"' => "''", "'" => "'");
18121788

18131789
sub Quote {
1814-
my $self = shift;
1815-
my $item = shift;
1816-
my $string = shift;
1790+
my ($self, $item, $block, $state) = @_;
1791+
my $string = $state->{strings}[-1] // '';
18171792
return $openQuote{ $item->{token} } if $string eq "" || $string =~ m/(^|[ ({\[\s])$/;
18181793
return $closeQuote{ $item->{token} };
18191794
}
@@ -1968,9 +1943,8 @@ sub Italic {
19681943
our %closeQuote = ('"' => "<rq/>", "'" => "<rsq/>");
19691944

19701945
sub Quote {
1971-
my $self = shift;
1972-
my $item = shift;
1973-
my $string = shift;
1946+
my ($self, $item, $block, $state) = @_;
1947+
my $string = $state->{strings}[-1] // '';
19741948
return $openQuote{ $item->{token} } if $string eq "" || $string =~ m/(^|[ ({\[\s])$/;
19751949
return $closeQuote{ $item->{token} };
19761950
}

0 commit comments

Comments
 (0)