View Single Post
Old 12-12-2011, 05:27 PM   #11
SBT
Fanatic
SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.
 
SBT's Avatar
 
Posts: 580
Karma: 810184
Join Date: Sep 2010
Location: Norway
Device: prs-t1, tablet, Nook Simple, assorted kindles, iPad
Handling footnotes

And for my next trick:
Take footnote references and footnotes, indicated by @'s and %f's respectively, and replace by properly referenced and back-referencing endnotes at the end of the chapter.
Code:
function zx_footnotes {
# Usage: zx_footnotes [text file]
# All @'s are replaced by links to corresponding footnote.
# All footnotes indicated by %f are converted to end-notes at the end of 
# the chapter.
# Footnotes which span more than one page must be collected on a single page.
# The end-notes have links back to the original reference.
# The @'s can have a number appended to them for control purpose, but
# they are not used y this function.
# If no input file is given; input is read from STDIN.
# Output is to STDOUT
[ $1 ] && inputfile=$1 || inputfile="/dev/stdin"
awk '
BEGIN {n=0;r=1;cn=1}
/@/ {sub(/@[0-9]*/, sprintf("<a name=\"R%2.2d_%3.3d\"/><a href=\"#F%2.2d_%3.3d\" class=\"footnote\">%d)</a>",cn,r,cn,r,r));r++}
/^%f/ {fn=1;n++;sub("%f","")}
/^%[eP]/ {fn=0}
/^%e/ { if (n>0) {
   print "<h3 class=\"footnoteheader\">Footnotes</h3>"
   print "<dl class=\"footnotelist\">"
   for (i=1; i<=n;i++) {
      printf("   <dt><a name=\"F%2.2d_%3.3d\"/><a href=\"#R%2.2d_%3.3d\">%d)</a></dt>" ,cn,i,cn,i,i)
      print "<dd>",fns[i],"</dd>"
   }
   print "</dl>"; 
   n=0;r=1;cn++;
   delete fns;
}
$0="<hr class=\"endchapter\" />";
}
{if (fn>0) {fns[n]=fns[n]$0} else print}
' $inputfile
}
No more than 999 footnotes pr. chapter or 99 chapters, please.

Why the prefix zx_ in zx_footnotes?
  • It's probably a good idea to have some kind of common, fairly unique prefix to all of the functions listed here. Makes tab-completion simpler, for one thing.
  • zx is easy to type
  • If i wanted to have everything lucid and self-explanatory, I wouldn't be using bash, sed, and awk in the first place, would I?
SBT is offline   Reply With Quote