wikiwords.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2001-2017 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This script adds WikiWord (CamelCase) processing to PmWiki.
  8. Originally WikiWords were part of the default configuration,
  9. but their usage has died out over time and so it's now optional.
  10. To enable WikiWord links, simply add the following to
  11. a local customization file:
  12. $EnableWikiWords = 1;
  13. To have PmWiki recognize and process WikiWords but not link
  14. them (i.e., the default behavior in PmWiki 2.1), also add
  15. $LinkWikiWords = 0;
  16. If you want only the first occurrence of a WikiWord to be converted
  17. to a link, set $WikiWordCountMax=1.
  18. $WikiWordCountMax = 1; # converts only first WikiWord
  19. $WikiWordCountMax = 0; # another way to disable WikiWord links
  20. The $WikiWordCount array can be used to control the number of times
  21. a WikiWord is converted to a link. This is useful for disabling
  22. or limiting specific WikiWords.
  23. $WikiWordCount['PhD'] = 0; # disables 'PhD'
  24. $WikiWordCount['PmWiki'] = 1; # convert only first 'PmWiki'
  25. $WikiWordCount['WikiWord'] = -1; # ignore $SpaceWikiWord setting
  26. By default, PmWiki is configured such that only the first occurrence
  27. of 'PmWiki' in a page is treated as a WikiWord. If you want to
  28. restore 'PmWiki' to be treated like other WikiWords, uncomment the
  29. line below.
  30. unset($WikiWordCount['PmWiki']);
  31. If you want to disable WikiWords matching a pattern, you can use
  32. something like the following. Note that the first argument has to
  33. be different for each call to Markup(). The example below disables
  34. WikiWord links like COM1, COM2, COM1234, etc.
  35. Markup('COM\d+', '<wikilink', '/\\bCOM\\d+/', "Keep");
  36. Script maintained by Petko YOTOV www.pmwiki.org/petko
  37. */
  38. SDV($LinkWikiWords, 1);
  39. ## bare wikilinks
  40. Markup('wikilink', '>urllink',
  41. "/\\b(?<![#&])($GroupPattern([\\/.]))?($WikiWordPattern)/",
  42. "MarkupWikiLink");
  43. function MarkupWikiLink($m) {
  44. extract($GLOBALS["MarkupToHTML"]); # get $pagename
  45. return Keep('<span class="wikiword">'.WikiLink($pagename,$m[0]).'</span>', 'L');
  46. }
  47. function WikiLink($pagename, $word) {
  48. global $LinkWikiWords, $WikiWordCount, $SpaceWikiWords, $AsSpacedFunction,
  49. $MarkupFrame, $WikiWordCountMax;
  50. if (!$LinkWikiWords || ($WikiWordCount[$word] < 0)) return $word;
  51. $text = ($SpaceWikiWords) ? $AsSpacedFunction($word) : $word;
  52. $text = preg_replace('!.*/!', '', $text);
  53. if (!isset($MarkupFrame[0]['wwcount'][$word]))
  54. $MarkupFrame[0]['wwcount'][$word] = $WikiWordCountMax;
  55. if ($MarkupFrame[0]['wwcount'][$word]-- < 1) return $text;
  56. return MakeLink($pagename, $word, $text);
  57. }