{dictionary:dictionaries:text:format:conds}

Description

Scans a block of text and replaces every whole word that matches a keyword in a dictionary slice with a format string of your choice - typically a link. The dictionary is one or more slices; each item lists its trigger words in its keywords........ field, and the format template uses the token _#KEYWORD_ for the matched word. Only whole words are replaced (not substrings), a keyword also matches its first-letter-uppercase form, and text inside script, h1-h6 and a tags, and inside HTML tags themselves, is skipped so existing links and headings stay intact. With an empty dictionary, or when nothing matches, the text is returned unchanged. Best used on a single item fulltext rather than long listings, since matching large dictionaries is costly.

Parameters

dictionaries required

One dictionary slice id, or several joined by dashes. Each item in the slice supplies its keywords........ field as the words to look for. Required - an empty value loads no dictionary and the text is returned unchanged.

text required

The text to scan. Every whole word that matches a dictionary keyword is replaced by the format string. Text inside script, h1-h6 and a tags, and inside HTML tags themselves, is left untouched. Required.

format optional

Template that replaces each matched word. The token _#KEYWORD_ stands for the matched word. If format is instead the id of a field in the dictionary slice, it is auto-wrapped as a substr of that field. Empty by default, but needed for useful output.

conds optional

Optional slice condition set (the same syntax a view uses) that restricts which dictionary items are loaded. If it contains a sort key it is also used to order them.

Examples

test{dictionary::APC publishes news daily}
ExpectedAPC publishes news daily
ActualAPC publishes news daily
With an empty first parameter no dictionary is loaded, so the text is returned unchanged (a warning is logged, not printed).
test{dictionary:9e1d2b9f88e3d6c3bf0eb967378610d6:Visit the website today}
ExpectedVisit the website today
ActualVisit the website today
When the dictionary slice holds no keyword that matches a whole word in the text, the text passes through unchanged. (Test Data - Stable has no keywords field, so nothing matches.)
virtual{dictionary:b8f3:APC publishes news:<a href="https#://apc.org">_#KEYWORD_</a>}
Expected(<a href="https://apc.org">APC</a> publishes news)
ActualAPC publishes news
A dictionary item whose keywords field is APC turns every whole-word APC in the text into the format string. _#KEYWORD_ is replaced by the matched word. (Needs a real dictionary slice id in place of b8f3.)
virtual{dictionary:b8f3:apc and APC and apcnews:[_#KEYWORD_]}
Expected([apc] and [APC] and apcnews)
Actualapc and APC and apcnews
Only whole words are replaced - apcnews is left alone because it is not a standalone word. A keyword apc also matches its first-letter-uppercase form APC, and _#KEYWORD_ keeps the form actually found.
virtual{dictionary:b8f3:<h2>APC</h2> <a href="x">APC</a> APC:[_#KEYWORD_]}
Expected(only the bare APC at the end becomes [APC]; the APC in the heading and link is left untouched)
Actual

APC

APC APC
Words inside script, h1-h6, a tags and inside any HTML tag itself are never replaced - only the bare APC at the end is matched. This protects existing links and headings.
virtual{dictionary:b8f3:APC and UNEP:[_#KEYWORD_]:highlight........-1-1}
Expected([APC] and UNEP)
ActualAPC and UNEP
The fourth parameter is a slice condition set (same syntax as a view). Here only highlighted dictionary items are loaded, so UNEP - not highlighted - is not replaced. A conds string containing sort is also used as the sort order.
virtual{dictionary:b8f3:APC publishes news:abstract........}
Expected(The Association ... publishes news)
ActualAPC publishes news
If format is the id of a field in the dictionary slice, it is auto-wrapped as {substr#:{index#:abstract........}#:0#:50} - the matched word is replaced by the first 50 characters of that field from the dictionary item.