wikistyles.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2006 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. */
  8. SDV($WikiStylePattern,'%%|%[A-Za-z][-,=:#\\w\\s\'"().]*%');
  9. ## %% markup
  10. Markup('%%','style','%','return ApplyStyles($x);');
  11. ## %define=...% markup on a line by itself
  12. Markup('%define=', '>split',
  13. "/^(?=%define=)((?:$WikiStylePattern)\\s*)+$/e",
  14. "PZZ(ApplyStyles(PSS('$0')))");
  15. ## restore links before applying styles
  16. Markup('restorelinks','<%%',"/$KeepToken(\\d+L)$KeepToken/e",
  17. '$GLOBALS[\'KPV\'][\'$1\']');
  18. # define PmWiki's standard/default wikistyles
  19. if (IsEnabled($EnableStdWikiStyles,1)) {
  20. ## standard colors
  21. foreach(array('black','white','red','yellow','blue','gray',
  22. 'silver','maroon','green','navy','purple') as $c)
  23. SDV($WikiStyle[$c]['color'],$c);
  24. ## %newwin% style opens links in a new window
  25. SDV($WikiStyle['newwin']['target'],'_blank');
  26. ## %comment% style turns markup into a comment via display:none css
  27. SDV($WikiStyle['comment']['display'],'none');
  28. ## display, margin, padding, and border css properties
  29. $WikiStyleCSS[] =
  30. 'float|display|(margin|padding|border)(-(left|right|top|bottom))?';
  31. $WikiStyleCSS[] = 'white-space';
  32. ## list-styles
  33. $WikiStyleCSS[] = 'list-style';
  34. $WikiStyleCSS[] = 'width|height';
  35. foreach(array('decimal'=>'decimal', 'roman'=>'lower-roman',
  36. 'ROMAN'=>'upper-roman', 'alpha'=>'lower-alpha', 'ALPHA'=>'upper-alpha')
  37. as $k=>$v)
  38. SDV($WikiStyle[$k],array('apply'=>'list','list-style'=>$v));
  39. ## apply ranges
  40. SDVA($WikiStyleApply,array(
  41. 'item' => 'li|dt',
  42. 'list' => 'ul|ol|dl',
  43. 'div' => 'div',
  44. 'pre' => 'pre',
  45. 'img' => 'img',
  46. 'block' => 'p(?!\\sclass=)|div|ul|ol|dl|li|dt|pre|h[1-6]',
  47. 'p' => 'p(?!\\sclass=)'));
  48. foreach(array('item', 'list', 'block', 'p', 'div') as $c)
  49. SDV($WikiStyle[$c],array('apply'=>$c));
  50. ## block justifications
  51. foreach(array('left','right','center') as $c)
  52. SDV($WikiStyle[$c],array('apply'=>'block','text-align'=>$c));
  53. ## frames, floating frames, and floats
  54. SDV($HTMLStylesFmt['wikistyles'], "
  55. .frame
  56. { border:1px solid #cccccc; padding:4px; background-color:#f9f9f9; }
  57. .lfloat { float:left; margin-right:0.5em; }
  58. .rfloat { float:right; margin-left:0.5em; }\n");
  59. SDV($WikiStyle['thumb'], array('width' => '100px'));
  60. SDV($WikiStyle['frame'], array('class' => 'frame'));
  61. SDV($WikiStyle['lframe'], array('class' => 'frame lfloat'));
  62. SDV($WikiStyle['rframe'], array('class' => 'frame rfloat'));
  63. SDV($WikiStyle['cframe'], array(
  64. 'class' => 'frame', 'margin-left' => 'auto', 'margin-right' => 'auto',
  65. 'width' => '200px', 'apply' => 'block', 'text-align' => 'center'));
  66. SDV($WikiStyle['sidehead'], array('apply' => 'block', 'class' => 'sidehead'));
  67. }
  68. SDVA($WikiStyleAttr,array(
  69. 'vspace' => 'img',
  70. 'hspace' => 'img',
  71. 'align' => 'img',
  72. 'value' => 'li',
  73. 'target' => 'a',
  74. 'accesskey' => 'a',
  75. 'rel' => 'a'));
  76. SDVA($WikiStyleRepl,array(
  77. '/^%(.*)%$/' => '$1',
  78. '/\\bbgcolor([:=])/' => 'background-color$1',
  79. '/\\b(\d+)pct\\b/' => '$1%',
  80. ));
  81. $WikiStyleCSS[] = 'color|background-color';
  82. $WikiStyleCSS[] = 'text-align|text-decoration';
  83. $WikiStyleCSS[] = 'font-size|font-family|font-weight|font-style';
  84. SDV($imgTag, '(?:img|object|embed)'); SDV($aTag, 'a'); SDV($spanTag, 'span');
  85. function ApplyStyles($x) {
  86. global $UrlExcludeChars, $WikiStylePattern, $WikiStyleRepl, $WikiStyle,
  87. $WikiStyleAttr, $WikiStyleCSS, $WikiStyleApply, $BlockPattern,
  88. $WikiStyleTag, $imgTag, $aTag, $spanTag, $WikiStyleAttrPrefix;
  89. $wt = @$WikiStyleTag; $ns = $WikiStyleAttrPrefix; $ws = '';
  90. $x = preg_replace("/\\bhttps?:[^$UrlExcludeChars]+/e", "Keep('$0')", $x);
  91. $parts = preg_split("/($WikiStylePattern)/",$x,-1,PREG_SPLIT_DELIM_CAPTURE);
  92. $parts[] = NULL;
  93. $out = '';
  94. $style = array();
  95. $wikicsspat = '/^('.implode('|',(array)$WikiStyleCSS).')$/';
  96. while ($parts) {
  97. $p = array_shift($parts);
  98. if (preg_match("/^$WikiStylePattern\$/",$p)) {
  99. $WikiStyle['curr']=$style; $style=array();
  100. foreach((array)$WikiStyleRepl as $pat=>$rep)
  101. $p=preg_replace($pat,$rep,$p);
  102. preg_match_all(
  103. '/\\b([a-zA-Z][-\\w]*)([:=]([-#,\\w.()%]+|([\'"]).*?\\4))?/',
  104. $p, $match, PREG_SET_ORDER);
  105. while ($match) {
  106. $m = array_shift($match);
  107. if (@$m[2]) $style[$m[1]]=preg_replace('/^([\'"])(.*)\\1$/','$2',$m[3]);
  108. else if (!isset($WikiStyle[$m[1]])) @$style['class'] .= ' ' . $m[1];
  109. else {
  110. $c = @$style['class'];
  111. $style=array_merge($style,(array)$WikiStyle[$m[1]]);
  112. if ($c) $style['class'] = $c . ' ' . $style['class'];
  113. }
  114. }
  115. if (@$style['define']) {
  116. $d = $style['define']; unset($style['define']);
  117. $WikiStyle[$d] = $style;
  118. }
  119. if (@$WikiStyleApply[$style['apply']]) {
  120. $apply[$style['apply']] =
  121. array_merge((array)@$apply[$style['apply']],$style);
  122. $style=array();
  123. }
  124. continue;
  125. }
  126. if (is_null($p))
  127. { $alist=@$apply; unset($alist['']); $p=$out; $out=''; }
  128. elseif ($p=='') continue;
  129. else { $alist=array(''=>$style); }
  130. foreach((array)$alist as $a=>$s) {
  131. $spanattr = ''; $stylev = array(); $id = '';
  132. foreach((array)$s as $k=>$v) {
  133. $v = trim($v);
  134. if ($wt) $ws = str_replace('$1', "$ns$k='$v'", $wt);
  135. if ($k == 'class' && $v) $spanattr = "{$ns}class='$v'";
  136. elseif ($k=='id') $id = preg_replace('/[^-A-Za-z0-9:_.]+/', '_', $v);
  137. elseif (($k=='width' || $k=='height') && !@$WikiStyleApply[$a]
  138. && preg_match("/\\s*<$imgTag\\b/", $p))
  139. $p = preg_replace("/<($imgTag)\\b(?![^>]*\\s$k=)/",
  140. "$ws<$1 $ns$k='$v'", $p);
  141. elseif (@$WikiStyleAttr[$k])
  142. $p = preg_replace(
  143. "/<({$WikiStyleAttr[$k]}(?![^>]*\\s(?:$ns)?$k=))([^>]*)>/s",
  144. "$ws<$1 $ns$k='$v' $2>", $p);
  145. elseif (preg_match($wikicsspat,$k)) $stylev[]="$k: $v;";
  146. }
  147. if ($stylev) $spanattr .= " {$ns}style='".implode(' ',$stylev)."'";
  148. if ($id) $spanattr .= " {$ns}id='$id'";
  149. if ($spanattr) {
  150. if ($wt) $ws = str_replace('$1', $spanattr, $wt);
  151. if (!@$WikiStyleApply[$a]) {
  152. $p = preg_replace("!^(.*?)($|</?($BlockPattern))!s",
  153. "$ws<$spanTag $spanattr>$1</$spanTag>$2", $p, 1);
  154. }
  155. elseif (!preg_match('/^(\\s*<[^>]+>)*$/s',$p) ||
  156. preg_match("/<$imgTag\\b/", $p)) {
  157. $p = preg_replace("/<({$WikiStyleApply[$a]})\\b/",
  158. "$ws<$1 $spanattr", $p);
  159. }
  160. }
  161. if (@$s['color']) {
  162. $colorattr = "{$ns}style='color: {$s['color']}'";
  163. if ($wt) $ws = str_replace('$1', $colorattr, $wt);
  164. $p = preg_replace("/<$aTag\\b/", "$ws<$aTag $colorattr", $p);
  165. }
  166. }
  167. $out .= $p;
  168. }
  169. return $out;
  170. }