|
Cookbook /
Replace on saveQuestionHow do I use AnswerThe ROS (Replace On Save) array defines a pattern as key and a text which should replace it when saving an edited text. The In general The "text to search for" is searched and replaced using the PHP preg_replace function, thus the pattern has to use the preg_replace syntax. ExamplesYou can use a replace on save pattern to assist you when converting HTML to wiki. The following patterns work # replace both <i> and </i>, upper and lower case $ROSPatterns ["/<\\/?i>/i"] = "''"; # replace both <b> and </b>, upper and lower case $ROSPatterns ["/<\\/?b>/i"] = "'''"; # replace <em>, upper and lower case, markup must be enabled in config.php $ROSPatterns ["/<em>/i"] = "'~"; # replace </em>, upper and lower case, markup must be enabled in config.php $ROSPatterns ["/<\\/em>/i"] = "~'"; # replace <strong>, upper and lower case, markup must be enabled in config.php $ROSPatterns ["/<strong>/i"] = "'*"; # replace </strong>, upper and lower case, markup must be enabled in config.php $ROSPatterns ["/<\\/strong>/i"] = "*'"; # replace <sup>, upper and lower case $ROSPatterns ["/<sup>/i"] = "'^"; # replace </sup>, upper and lower case $ROSPatterns ["/<\\/sup>/i"] = "^'"; # replace <sub>, upper and lower case $ROSPatterns ["/<sub>/i"] = "'_"; # replace </sub>, upper and lower case $ROSPatterns ["/<\\/sub>/i"] = "_'"; # replace both <br> and <br />, upper and lower case $ROSPatterns ["/<br\\s*\\/?>/i"] = "[[<]]"; # replace both <p> and </p>, upper and lower case $ROSPatterns ["/<\\/?p>/i"] = "\n\n"; # replace html links with the [[link|text]] markup $ROSPatterns ['/<\\s*?a.+?href\\s*?=\\s*?["\'](.*?)["\'].*?>(.*?)<\/a>/sim'] = '[[$1|$2]]'; # extract the image link from the <img ...> tag $ROSPatterns ['/<\\s*?img.+?src\\s*?=\\s*?["\'](.*?)["\'].*?>/sim'] = '$1'; # replace <h1> and </h1>, upper and lower case $ROSPatterns["/<h1\\s*>/i"] = "\n!"; $ROSPatterns["/<\\/h1\\s*>/i"] = ""; Replace Curly QuotesUse this to replace curly quotation marks:
$ROSPatterns ['/\x{201C}/u'] = '"';
$ROSPatterns ['/\x{201D}/u'] = '"';
$ROSPatterns ['/\x{2018}/u'] = "'";
$ROSPatterns ['/\x{2019}/u'] = "'";
NotesMore patterns will be added to cater for other simple HTML constructs.
ReleasesAdd these patterns to the bottom of your local/config.php CommentsThe following test cases were used <i>italic</i> <I>ITALIC</I> bre<br>ak br<br />eak BRE<BR>AK BR<BR />EAK BRE<BR />EAK <b>bolD</B> <B>Bold</b> <EM>em</em> <sup>sup</sup>erscript <sub>sub</sub>script <strong>strong</STRONG> This is a simple <p>paragraph</p> sort of. See AlsoCookbook.QuickReplace Quickly define replacement texts in wiki pages, and use them as markup or during page save.
Cookbook.ConvertHTML Convert an HTML page to PmWiki markup
ContributorsSimon
Anno
Incorrect Links
$ROSPatterns["/\\[{2}([^\/|\\[\\]]*?)(\/){0,1}([^\/|\\[\\]]*?)\\.
([^|\\]\\[]*?)\\]{2}/i"] =
"[[$1$2$3 $4|$3. $4]]";
The explanation of the pattern:
Match the character "[" literally «\[{2}»
Exactly 2 times «{2}»
Match the regular expression below and capture its match into
backreference number 1 «([^/|\[\]]*?)»
Match a single character NOT present in the list below «[^/|\[\]]*?»
Between zero and unlimited times, as few times as possible,
expanding as needed (lazy) «*?»
One of the characters "/|" «/|»
A [ character «\[»
A ] character «\]»
Match the regular expression below and capture its match into
backreference number 2 «(/){0,1}»
Between zero and one times, as many times as possible, giving back
as needed (greedy) «{0,1}»
Match the character "/" literally «/»
Match the regular expression below and capture its match into
backreference number 3 «([^/|\[\]]*?)»
Match a single character NOT present in the list below «[^/|\[\]]*?»
Between zero and unlimited times, as few times as possible,
expanding as needed (lazy) «*?»
One of the characters "/|" «/|»
A [ character «\[»
A ] character «\]»
Match the character "." literally «\.»
Match the character " " literally « »
Match the regular expression below and capture its match into
backreference number 4 «([^|\]\[]*?)»
Match a single character NOT present in the list below «[^|\]\[]*?»
Between zero and unlimited times, as few times as possible,
expanding as needed (lazy) «*?»
The character "|" «|»
A ] character «\]»
A [ character «\[»
Match the character "]" literally «\]{2}»
Exactly 2 times «{2}»
email from Anno
Special CharactersBeware of default encoding mismatching when trying to handle special characters. For example: $ROSPatterns ["/\x{2026}/u"] = "…"; Should change a typed ellipsis into an html entity … -- but when I was EDITING the wiki, my browser's default encoding was WESTERN ISO-Latin-1 (no idea why!). So not only didn't the typed ellipsis character (option-: on the Mac) match the unicode, but this code would throw an error and wipe all contents from the page. Changing my browser's default encoding to UTF-8 solved the issue, but this is a USER-END problem, so using ROSPatterns on special characters can hurt, since character codes are a user-end issue. :/ And that means if I type a ™ in the pattern match /™/ (presumably to change it to ™) that it will only match if someone types in that character in the same encoding that I used in the PHP file. XES August 16, 2010, at 09:21 AM User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |