How RakuAsT-RakuDoc-Render handles forward references or placement material that has not yet been rendered.
RakuDoc statements that make references to structures or data that have not been completely rendered embed the references in PCells, which will interpolate the rendering once known.
For instance, when =place toc:* is given in a RakuDoc source (eg at the top of a source), typically the source has not yet been completely rendered.
The renderer actually renders a source into PStr or PromiseStrings rather than Str. As forward references become known, they are included in the text.
Consequently, parameters to a template may contain a mixture of Raku Str and PCells, in an object of type called PStr.
PCells should not be visible to the template user.
If a PStr or PCell is stringified before the data has been rendered, its internal id and UNAVAILABLE will be the rendered result.
Since the embedded content of a PStr may ony be available after a template has rendered, care must be taken not be stringify any of the parameters prematurely.
Consequently, the return object from a template should be built from the parameter values using the concatenation operator ~.
A PStr is built up by concatenating using the infix operator ~. Assignment does not add to a PStr. Concatenation can be on the left or the right of the PStr and the result will depend on the type.
Since left concatenation has an effect on the PStr on the right, use sink to discard the return value, unless of course the return value is the last line of a block, in which case it is returned as the value of the block, eg.,
my PStr $p;
$p ~= PCell.new( :s($a-supplier.Supply), :id<AAA> );
sink '<bold>' ~ $p
The following methods are defined for PStr. Unless specified, they result in a PStr allowing for chaining.
Creates a new PStr either with no parameters, or a sequence of strings.
strip is first called, then trailing or leading space, respectively, are removed from final, initial strings in the PStr. trim calls both trim-trailing and trim-leading.
Determines whether there are any unexpanded PCell in a PStr.
Returns the number of segments (strings or PCells) in a PStr
These two methods of a PStr object return any of the leading or tailing (respectively) Str elements of the PStr. The elements are removed, and so should be concatenated back on after processing.
For simplicity above, examples were given of pre- and post-processing templates, and treating the contents of %prm as Str. Since some parameters may contain PStr, more care is needed. For example, the post-processing should be done as follows:
table => -> %prm, $tmpl {
my $rv = $tmpl.prev;
if $rv ~~ PStr {
# get leading text
my $lead = $rv.lead;
# process the string, if it exists
$lead.subst( / '<table' \s+ 'class="pod-table' /, '<table class="table is-centered');
# left concatenate onto the PStr
$lead ~ $rv
# concatenating to a PStr results in a PStr, which is the return object
}
else { $rv.subst( / '<table' \s+ 'class="pod-table' /, '<table class="table is-centered') }
}
PCells should be invisible to the end user. However, if references are made that are not expanded, then a PCell will become visible.
Typically, PCell instances are converted to Str once a future block has provided the payload needed for the cell.
Provides information about a PCell.