Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

Replace on save

Summary: How to use $ROSPatterns (Replace On Save Patterns)?
Version:
Prerequisites:
Status:
Maintainer:
Categories: Markup

Question

How do I use $ROSPatterns? What can I use them for? Show me some examples that work please?

Answer

The ROS (Replace On Save) array defines a pattern as key and a text which should replace it when saving an edited text. The $ROSPatterns are enabled by placing them in the local/config.php file.

In general
$ROSPatterns ["text to search for"] = "text to replace with";

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.

Examples

You 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"] = "";

Notes

More patterns will be added to cater for other simple HTML constructs.

  • This recipe was last tested on PmWiki version: 2.11

Releases

Add these patterns to the bottom of your local/config.php

Comments

The 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 Also

Cookbook.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

Contributors

Simon
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
Edit - History - Print - Recent Changes - Search
Page last modified on September 18, 2009, at 06:05 AM