source: spip-zone/_plugins_/typo_caps/typo_caps_fonctions.php @ 117558

Last change on this file since 117558 was 43238, checked in by fil@…, 10 years ago

les plugins typo_caps et typo_mois sont compatibles (ayant le meme prefixe ils etaient mutuellement exclusifs)

File size: 1.5 KB
Line 
1<?php
2
3/* code tire de typogrify
4 * http://jeffcroft.com/sidenotes/2007/may/29/typogrify-easily-produce-web-typography-doesnt-suc/
5 */
6
7
8/**
9 * This is necessary to keep dotted cap strings to pick up extra spaces
10 * used in preg_replace_callback later on
11 */
12function _cap_wrapper( $matchobj )
13{
14    if ( !empty($matchobj[2]) )
15    {
16        return sprintf('<span class="caps">%s</span>', $matchobj[2]);
17    }
18    else 
19    {
20        $mthree = $matchobj[3];
21        if ( ($mthree{strlen($mthree)-1}) == " " )
22        {
23            $caps = substr($mthree, 0, -1);
24            $tail = ' ';
25        }
26        else
27        {
28            $caps = $mthree;
29            $tail = '';
30        }           
31        return sprintf('<span class="caps">%s</span>%s', $caps, $tail);
32    }
33}
34
35function typo_caps_caps($t){
36        // eviter les tags
37        $t = preg_split(',(<[^>]+>),UmsS', $t, null, PREG_SPLIT_DELIM_CAPTURE);
38        $cap_finder = "/(
39            (\b[A-Z\d]*        # Group 2: Any amount of caps and digits
40            [A-Z]\d*[A-Z]      # A cap string much at least include two caps (but they can have digits between them)
41            [A-Z\d]*\b)        # Any amount of caps and digits
42            | (\b[A-Z]+\.\s?   # OR: Group 3: Some caps, followed by a '.' and an optional space
43            (?:[A-Z]+\.\s?)+)  # Followed by the same thing at least once more
44            (?:\s|\b|$))/xS";
45        for ($i = 0; $i < count($t); $i+=2)
46                $t[$i] = preg_replace_callback($cap_finder, _cap_wrapper, $t[$i]);
47        return join('', $t);
48}
49
50?>
Note: See TracBrowser for help on using the repository browser.