changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / markup / css.php
1 <?php
2
3 /*
4    This plugin provides for CSS support in WikiPages. To add a style
5    (color, background, font, etc.) simply use the "@@" to initiate
6    a CSS definition:
7
8         @@cssparagraph  ... here comes the text
9         that is formatted according to the style
10         class ".cssparagraph" of our stylesheet
11
12    In the above example the style is applied to the whole paragraph (every
13    piece of text, that follows the @@). But you can also assign styles to
14    just some parts of the text or even intermix and overlap multiple style
15    definitions. To do so, you must however close a begun style allocation:
16
17         @@parastyle  ... some text following
18         ... but @@color:red; this part@@ is
19         coloured!
20         And @@subdef1 ...here... @@more3 ... @@
21         a piece@@ of nested CSS-stuff.
22
23    In this example (looks a bit weird) the last two definitions are nested!
24    Note also, that you cannot only assign CSS class names to a paragraph or
25    piece of text, but also direct format it using all possible CSS
26    definitions - but beware that there cannot be any whitespace in the CSS
27    instruction that you apply using this syntax.
28
29    This plugin uses regular expressions, but does not slow down the
30    rendering process much more than any other plugin!
31
32    See also the 'markup_css_singleat' plugin, which allows to use just
33    a single @ instead of two, like with javadoc. Both can be used
34    alternative or in conjunction.
35 */
36
37
38 define("EWIKI_CSS_BLOCK", "div");
39 define("EWIKI_CSS_INLINE", "span");
40 define("EWIKI_CSS_CLASSPREP", "");      # "wiki-" for example
41 define("EWIKI_CSS_LOWER", 0);           # classnames to lowercase
42 define("EWIKI_CSS_FIX", 0);             # allow only correct classnames
43
44 $ewiki_plugins["format_source"][] = "ewiki_format_css";
45
46
47
48 function ewiki_format_css(&$src) {
49
50    #-- wikisource is splitted into paragraphs and later reconcatenated
51    #   (which will collapse multiple linebreaks, and thus may break some
52    #   things in <pre> and eventually others - the impact would be little!)
53    if (strpos($src, "@@") !== false) {
54
55       $src = explode("\n\n", $src);
56
57       foreach ($src as $i=>$para) {
58          if (strpos($para, "@@") !== false) {
59             ewiki_format_css_para($src[$i]);
60          }
61       }
62
63       $src = implode("\n\n", $src);
64    }
65
66 }
67
68
69
70 function ewiki_format_css_para(&$para) {
71
72    #-- opened html container elememts
73    $stack = array();
74
75    #-- how class names may look
76    $s_defs = EWIKI_CSS_FIX
77         ? '[-\dA-Za-z]*|[^\s]+:[^\s]+'  // disallow wrong class names
78         : '[^\s]*';                     // allows nearly anything
79
80    #-- find every @@ occourence
81    while (preg_match('/^(.*?)@@('.$s_defs.')(.*)$/s', $para, $uu)) {
82
83       #-- closing @@
84       if (!strlen($uu[2])) {
85
86          if ($stack) {
87             $repl = "</" . array_pop($stack) . ">";
88          }
89          else {
90             $repl = "@&#x40;";
91          }
92          $para = $uu[1] . $repl . $uu[3];
93
94       }
95
96       #-- opening @@...
97       else {
98
99          #-- html container element
100          $span = (trim( $uu[1]) ? EWIKI_CSS_INLINE : EWIKI_CSS_BLOCK);
101          $stack[] = $span;
102
103          #-- style= or class= definition?
104          $is_class = !strpos($uu[2], ":");
105
106          #-- class names into lowercase?
107          if ($is_class && EWIKI_CSS_LOWER) {
108             $uu[2] = strtolower($uu[2]);
109          }
110
111          #-- output
112          $para = $uu[1] . "<$span " . ($is_class ? "class" : "style")
113                . '="' . ($is_class ? EWIKI_CSS_CLASSPREP : "") . $uu[2] . '">'
114                . $uu[3];
115       }
116
117    }
118
119    #-- close still opened html container elements
120    while ($span = array_pop($stack)) {
121       $para .= "</$span>";
122    }
123
124    #<off># return($para);
125          # we're now pass-by-reference
126 }
127
128
129 ?>