scriptlet for gathering paragraphs into sed work space

This file is copyright © 2004,2005,2006,2011 by Panu Kalliokoski and released under the stx2any license.

Gather paragraphs into pattern space. This is a sed scriptlet to be used as a prefix program for rules that want to handle syntactic constructs that might span multiple lines but not paragraph boundaries. Such syntactic constructs include emphasis and linking constructs.

If we are in the end of a file, we must just take the hold space and proceed, otherwise the hold space will be trashed:

${;x;G;s#^\n##;b nogather;}

If a new block element was met when gathering a paragraph's lines (see below), it was saved in hold space, and is processed now. We turn an empty line into a space in case it gets held; otherwise we could not tell it from no held line at all.

s#^$# #
x
/^  *$/{;s#^ $##;b nogather;}
/^--*$/ b nogather
/^!/ b nogather
/::$/ b nogather
/./{;G;s#\n $#\n#;x;s#.*##;}
x
s#^ $##

Preformatted blocks are simply passed through. (This code has a bug: if an empty preformatted block is right after a paragraph, the end tag is not noted.)

/^{{{$/,/^}}}$/ b
/^{{{\n/,/^}}}$/ b

These are always their own paragraphs:

/^ *$/ b nogather
/^--*$/ b nogather
/^!/ b nogather
/::$/ b nogather

Begin gathering lines in pattern space. First check if we got a line that should start a new paragraph. If we got one, save it for later use.

: gather
s#
$##
/\n{{{$/ b save
/\n--*$/ b save
/\n *\* [^\n]*$/ b save
/\n *- [^\n]*$/ b save
/\n *# [^\n]*$/ b save
/\n![^\n]*$/ b save
/\n[^\n]*::$/ b save

Gather lines in pattern space until we reach an empty line (or something else, as found above). End of file has to be made a special case.

$ b nogather
N
/\n$/ b nogather
b gather

We've got something to save in the hold space.

: save
h
s#^.*\n##
x
s#\n[^\n]*$##

Proceed with normal handling.

: nogather