[[PageOutline(2-5,Contents,pullout)]] = Include and do Regex-replacements = == Description == This is a hack upon the Include macro. Features: * regular expression search * regular expression replace * Can include URL's containing logged in users name * Can choose not to include for anonymous users Regular expressions are very usable when you whish to extract data from and/or change data inside an included page.[[BR]] Username feature is very usefull for personalized content. '''Note:''' You dont HAVE to use the regular expressions fields. You can still use the other functions as is. Arguments: * arg 1 : url - '''!http://page.com''' or '''!wiki:WikiPage'''. Local files cannot be included[[FootNote(Well, they can if the file resides in /tmp/trac_include/ and no slashes are used in filename.)]]. Also accepts !wiki:MyPage * arg 2 : format. Either "wiki" or "raw", where "wiki" does WikiFormatting * arg 3+: "Control arguments". See [wiki:RegexIncludeMacro#ControlArguments Control Arguments] below. * arg last:The regular expressions MUST be the last argument Only arg 1 & 2 are '''required'''. Regular expressions are defined as such:[[BR]] Search:[[BR]] 'expression'[[BR]] ''(Remember to define at least one group by using paranthesis)'' Replacement:[[BR]] '<expression>'/'<replacement>'[[BR]] You can use several expressions:[[BR]] '<expression>'/'<replacement>','<expression>'/'<replacement>' And search and capture combined[[BR]] '','<expression>'/'<replacement>' You can also use capture groups. In python these are defined as \1,\2 ect. (where in eg. perl its $1,$2) The result/output of each expression is passed on to the next expression, from left to right. === Control Arguments === Control Arguments are defined AFTER the second argument (raw,wiki) and BEFORE the regular expressions. Multiple arguments are seperated by comma.[[BR]] Example: {{{ RegexInclude(http://google.com,wiki,no_anon,use_vars=lower,'Expression'/'Replacement') }}} use_vars:: ''If defined, replace $USER with username in '''URL'''''. (NOT in the included text!). [[BR]] use_vars accepts a sub-argument, used change the case of the username. This is one of:[[BR]] * upper * lower * ucfirst example: use_vars=upper (note: ucfirst is "uppercase first letter only") no_anon:: ''Do not include this page for anonymous users.''[[BR]] '''NOTE:''' this is NOT a security feature, as anyone can still read your source code.[[BR]] Good feature to not include unneseseary information for anon users. match_seperator:: ''Char or string that seperate returned matches from a '''search''' expression''.[[BR]] Use like this: * match_seperator=Seperate This * match_seperator="Seperate This" Note: To use comma as seperator, it must be quoted. (match_seperator=",") no_dotall:: ''Disable the use of DOTALL option for regular expression''.[[BR]] pr. default DOTALL is enabled and this make the dot (".") also include linebreaks [[FootNote]] == Bugs/Feature Requests == Existing bugs and feature requests for !RegexIncludeMacro are [report:9?COMPONENT=RegexIncludeMacro here]. If you have any issues, create a [http://trac-hacks.org/newticket?component=RegexIncludeMacro&owner=dfaerch new ticket]. Feel free to use the comment field in the [wiki:RegexIncludeMacro#CommentsFeedback bottom] == Download == Download the zipped source from [download:regexincludemacro here]. == Source == You can check out RegexIncludeMacro from [http://trac-hacks.org/svn/regexincludemacro here] using Subversion, or [source:regexincludemacro browse the source] with Trac. == Examples == A simple example. Replace all occurences of the word "windows" with the word "Linux" {{{ [[RegexInclude(http://yourpage.tld,raw,'[Ww]indows'/'Linux')]] }}} [[BR]] The next example shows how to use the Control Argument ''use_vars'' to create a personalized start page on the wiki: {{{ [[RegexInclude(wiki:StartPage_$USER,wiki,use_vars=lower)]] }}} In my case, it would include a page from the wiki called "StartPage_dfaerch". (and always lowercasing the username). And for anonymous users, it would include "StartPage_anonymous", unless "no_anon" is defined as well. [[BR]] This example shows the 2 graphs from kernel.org's frontpage. It uses regular-expression '''search''' to capture the image-urls. Also, it uses '''match_seperator''' to seperate the to found image-urls by ![[BR]] to put them on seperate lines. {{{ [[RegexInclude(http://kernel.org,wiki,match_seperator=" [[BR]]",'(http://www\d+.kernel.org/bw-zeus\d+\.png)')]] }}} [[BR]] The last example includes http://www.kernel.org/kdist/finger_banner and wiki-formats the wanted data, and removes the unwanted. Perticulary, i want to only keep the major version information, not patches nor snapshots. Note how i use 2 regular expression replaces, seperated by ','. The first expression formats the lines i want to keep, the second deletes the rest. {{{ [[RegexInclude(http://www.kernel.org/kdist/finger_banner,wiki,'The latest ([\w.]+) version [^:]+:\s*([a-z0-9.-]+)'/' '''\1''' :: \n ''\2'' ','\nThe[ a-zA-Z0-9.:-]+'/'\n')]] }}} The result is: '''stable''':: ''2.6.18'' '''2.4''':: ''2.4.33.3'' '''2.2''':: ''2.2.26'' == Known limitations == * expressions or replacement-strings cannot contain ',' (that is: tick-comma-tick) since that combination is used for splitting the expressions: * expressions or replacement-strings cannot contain '/' (that is: tick-slash-tick) since that combination is used for splitting the expression from replacement-string.[http://www.costumesdelux.com/halloween-costumes Halloween Costumes] I hope these two combinations are so unlikely, that the limitation wont cause any trouble. * url's cannot contain comma. If you need comma, you can URLEncode it. (as such: /file?arg=1,2 becomes /file?arg=1%2c2) * match_seperator must be in quotes (") if you want to use commas. == Recent Changes == [[ChangeLog(regexincludemacro, 3)]] == Author/Contributors == '''Author:''' [wiki:dfaerch] [[BR]] '''Maintainer:''' [wiki:dfaerch] [[BR]] '''Contributors:'''