Seen on Textpattern Forum:
I’d like to be able to create deep links for keyword content. In my example, during the course of writing tutorials sometimes I need to use technical language. For some of these terms it is possible to just do a tooltip – but for others I’d like to be able to either link every instance of a piece of text to a link, or else just the first instance.
Solution
Suppose that we want to link the term dolor
and create a tooltip for dolore
in the following text:
<p><abbr title="Wasn't it dolorem ipsum?">Lorem ipsum dolor sit amet</abbr>, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
Straightforward replacements would break the document tree, so we need to target precisely the concerned elements of the document. This will create deep links only in paragraphs, but not inside other tags:
<txp:variable name="replacements">{
"dolore" : "<abbr title=\"see dolor\">dolore</abbr>",
"dolor" : "<a href=\"http://en.wiktionary.org/wiki/dolor#Latin\">dolor</a>"
}</txp:variable>
<txp:etc_query data='<txp:body />'
replace="//p/text()&={$json_decode({?replacements}|1).strtr({?}|$)}" />
Result
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
File(s)
- File: etc_query.txt [60.72 kB] (4258 downloads, ~29 per month)