Table of Contents
TextInsert Plugin
Installation
Search and install the plugin using the Extension Manager. If you cannot use the DokuWiki download Manager to install this plugin, install it manually. Refer to manual instructions for how to install plugins manually.
Examples/Usage
This plugin provides an administrative panel where you enter macro names and their substitution texts. These are saved in the meta directory (in meta/macros/macros.ser
). In farms, you can choose to have a database for each animal (see: configuration_and_settings). You can save individual words, and strings of text, including lengthy paragraphs. The substitutions accept html, quotation marks and other punctuation. The macro names can accept spaces, dots, underscores and hyphens.
So, Macro_1.txt
is a valid macro name and could, for instance, have the following substitution: /var/usr/lib/php/readme.txt
.
You could have a footer message, FOOTER-1
, which has the following text substitution:
All Roads Lead to "Here" except for the ones which don't
The textinsert plugin will allow you to create macros for extended texts. You can create paragraph length texts with HTML markup which has to be repeated on multiple pages. If you want the HTML to be rendered as such, then the macro name must have the _HTML suffix:
Macro_HTML
Note: The underscore is required.
Basic Syntax
#@MACRO_NAME@#
Macro Inclusions
Some of the functionality of this plugin at its simplest can be implemented with DokuWiki's own entities facility: entities. But for complex substitutions and for control over where and how substitutions are made, Textinsert offers a wide variety of solutions and techniques.
One advantage of TextInsert comes when dealing with extended blocks of text and with its ability to include other macros inside the primary macro definition. That is, it can accept:
MACRO_1 This macro can include #@MACRO_2@# inside it. MACRO_2 a second macro
The result of using #@MACRO_1@#
is:
This macro can include a second macro inside it.
It accepts only one level of macro inclusion, so that if MACRO_3 were included in MACRO_2, MACRO_3 would not be rendered. If an include macro requires treatment as HTML, then the primary macro must be named as an HTML macro.
Entity Inclusions
TextInsert will also accept entities in its macro definitions. These are defined in dokuwiki/conf/entities.conf and dokuwiki/conf/entities.local.conf. Entities will be replaced in included macros as well as in the primary macro. For instance, if MACRO_2 above were defined as follows, using DokuWiki's long dash entity:
MACRO_2 a second---macro---
The final result would be: This macro can include a second—macro—inside it.
Standard Namespace Macros
Textinsert will accept namespace replacement patterns, as used in DokuWiki namespace templates. See Namespace Templates for the list of these. Textinsert can accept all but the MAIL and NAME macros, which go out of scope when the page is saved. Like regular textinsert macros these can also be embedded in other textinsert macros; see macro_inclusions. For instance, a macro named current_user and a user named jack:
Macro | Definition |
---|---|
current_user | The Current user is #@USER@# |
This would give you: The Current user is jack
.
There is a configuration option which governs whether or not standard namespace macros are accepted. It is set to true by default.
_ID_ and _NS_: Pseudo Namespace Macros for Sidebars
When used in a sidebar the standard namespace macro ID
resolves to the id of the sidebar page. To get the id of the currently displayed page, use instead _ID_
, of the currently displayed namespace _NS_
. These macros can also be used for macro_inclusions. So, for instance if you wanted to display the result as a wiki link, you could do this:
<a href="/mywiki/doku.php?id=#@_ID_@#" class="wikilink1">#@_ID_@#</a>
If you use _ID_
or _NS_
in your sidebar, then you must also insert the ~~NOCACHE~~
macro into the page so that the ID updates with each change of page. See control_macros.
Example of Multi Paragraph Text Block
If you want to create multi-pragraph blocks, then you need to use the HTML paragraph tag, <P>. . .</P>. The textinsert plugin will accept paragraphs like the the following, which is the introductory header found on the textinsert administration page:
<H2>Info</H2> <p> This panel enables you to add and delete macros and their replacement texts, and to edit them after they have been saved. A macro name can contain letters, numbers, underscores, hyphens and periods. For example: <TT>Macro_one.txt</TT> The replacement texts can accept HTML and can be of any length. <p> You can add up to six macros at a time. The sixth is a text area which will allow for extended texts. Enter the macro name in the <b>Macro</b> column and the texts which they represent in the <b>Substitution</b> column. </p> <p> Deletions are unlimited; check off the box(es) next the macro(s) to be deleted and click the Delete button at the bottom of the screen. Editing is done through the Edit screen, where you are presented with all your macros. You can edit the texts of any number of macros. </p>
Screen Shot of Edit Panel
Syntax
#@macro_name@#
#@Macro_HTML@#
In the second format, HTML tags, like <P>text. . .</P>
will be rendered as HTML; otherwise they will be rendered as HTML entities, as in <P>
.
The Macro names are case sensitive, so that Macro-A
and macro-A
are two different names.
Macro names are silently deaccented.
Expanded Functionality
The current version of textinsert handles foreign language translation and supports macros which accept parameters. If you don't need these features or if this version causes any issues you can download the earlier version: simplified version.
Parameter Interpolation
Basic Syntax
Macro Definition as defined on page:
#@macro_name~param1,param2,param3. . .param<n>~@#
Replacement String defined in Macro database:
string %1 more string %2 . . .%<n>
%1 will be replaced by param1,%2 by param2, etc.
Example:
Macro | Macro Replacement String |
---|---|
Mountain_Ranges | In Europe there are the %1, in North America the %2 and in South America the %3 |
#@Mountain_Ranges~Alps,Rockies,Andes~@#
Result: In Europe there are the Alps, in North America the Rockies and in South America the Andes
DokuWiki Basic Text Formatting Syntax
Parameters may use the basic text formatting syntax: bold,italic, etc. The one exception is the internal newline. If you need this you must use \n
.
Multi-line Syntax
#@Macro_name~ parameter one, //comment one parameter two, // comment two parameter three, parameter four . . parameter <n> ~@#
Comments are allowed but not included in the final output.
Example:
#@Mountain_Ranges~ Alps, //Switzerland Rockies, // U.S. and Canada Andes ~@#
The final result will be the same as for the single line syntax, as shown above in the example for the basic_syntax.
“Multi-line” also means that parameters and parameter format can continue across line boundaries:
#@Mountain_Ranges~ Alps, //Switzerland Rockies, // U.S. and Canada Andes or Andean Mountains (Spanish: Cordillera de los Andes)\, which are the longest continental mountain range in the world\, forming a continuous highland along the western edge of South America. The Andes also have the 2nd most elevated highest peak of any mountain range\, only behind the Himalayas. ~@#
In order for this to work, the commas must be escaped, since they represent parameter boundaries: \,
.
Language Translation
textinsert
makes translation substitutions based on ISO-derived namespaces.1) Macros found in an ISO-derived namespace will be read within the language context specified by the ISO designation. A namespace is defined by the topmost directory:
- de:ns1:ns2:page.text
The namespace here isde
, i.e. German - ns1:de:ns:page.txt
The namespace here isns1
, i.e. not an ISO designation
Macros of this type must use the following syntax:
#@LANG_name@#
A default entry for this type of macro must be entered into the textinsert database as LANG_name
. The translation strings must be stored in textinsert/lang/<ISO>/lang.php in the $lang array, using 'name' for the key. If you have a macro named LANG_greeting
, then in lang.php the entry will be:
$lang['greeting'] = 'language_string_for_greeting';
If a translation is not found, the default string will be used.
Use of lang.php
is deprecated. See improved language localization below for an updated method of storing translations.
Example
This example is based on the namespace de
, which references German pages.
You create a page named de:my_file into which you enter the following macro: #@LANG_greeting#@
. In the database you enter a default entry under the name LANG_greeting
. In textinsert/lang/de/lang.php, you create an entry for $lang['greeting'].
In each lang.php, you need only a single entry, for instance:
$lang['greeting'] = 'Hello from Germany';
When you include #@LANG_greeting@# in your document, the country identified by the IS0 namespace will be printed with its HTML formatting.
Improved Language Localization
Using lang/<iso>/lang.php
runs the risk that your macro replacement strings will be lost when the plugin is updated, since lang.php
will be overwritten. Beginning with the Oct 10, 2019 update, you can write your macro replacement strings to a separate file named macros.php
, also in the <iso>/lang
directory: <iso>/lang/macros.php
. For backward compatibility, this update still recognizes the use of lang.php
to hold your replacement strings.
The format of macros.php must follow this example:
<?php $lang_<iso>['key'] = 'replacement string'; $lang_<iso>['key'] = 'replacement string'; . .
To use the earlier example:
<?php /* textinsert/lang/de/macros.txt */ $lang_de['greeting'] = 'Hello from Germany';
Embedding
Translation macros can be included in standard and HTML macros. They also accept DokuWiki-defined entities, as described above. There is no special HTML translation macro. Any HTML features which appear in a language-based macro will automatically be output as HTML when the macro occurs in a language context.
# | Macro Name | Definition |
---|---|---|
1 | LANG_nation | <h2>I live in Canada</h2> |
2 | nation_HTML | #@LANG_nation@# |
3 | LANG_city | Toronto |
If the default replacement string contains HTML, then you should use form 2 in the above table when not in a language context; otherwise the HTML will appear in your output as plain text and the HTML will be lost.
In a language context, any HTML that you place in your definition will be output as HTML. Assume this is the definition for LANG_nation in the lang/de/lang.php:
$lang['nation'] = 'Ich lebe in <b>Kanada</b>';
In the German namespace, #@LANG_nation@# will be output as HTML: Ich lebe in Kanada.
It is also possible to include macros in your translations:
$lang['city'] = 'Berlin '; $lang['nation'] = 'Ich lebe in <b>#@LANG_city@#</b>, in Deutschland';
This results in: Ich lebe in Berlin, in Deutschland
Configuration and Settings
There are two configuration settings.
stdreplace | Accept standard macro replacements as used in namespace templates | default: on |
---|---|---|
farm | If this is a farm and you and would like each animal to have its own macro database, please select this option; otherwise all animals will share the same, i.e. the farmer's, database | default: off |
Development
Change Log
- Adds _NS_ for pseudo namespace macro, cf _ID_ (2021-08-13 22:46)
- Merge pull request #27 from dokuwiki-translate/lang_update_262_161417… (2021-02-24 17:51)
- translation update (2021-02-24 14:00)
- updates info (2019-10-03 23:39)
- Merge pull request #22 from turnermm/multi-lang (2019-10-03 23:21)
- added localization feature: lang/<iso>/macros.php hold translations. … (2019-10-03 03:31)
- updates info.txt (2019-09-26 17:49)
- Merge pull request #21 from turnermm/multi-line (2019-09-26 16:23)
- Enable marking up of DokuWiki basic syntax (2019-09-26 03:00)
- trim macro subsitutions before insertion (2019-09-24 23:48)
Discussion
All earlier questions have been moved to discussion. Additional questions should use github issues. Thanks.