plugins/GeSHi.php
changeset 2 9e3258dfae15
parent 1 c715631f809a
child 4 97d85c22ced7
equal deleted inserted replaced
1:c715631f809a 2:9e3258dfae15
    20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    21  */
    21  */
    22 
    22 
    23 global $db, $session, $paths, $template, $plugins; // Common objects
    23 global $db, $session, $paths, $template, $plugins; // Common objects
    24 $GLOBALS['geshi_supported_formats'] = array(
    24 $GLOBALS['geshi_supported_formats'] = array(
    25     'abap', 'blitzbasic', 'cpp-qt', 'd', 'idl', 'lua', 'ocaml', 'python', 'smalltalk', 'vhdl', 'actionscript', 'bnf', 'csharp',
    25     'abap', 'actionscript3', 'actionscript', 'ada', 'apache', 'applescript', 'apt_sources', 'asm', 'asp', 'autoit', 'avisynth', 'bash', 'basic4gl', 'bf', 'blitzbasic', 'bnf', 'boo', 'caddcl', 'cadlisp', 'cfdg', 'cfm', 'cil', 'c_mac', 'cobol', 'c', 'cpp', 'cpp-qt', 'csharp', 'css', 'delphi', 'diff', 'div', 'dos', 'dot', 'd', 'eiffel', 'email', 'fortran', 'freebasic', 'genero', 'gettext', 'glsl', 'gml', 'gnuplot', 'groovy', 'haskell', 'hq9plus', 'html', 'idl', 'ini', 'inno', 'intercal', 'io', 'java5', 'java', 'javascript', 'kixtart', 'klonec', 'klonecpp', 'latex', 'lisp', 'lolcode', 'lotusformulas', 'lotusscript', 'lscript', 'lua', 'm68k', 'make', 'matlab', 'mirc', 'mpasm', 'mxml', 'mysql', 'nsis', 'objc', 'ocaml-brief', 'ocaml', 'oobas', 'oracle11', 'oracle8', 'pascal', 'perl', 'per', 'php-brief', 'php', 'pic16', 'pixelbender', 'plsql', 'povray', 'powershell', 'progress', 'prolog', 'providex', 'python', 'qbasic', 'rails', 'reg', 'robots', 'ruby', 'sas', 'scala', 'scheme', 'scilab', 'sdlbasic', 'smalltalk', 'smarty', 'sql', 'tcl', 'teraterm', 'text', 'thinbasic', 'tsql', 'typoscript', 'vbnet', 'vb', 'verilog', 'vhdl', 'vim', 'visualfoxpro', 'visualprolog', 'whitespace', 'winbatch', 'xml', 'xorg_conf', 'xpp', 'z80'
    26     'eiffel', 'ini', 'm68k', 'oobas', 'qbasic', 'smarty', 'visualfoxpro', 'ada', 'caddcl', 'fortran', 'inno', 'matlab',
       
    27     'oracle8', 'rails', 'sql', 'winbatch', 'apache', 'cadlisp', 'css', 'freebasic', 'io', 'mirc', 'pascal', 'reg', 'tcl', 'xml',
       
    28     'applescript', 'cfdg', 'delphi', 'genero', 'java5', 'mpasm', 'perl', 'robots', 'text', 'xpp', 'asm', 'cfm', 'diff', 'gml', 'java',
       
    29     'mysql', 'per', 'ruby', 'thinbasic', 'z80', 'asp', 'c_mac', 'div', 'groovy', 'javascript', 'nsis', 'php-brief', 'sas', 'tsql',
       
    30     'autoit', 'c', 'dos', 'haskell', 'latex', 'objc', 'php', 'scheme', 'vbnet', 'bash', 'cpp', 'dot', 'html', 'lisp',
       
    31     'ocaml-brief', 'plsql', 'sdlbasic', 'vb'
       
    32   );
    26   );
    33 
    27 
    34 // Knock out the existing <code> tag support
    28 // Knock out the existing <code> tag support
    35 $plugins->attachHook('text_wiki_construct', 'geshi_disable_tw_code($this);');
    29 $plugins->attachHook('text_wiki_construct', 'geshi_disable_tw_code($this);');
    36 
    30 
    54 function geshi_strip_code(&$text, &$codeblocks, $random_id)
    48 function geshi_strip_code(&$text, &$codeblocks, $random_id)
    55 {
    49 {
    56   global $geshi_supported_formats;
    50   global $geshi_supported_formats;
    57   $codeblocks = array();
    51   $codeblocks = array();
    58   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    52   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    59   preg_match_all('/<code type="?' . $sf . '"?>([\w\W]*?)<\/code>/ms', $text, $matches);
    53   $regexp = '/<(code|source) (?:type|lang)="?' . $sf . '"?>(.*?)<\/\\1>/s';
       
    54   preg_match_all($regexp, $text, $matches);
    60   
    55   
    61   // for debug
    56   // for debug
    62   /*
    57   /*
    63   if ( strstr($text, '<code type') )
    58   if ( strstr($text, '<code type') )
    64     die('processing codes: <pre>' . htmlspecialchars(print_r($matches, true)) . '</pre><pre>' . htmlspecialchars($text) . '</pre>' . htmlspecialchars('/<code type="?' . $sf . '"?>([\w\W]*?)<\/code>/'));
    59     die('processing codes: <pre>' . htmlspecialchars(print_r($matches, true)) . '</pre><pre>' . htmlspecialchars($text) . '</pre>' . htmlspecialchars($regexp));
    65   */
    60   */
    66   
    61   
    67   foreach ( $matches[0] as $i => $match )
    62   foreach ( $matches[0] as $i => $match )
    68   {
    63   {
    69     $codeblocks[$i] = array(
    64     $codeblocks[$i] = array(
    70         'match' => $match,
    65         'match' => $match,
    71         'lang' => $matches[1][$i],
    66         'lang' => $matches[2][$i],
    72         'code' => $matches[2][$i]
    67         'code' => $matches[3][$i]
    73       );
    68       );
    74     $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
    69     $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
    75   }
    70   }
    76 }
    71 }
    77 
    72 
    93   if ( !$did_header_tweak )
    88   if ( !$did_header_tweak )
    94   {
    89   {
    95     $did_header_tweak = true;
    90     $did_header_tweak = true;
    96     global $template;
    91     global $template;
    97     $template->add_header('<style type="text/css">
    92     $template->add_header('<style type="text/css">
    98       pre.geshi_highlighted {
    93       .geshi_highlighted {
    99         max-height: 1000000px !important;
    94         max-height: 1000000px !important;
       
    95         width: 600px !important;
       
    96         clip: rect(0px,auto,auto,0px);
       
    97         overflow: auto;
   100       }
    98       }
   101       pre.geshi_highlighted a {
    99       .geshi_highlighted a {
   102         background-image: none !important;
   100         background-image: none !important;
   103         padding-right: 0 !important;
   101         padding-right: 0 !important;
   104       }
   102       }
   105       </style>
   103       </style>
   106       ');
   104       ');
   113   foreach ( $codeblocks as $i => $match )
   111   foreach ( $codeblocks as $i => $match )
   114   {
   112   {
   115     $lang =& $match['lang'];
   113     $lang =& $match['lang'];
   116     $code =& $match['code'];
   114     $code =& $match['code'];
   117     
   115     
   118     $geshi = new GeSHi($code, $lang, null);
   116     $geshi = new GeSHi(trim($code), $lang, null);
   119     $geshi->set_header_type(GESHI_HEADER_PRE);
   117     $geshi->set_header_type(GESHI_HEADER_PRE);
   120     // $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
   118     // $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
   121     $geshi->set_overall_class('geshi_highlighted');
   119     $geshi->set_overall_class('geshi_highlighted');
   122     $parsed = $geshi->parse_code();
   120     $parsed = $geshi->parse_code();
   123     
   121