Advertisement
spacechase0

GDN Syntax Parser

Mar 2nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.36 KB | None | 0 0
  1. <?php
  2.  
  3. global $smilies;
  4. $smilies = array( ":)" => "glad.png",
  5.                   ":(" => "sad.png",
  6.                   ":p" => "tongue.png",
  7.                   ":P" => "tongue.png",
  8.                   ":o" => "surprised.png",
  9.                   ":O" => "surprised.png",
  10.                   ":d" => "happy.png",
  11.                   ":D" => "happy.png",
  12.                   ":s" => "confused.png",
  13.                   ":S" => "confused.png",
  14.                   "B)" => "cool.png",
  15.                   "-_-'" => "dissapointed.png",
  16.                   ";)" => "wink.png",
  17.                   ">_<" => "upset.png",
  18.                   "&gt;_&lt;" => "upset.png",
  19.                   "^^" => "satisfied.png" );
  20.  
  21. global $elements_noparse;
  22. $elements_noparse = array( "noparse" => "___CONTENT___",
  23.                            "code" => "<pre>___CONTENT___</pre>",
  24.                            "inl" => "<tt>___CONTENT___</tt>" );
  25.  
  26. global $elements_nocontent;
  27. $elements_nocontent = array( "img" => '<img class="forum-image" src="___ARGUMENT___" alt="___ARGUMENT___" border="0" />' );
  28.  
  29. global $elements_noargs;
  30. $elements_noargs = array( "b" => '<strong>___CONTENT___</strong>',
  31.                           "u" => '<span style="text-decoration: underline">___CONTENT___</span>',
  32.                           "i" => '<em>___CONTENT___</em>',
  33.                           "s" => '<span style="text-decoration: line-through">___CONTENT___</span>',
  34.                           "center" => '<div style="text-align: center">___CONTENT___</div>',
  35.                          
  36.                           "url" => '<a href="___CONTENT___">___CONTENT___</a>',
  37.                           "img" => '<img class="forum-image" src="___CONTENT___" border="0" />',
  38.                           "quote" => '<blockquote>___CONTENT___</blockquote>',
  39.                          
  40.                           "*" => '<li>___CONTENT___</li>',
  41.                           "list" => '<ol>___CONTENT___</ol>',
  42.                           "ulist" => '<ul>___CONTENT___</ul>',
  43.                          
  44.                           "h1" => '<h2>___CONTENT___</h2>',
  45.                           "h2" => '<h3>___CONTENT___</h3>',
  46.                           "h3" => '<h4>___CONTENT___</h4>',
  47.                           "h4" => '<h5>___CONTENT___</h5>',
  48.                           "h5" => '<h6>___CONTENT___</h6>',
  49.                          
  50.                           "user" => '<a href="user.php?un=___CONTENT___">___CONTENT___</a>' );
  51.  
  52. global $elements_args;
  53. $elements_args = array( "url" => '<a href="___ARGUMENT___">___CONTENT___</a>',
  54.                        
  55.                         "quote" => '<blockquote><span class="quote-user">Posted by ___ARGUMENT___: </span><br/>___CONTENT___</blockquote>',
  56.                        
  57.                         "font" => '<span style="font-family: ___ARGUMENT___">___CONTENT___</span>',
  58.                         "size" => '<span style="font-size: ___ARGUMENT___">___CONTENT___</span>',
  59.                         "color" => '<span style="color: ___ARGUMENT___">___CONTENT___</span>' );
  60.  
  61. function FindAllOccurences( $haystack, $needle )
  62. {
  63.     $toReturn = array();
  64.    
  65.     $foundPos = 0;
  66.     while ( true )
  67.     {
  68.         $found = stripos( $haystack, $needle, $foundPos + 1 );
  69.        
  70.         if ( $found === false )
  71.         {
  72.             break;
  73.         }
  74.        
  75.         $toReturn[] = $foundPos = $found;
  76.     }
  77.    
  78.     return $toReturn;
  79. }
  80.  
  81. function DoNoparseStuff( $text )
  82. {
  83.     global $elements_noparse;
  84.     global $npLastFound;
  85.     global $npLastFoundCode;
  86.    
  87.     $tmpFound = false;
  88.     $tmpCode = false;
  89.     foreach ( $elements_noparse as $code => $format )
  90.     {
  91.         $foundIt = strpos( $text, '[' . $code . ']', $npLastFound );
  92.         if ( $foundIt !== false and ( $tmpFound === false or $tmpFound > $foundIt ) )
  93.         {
  94.             $tmpCode = $code;
  95.             $tmpFound = $foundIt;
  96.         }
  97.     }
  98.    
  99.     $npLastFound = $tmpFound;
  100.     $npLastFoundCode = $tmpCode;
  101. }
  102.  
  103. function nl2br_custom( $text )
  104. {
  105.     $text = str_replace( "\r\n", "\n", $text );
  106.     $text = str_replace( "\r", "\n", $text );
  107.    
  108.     $tokens = explode( "\n", $text );
  109.    
  110.     $str = '';
  111.     $nsf = 1;
  112.     $c = count( $tokens );
  113.     foreach ( $tokens as $key => $value )
  114.     {
  115.         $str .= $value;
  116.        
  117.         $trimmed = trim( $value );
  118.         $last3Chars = substr( $trimmed, strlen( $trimmed ) - 3, 3 );
  119.         $last4Chars = substr( $trimmed, strlen( $trimmed ) - 4, 4 );
  120.         $last5Chars = substr( $trimmed, strlen( $trimmed ) - 5, 5 );
  121.         $last6Chars = substr( $trimmed, strlen( $trimmed ) - 6, 6 );
  122.         if ( $nsf != $c and $last3Chars != "/*]" and $last4Chars != "/h1]" and $last4Chars != "/h2]" and $last4Chars != "/h3]" and $last4Chars != "/h4]" and $last4Chars != "/h5]" and $last5Chars != "list]" and $last6Chars != "ulist]" )
  123.         {
  124.             $str .= "<br/>\n";
  125.         }
  126.         else
  127.         {
  128.             $str .= "\n";
  129.         }
  130.         ++$nsf;
  131.     }
  132.    
  133.     return $str;
  134. }
  135.  
  136. function Parse( $text )
  137. {
  138.     global $elements_args;
  139.     global $elements_noargs;
  140.     global $elements_nocontent;
  141.     global $elements_noparse;
  142.     global $smilies;
  143.    
  144.     $foundSome = false;
  145.     foreach ( $elements_noparse as $code => $format )
  146.     {
  147.         if ( strpos( $text, '[' . $code . ']' ) !== false and strpos( $text, '[/' . $code . ']' ) !== false )
  148.         {
  149.             $foundSome = true;
  150.         }
  151.     }
  152.    
  153.     $sections = array();
  154.     if ( !$foundSome )
  155.     {
  156.         $sections[] = array( $text => true );
  157.     }
  158.     else
  159.     {
  160.         global $npLastFound;
  161.         global $npLastFoundCode;
  162.         $npLastFound = 0;
  163.         $lastSection = 0;
  164.         for ( DoNoparseStuff( $text ); $npLastFound !== false; DoNoparseStuff( $text ) )
  165.         {
  166.             $tag = $npLastFoundCode;
  167.             $tagStart = '[' . $tag . ']';
  168.             $tagEnd = '[/' . $tag . ']';
  169.            
  170.             $endFound = strpos( $text, $tagEnd, $npLastFound );
  171.             if ( $endFound === false  )
  172.             {
  173.                 $npLastFound += strlen( $tagStart );
  174.                 continue;
  175.             }
  176.            
  177.             $sections[] = array( substr( $text, $lastSection, $npLastFound - $lastSection ) => true );
  178.             $sections[] = array( substr( $text, $npLastFound, $endFound + strlen( $tagEnd ) - $npLastFound ) => $tag );
  179.            
  180.             $lastSection = $endFound + strlen( $tagEnd );
  181.             $npLastFound += strlen( $tagStart );
  182.         }
  183.         $sections[] = array( substr( $text, $lastSection, strlen( $text ) - $lastSection ) => true );
  184.     }
  185.    
  186.     $parsedText = "";
  187.     foreach ( $sections as $key => $sectionInfo )
  188.     {
  189.         $theKeyList = array_keys( $sectionInfo );
  190.         $toParseOriginal = $theKeyList[ 0 ];
  191.         $toParse = htmlentities( $toParseOriginal );
  192.         if ( $sectionInfo[ $toParseOriginal ] !== true )
  193.         {
  194.             $format = $elements_noparse[ $sectionInfo[ $toParseOriginal ] ];
  195.             $tag = $sectionInfo[ $toParseOriginal ];
  196.             if ( $tag !== 'code' and $tag != 'inl' )
  197.             {
  198.                 $toParse = nl2br_custom( $toParse );
  199.             }
  200.             $stuff = $toParse;
  201.             $stuff = str_replace( '[' . $tag . ']', '', $stuff );
  202.             $stuff = str_replace( '[/' . $tag . ']', '', $stuff );
  203.             $parsed = str_replace( '___CONTENT___', $stuff, $format );
  204.             $parsedText .= $parsed;
  205.             continue;
  206.         }
  207.         $toParse = trim( $toParse );
  208.         $toParse = nl2br_custom( $toParse );
  209.        
  210.         foreach ( $smilies as $smily => $image )
  211.         {
  212.             for ( $lastFound = strpos( $toParse, $smily ); $lastFound !== false; $lastFound = @strpos( $toParse, $smily, $lastFound + 1 ) )
  213.             {
  214.                 $contents = '<img class="smily" src="res/' . $image . '" alt="' . htmlentities( $smily ) . '" />';
  215.                
  216.                 $replaceEnd = $lastFound + strlen( $smily );
  217.                 $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
  218.                 $toParse = str_replace( $stuff, $contents, $toParse );
  219.                 $lastFound = $lastFound + strlen( $contents );
  220.             }
  221.         }
  222.        
  223.         foreach ( $elements_noargs as $code => $format )
  224.         {
  225.             $codeStart = '[' . $code . ']';
  226.             $codeEnd = '[/' . $code . ']';
  227.             for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
  228.             {
  229.                 $endFound = strpos( $toParse, $codeEnd, $lastFound );
  230.                 if ( $endFound === false )
  231.                 {
  232.                     continue;
  233.                 }
  234.                
  235.                 $contentsStart = $lastFound + strlen( $codeStart );
  236.                 $contents = substr( $toParse, $contentsStart, $endFound - $contentsStart );
  237.                
  238.                 $guts = str_replace( "___CONTENT___", $contents, $format ); // Couldn't think of a name, ok? :P
  239.                 $replaceEnd = $endFound + strlen( $codeEnd );
  240.                 $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
  241.                 $toParse = str_replace( $stuff, $guts, $toParse );
  242.                 $lastFound = $lastFound + strlen( $guts );
  243.             }
  244.         }
  245.        
  246.         foreach ( $elements_args as $code => $format )
  247.         {
  248.             $codeStart = '[' . $code . '';
  249.             $codeEnd = '[/' . $code . ']';
  250.             for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
  251.             {
  252.                 if ( $toParse[ $lastFound + strlen( $codeStart ) ] !== '=' )
  253.                 {
  254.                     continue;
  255.                 }
  256.                
  257.                 $elemEnd = strpos( $toParse, "]", $lastFound );
  258.                 if ( $elemEnd === false )
  259.                 {
  260.                     return false;
  261.                 }
  262.                
  263.                 $endFound = strpos( $toParse, $codeEnd, $lastFound );
  264.                 if ( $endFound === false )
  265.                 {
  266.                     continue;
  267.                 }
  268.                
  269.                 $argStart = $lastFound + strlen( $codeStart );
  270.                 $theArg = substr( $toParse, $argStart + 1, $elemEnd - $argStart - 1 );
  271.                
  272.                 $contentsStart = $argStart + 1 + strlen( $theArg ) + 1;
  273.                 $contents = substr( $toParse, $contentsStart, $endFound - $contentsStart );
  274.                
  275.                 $guts = str_replace( "___CONTENT___", $contents, $format ); // Couldn't think of a name, ok? :P
  276.                 $guts = str_replace( "___ARGUMENT___", $theArg, $guts );
  277.                 $replaceEnd = $endFound + strlen( $codeEnd );
  278.                 $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
  279.                 $toParse = str_replace( $stuff, $guts, $toParse );
  280.                 $lastFound = $lastFound + strlen( $guts );
  281.             }
  282.         }
  283.        
  284.         foreach ( $elements_nocontent as $code => $format )
  285.         {
  286.             $codeStart = '[' . $code . '';
  287.             for ( $lastFound = strpos( $toParse, $codeStart ); $lastFound !== false; $lastFound = @strpos( $toParse, $codeStart, $lastFound + 1 ) )
  288.             {
  289.                 if ( $toParse[ $lastFound + strlen( $codeStart ) ] !== '=' )
  290.                 {
  291.                     continue;
  292.                 }
  293.                
  294.                 $elemEnd = strpos( $toParse, "]", $lastFound );
  295.                 if ( $elemEnd === false )
  296.                 {
  297.                     return false;
  298.                 }
  299.                
  300.                 $argStart = $lastFound + strlen( $codeStart );
  301.                 $theArg = substr( $toParse, $argStart + 1, $elemEnd - $argStart - 1 );
  302.                
  303.                 $guts = str_replace( "___ARGUMENT___", $theArg, $format );
  304.                 $replaceEnd = $elemEnd + 1;
  305.                 $stuff = substr( $toParse, $lastFound, $replaceEnd - $lastFound );
  306.                 $toParse = str_replace( $stuff, $guts, $toParse );
  307.                 $lastFound = $lastFound + strlen( $guts );
  308.             }
  309.         }
  310.        
  311.         $parsedText .= $toParse;
  312.     }
  313.    
  314.     return $parsedText;
  315. }
  316.  
  317. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement