PEAR2_Text_MarkdownPEAR2_Text_Markdown-0.1.0/php/PEAR2/Text/Markdown/Extra/EmStrong.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * 
 * Span plugin to insert emphasis and strong tags.
 * 
 * Differs from default Markdown in that underscores and stars inside a
 * word will not trigger the markup.
 * 
 * @category Solar
 * 
 * @package Markdown_Extra
 * 
 * @author John Gruber <http://daringfireball.net/projects/markdown/>
 * 
 * @author Michel Fortin <http://www.michelf.com/projects/php-markdown/>
 * 
 * @author Paul M. Jones <pmjones@solarphp.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: EmStrong.php 3732 2009-04-29 17:27:56Z pmjones $
 * 
 */
namespace PEAR2\Text;

class Markdown_Extra_EmStrong extends Markdown_Plugin_EmStrong
{
    /**
     * 
     * Converts emphasis and strong text.
     * 
     * @param string $text The source text.
     * 
     * @return string The transformed XHTML.
     * 
     */
    public function parse($text)
    {
        // <strong> must go first:
        $text = preg_replace_callback(
            array(
                '{                                                  # __strong__
                    ( (?<!\w) __ )                                  # $1: Marker (not preceded by alphanum)
                    (?=\S)                                          # Not followed by whitespace 
                    (?!__)                                          #   or two others marker chars.
                    (                                               # $2: Content
                        (?>                                         
                            [^_]+?                                  # Anthing not em markers.
                        |                                           
                                                                    # Balance any regular _ emphasis inside.
                            (?<![a-zA-Z0-9])_ (?=\S) (?! _) (.+?) 
                            (?<=\S) _ (?![a-zA-Z0-9])
                        )+?
                    )
                    (?<=\S) __                                      # End mark not preceded by whitespace.
                    (?!\w)                                          # Not followed by alphanum.
                }sx',                                               
                '{                                                  # **strong**
                    ( (?<!\*\*) \*\* )                              # $1: Marker (not preceded by two *)
                    (?=\S)                                          # Not followed by whitespace 
                    (?!\1)                                          #   or two others marker chars.
                    (                                               # $2: Content
                        (?>                                         
                            [^*]+?                                  # Anthing not em markers.
                        |                                           
                                                                    # Balance any regular * emphasis inside.
                            \* (?=\S) (?! \*) (.+?) (?<=\S) \*
                        )+?
                    )
                    (?<=\S) \*\*                                    # End mark not preceded by whitespace.
                }sx',
            ),
            array($this, '_parseStrong'),
            $text
        );
        
        // Then <em>:
        $text = preg_replace_callback(
            array(
                '{ ( (?<!\w) _ ) (?=\S) (?! _)  (.+?) (?<=\S) _ (?!\w) }sx',
                '{ ( (?<!\*)\* ) (?=\S) (?! \*) (.+?) (?<=\S) \* }sx',
            ),
            array($this, '_parseEm'),
            $text
        );
        
        return $text;
    }
}
EOF