From 987c3b5a6e0b6a800c354725d214d71e5098c1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 4 Jan 2021 17:03:41 +0100 Subject: Change multi-line format specification syntax --- rf | 44 ++++++++++++++++++++------------------------ rf.1 | 29 ++++++++++++++--------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/rf b/rf index 4025108..bb9343c 100755 --- a/rf +++ b/rf @@ -7,8 +7,8 @@ use warnings; my %fmt; $fmt{r} = '%a {(%y)}. {\fI%t\fR}. {\*Q%q\*U}. %n, %d. %c: %p. {Available: %w}'; -$fmt{f} = "\n(%A %y)"; -$fmt{x} = "\n(%y)"; +$fmt{f} = "(%A %y)"; +$fmt{x} = "(%y)"; # Internal variables @@ -21,16 +21,19 @@ while (<>) { chomp; # Format specification - if (/^\.F([rfx])(\\?) +(.*)/) { - my ($type, $join, $rest) = ($1, $2, $3); - $rest = "\n$rest" if not $join; - while (not eof()) { - last if not $rest =~ s/\\$//; - $rest .= "\n" . <>; - chomp $rest; + if (/^\.F([rfx])( +.*)?/) { + my ($type, $def) = ($1, $2); + if ($def) { + $def =~ s/^ +//; + } else { + $def = ''; + while (<>) { + last if /^\.\.$/; + $def .= $_; + } } - chomp $rest; - $fmt{$type} = $rest; + chomp $def; + $fmt{$type} = $def; next; } @@ -69,11 +72,9 @@ for (my $i = $#lines; $i >= 0; $i--) { if (/^\.R([fx]) +(.*)/) { my ($fld, $rest) = ($1, $2, $3); - $inlref = 1; - # find potential prefix/suffix my ($suffix, $prefix); - if ($rest =~ s/ ([.,?!:;\])]) ?([\[(])?$//) { + if ($rest =~ s/ ([.,?!:;\])]+) ?([\[(]*)$//) { ($suffix, $prefix) = ($1, $2); } @@ -131,9 +132,10 @@ for (my $i = $#lines; $i >= 0; $i--) { $prefix = '' if not $prefix; $suffix = '' if not $suffix; push @r, "\n"; - if ($n != 0) { - push @r, substr($fmt, index($fmt, "\n")); - push @r, $prefix . substr($fmt, 0, index($fmt, "\n")) . "$suffix"; + if ($n > 0) { + my %affix = (2 => $suffix, 3 => $prefix); + $fmt =~ s/\\\$([23])/$affix{$1}/g; + push @r, $fmt; } else { push @r, "$prefix$fmt$suffix"; } @@ -142,16 +144,10 @@ for (my $i = $#lines; $i >= 0; $i--) { # non-rf line - # whether or not to include a final newline depends on the - # format of the inline reference on the line below - if (!$inlref) { - push @r, "\n"; - } - + push @r, "\n"; push @r, $_; $refblock = 0; - $inlref = 0; } # Third pass (print the result) diff --git a/rf.1 b/rf.1 index 924f9c7..ca37ad4 100644 --- a/rf.1 +++ b/rf.1 @@ -233,24 +233,24 @@ of the authors, you can use the special syntax, with an uppercase instead of lowercase .Sq a . . -.Ss Special backslash behavior -.Bl -enum -.It -A backslash at the end of a format line acts as a line continuation marker. -The newline after the backslash is preserved. -.It -For format macros describing citations, a backslash immediately following the macro name will cause the reference be joined to the preceding line. -.El +.Ss Multiple lines +If no argument is given to a format macro, then +.Nm +will read the format specification from the following lines until a line containing +.Sq .. +is encountered, signifying the end of the multi-line format specification. .Pp -Anywhere else in the line, backslashes are preserved literally. +Within a multi-line format spefication, use the escapes \\$2 and \\$3 +to interpolate (respectively) any suffix and prefix given to the citation macro. .Pp -By using backslashes, you can implement footnote-based citations. -For example, using the ms macro package: +For example, the following multi-line format specification implements footnote references: .Bd -literal -offset indent -\&.Fx\\ \\**\\ -\&.FS\\ -%a, {\\fI%t\\fR}, {\\*Q%q\\*U}, %y.\\ +\&.Fx +\\$3\\**\\$2 +\&.FS +%a, {\\fI%t\\fR}, {\\*Q%q\\*U}, %y. \&.FE +\&.. .Ed . .Ss Automatic cleanup @@ -311,7 +311,6 @@ will output the following source code: \&.SH Previous work - \&.LP Davidson (1991) -- cgit v1.2.3