resolve_alternatives(text,
select,
total,
althead=' ~@ ' ,
altfilter=None,
outfilter=None,
condf=None,
srcname=None)
|
|
Replace alternatives directives in the text with the selected
alternative.
Alternatives directives are of the form ~@/.../.../... ,
for example:
I see a ~@/pink/white/ elephant.
where ~@ is the directive head, followed by a character
that defines the delimiter of alternatives (like in sed
command). The number of alternatives per directive is not defined by the
directive itself, but is provided as an external parameter.
Alternative directive is resolved into one of the alternative
substrings by given index of the alternative (one-based). Before
substituting the directive, the selected alternative can be filtered
through function given by altfilter parameter. Text outside
of directives can be filtered as well, piece by piece, through the
function given by outfilter parameter.
If an alternatives directive is malformed (e.g. to little
alternatives), it may be reported to standard output. Unless all
encountered directives were well-formed, the original text is returned
instead of the partially resolved one.
- Parameters:
text (string) - the text to transform
select (int > 0) - index of the alternative to select (one-based)
total (int > 0) - number of alternatives per directive
althead (string) - directive head to use instead of the default one
altfilter ((string) -> string) - filter to apply to chosen alternatives
outfilter ((string) -> string) - filter to apply to text outside of directives
condf (None or (x_1, ..., x_n) -> True/False ) - resolve current alternative directive only when this function
returns True on call with each alternative as
argument
srcname (None or string) - if not None, report malformed directives to standard output, with
this string as source identifier
- Returns: string, int, bool
- resulting text, number of resolved alternatives, and an indicator
of well-formedness (
True if all directives
well-formed)
|