Icy Phoenix

     
 

PHP Based Code To HTML With BBcode

PHP Based Code To HTML With BBcode

Article
Reply with quote    Download Post  
Post PHP Based Code To HTML With BBcode 
 
]I wrote this PHP script to convert JAVA, PYTHON, VISUAL BASIC, and VISUAL BASIC .NET to formated html, using a style sheet.
i.e.

Code: [Download] [Hide]
  1. // Sample Class provide to demostrate how to create Abritute methods in Java  
  2. public class Sample1  
  3. {  
  4.     // internal variables  
  5.     private int age;  
  6.     // Asscesor or Get Method  
  7.     public int getAge()  
  8.     {  
  9.         return age;  
  10.     }  
  11.     // Mutator or Set Method  
  12.     public void setAge(int value)  
  13.     {  
  14.         age = value;  
  15.     }  


is converted to

Code: [Download] [Hide]
  1. <span class="Comment">// Sample Class provide to demostrate how to create Abritute methods in Java</span><br>  
  2. <span class="Keywords">public</span> <span class="Keywords">class</span> Sample1<br>  
  3. {<br>  
  4.     <span class="Comment">// internal variables</span><br>  
  5.     <span class="Keywords">private</span> <span class="Keywords">int</span> age<span class="Operator">;</span><br>  
  6.     <span class="Comment">// Asscesor or Get Method</span><br>  
  7.     <span class="Keywords">public</span> <span class="Keywords">int</span> getAge<span class="Operator">(</span><span class="Operator">)</span><br>  
  8.     {<br>  
  9.         <span class="Keywords">return</span> age<span class="Operator">;</span><br>  
  10.     }<br>  
  11.     <span class="Comment">// Mutator or Set Method</span><br>  
  12.     <span class="Keywords">public</span> <span class="Keywords">void</span> setAge<span class="Operator">(</span><span class="Keywords">int</span> value<span class="Operator">)</span><br>  
  13.     {<br>  
  14.         age <span class="Operator">=</span> value<span class="Operator">;</span><br>  
  15.     }<br>  
  16. }<br> 


see this page for the output to user http://learntoprogram.net84.net/ind...itute&lang=java

I want to adapt this script so that it works with both, IcyPhoenix and phpBB3 BBcode for the [Code] tag.

Can someone give me some help with this, Thank you.

Also feel free to use this code your self, under the trems of either the CDDL or GPL

Also the code takes both a string for the code input or a filename that points to a source file, it also takes a language ID.

phpcode2html.tar.gz
Description: This is an archive of the code with all every thing I've done with it as of Sat 20 Sep 2008 11:31:31 EST. 
Download
Filename: phpcode2html.tar.gz
Filesize: 2.97 KB
Downloaded: 344 Time(s)




 
glenflet - View user's profile Send private message  
glenflet [ Sat 20 Sep, 2008 03:36 ]
Icy Phoenix is an open source project, you can show your appreciation and support future development by donating to the project.

Support us


PHP Based Code To HTML With BBcode

Comments
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Moved to "Other Mods Support" - - -



 
   
Inactive User [ Sat 20 Sep, 2008 08:36 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
I worked out how to do this with pbpBB3 BBcode, but I'm not sure how to do it with Icy Phoenix, can some one help m


EDIT: Tue 23 Sep, 2008 23:39
I found out My so called succes was not what it seams, the varable passing didn't work proply, it only worked because my test language was the defualt language.

So i need halp with passing, singal variable to phpBB3 BBcode, I aslo need to known how if possible to pass mulitable variables in phpBB3.



 
glenflet - View user's profile Send private message  
glenflet [ Tue 23 Sep, 2008 08:09 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Hi,
to be able to use these scripts within Icy Phoenix you need to alter bbcode.php and add highlighting there.

You can refer to CODE part in that file and apply what you need.

Regarding phpBB 3... I don't know.



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Thu 25 Sep, 2008 10:02 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
It may also be helpful if you add your attachments in .zip format only - Not everyone uses exotic compression.



 
   
Inactive User [ Thu 25 Sep, 2008 11:41 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
I Include the followign file in the BBcode.php
Code: [Download] [Hide] [Select]
<?php
/**
  *
  * @package PHPCode2Html
  * @version 1.0
  * @copyright (c) 2008 Glen Fletcher
  * @license DUAL LICENSE CDDL+GPL (http://www.opensource.org/licenses/cddl1.php, Common Development and Distribution License (CDDL)) & (http://opensource.org/licenses/GPL-license.php GNU Public License)
  *
  */
include("phpjava2html.php");
include(
"phppython2html.php");
include(
"phpvb2html.php");
include(
"phpvb_net2html.php");
/**
  *
  * Main Function
  *
  *
  * @param $filename - The Filename of the source file
  * @param $lang - The Language of the source file, string = "JAVA", "PYTHON", "VB", or "VB.NET" assumes JAVA
  *
  * @return The Content of the source file in html format.
  */
function source2html($filename, $lang = "JAVA")
{
    
$line = "";
    if (
file_exists($filename))
    {
        
$file = fopen($filename, "r");
        while (!
feof($file))
        {
            
$line = fgets($file,4096);
            
$lines = $lines . $line;
        }
        
fclose($file);
        
$lines = code2html($lines, $lang);
    }
    return
$lines;
}
/**
  *
  * Code Converter
  *
  *
  * @param $lines - The Source Code
  * @param $lang - The Language of the source file, string = "JAVA", "PYTHON", "VB", or "VB.NET" assumes JAVA
  *
  * @return The Content of the source file in html format.
  */
function code2html($lines, $lang = "JAVA")
{
    
$lines = ereg_replace("&lt;", "<", $lines);
    
$lines = ereg_replace("&gt;", ">", $lines);
    switch (
$lang)
    {
        case
"JAVA":
            
$lines = java2html($lines);
            break;
        case
"PYTHON":
            
$lines = python2html($lines);
            break;
        case
"VB":
            
$lines = vb2html($lines);
            break;
        case
"VB.NET":
            
$lines = vb_net2html($lines);
            break;
        default:
// Assume Java, if fail
            
$lines = java2html($lines);
            break;
    }
    return
$lines;
}
?>


I put the following in the syntax = php if statment
Code: [Download] [Hide] [Select]
else
{
    $text = code2html($text,strtoupper($item['params']['syntax']));
}


However when I tested it with lang = python, I get a blank code box, wwhy would this happen

I got the following HTML Output
Code: [Download] [Hide] [Select]
div class="code"><div class="code-header" id="codehdr2_2789d6ba" style="position: relative;">Code: [<a href="javascript:void(0)" onclick="ShowHide('code_2789d6ba','code2_2789d6ba',''); ShowHide('codehdr_2789d6ba', 'codehdr2_2789d6ba', '')">Hide</a>]</div><div class="code-header" id="codehdr_2789d6ba" style="position: relative; display: none;">Code: [<a href="javascript:void(0)" onclick="ShowHide('code_2789d6ba','code2_2789d6ba','');ShowHide('codehdr_2789d6ba','codehdr2_2789d6ba','')">Show</a>]</div><div class="code-content" id="code_2789d6ba" style="position: relative;"><ol class="code-list" start="1"></ol></div></div>


from

Code: [Download] [Hide] [Select]
[code linenumbers=true syntax=python]def Hello():
    return "Hello World"[/code]




 
glenflet - View user's profile Send private message  
glenflet [ Fri 26 Sep, 2008 04:18 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Try to add your code here:
Code: [Download] [Hide] [Select]
                $php_syntax = false;
                if(isset($item['params']['syntax']))
                {
                    if ($item['params']['syntax'] == 'php')
                    {
                        $html = strtr($text, array_flip(get_html_translation_table(HTML_ENTITIES)));
                        $html = highlight_string($html, true);
                        $html_search = array('<code>', '</code>', '<font color="', '</font', '&nbsp;', '<code style="color:#0000BB"></code>', '<code style="color:#0000BB"> </code>');
                        $xhtml_replace = array('', '', '<code style="color:', '</code', ' ', '', '');
                        $html = str_replace ($html_search, $xhtml_replace, $html);
                        $php_syntax = true;
                    }
                }


Add an elseif statement to check the syntax you want to apply, and then just apply the function to $html var.

Remember that we are in linenumbers = false now... but first try to apply the code there just to check if it works, then we will try to extend it to other parts.



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Fri 26 Sep, 2008 13:16 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Ok I've got every thing working now, thanks for the help.

How to Install this BBcode

EXTRACT phpcode2html.zip to includes/phpcode2html/

COPY code.css to templates/mg_themes/

OPEN includes/BBcode.php
FIND
Code: [Download] [Hide] [Select]
global $db, $board_config, $phpbb_root_path, $phpEx, $lang;

ADD AFTER
Code: [Download] [Hide] [Select]
include($phpbb_root_path . 'includes/phpcode2html/phpcode2html.'. $phpEx);

FIND
Code: [Download] [Hide] [Select]
                        $html_search = array('<font color="', '</font', '&nbsp;', '<code style="color:#0000BB"></code>', '<code style="color:#0000BB"> </code>', '>  <');
                        $xhtml_replace = array('<code style="color:', '</code', ' ', '', '', '>&nbsp;<');
                        $html = str_replace($html_search, $xhtml_replace, $html);
                        //PHP Highlight - End
                    }

ADD AFTER
Code: [Download] [Hide] [Select]
                    else
                    {
                        $text = html_entity_decode($text);
                        $text = ereg_replace(html_entity_decode("&nbsp;")," ",$text);
                        //Add Span based Code highlighting using style sheets
                        $html = code2html($text,strtoupper($item['params']['syntax']));
                        $html = ereg_replace("<br />n", "&nbsp;</span></li>n<li class="code-row"><span class="code-row-text">",$html);
                        $html = "<li class="code-row"><span class="code-row-text">" . $html;
                    }

FIND
Code: [Download] [Hide] [Select]
                    if ($item['params']['syntax'] == 'php')
                    {
                        $html = strtr($text, array_flip(get_html_translation_table(HTML_ENTITIES)));
                        $html = highlight_string($html, true);
                        $html_search = array('<code>', '</code>', '<font color="', '</font', '&nbsp;', '<code style="color:#0000BB"></code>', '<code style="color:#0000BB"> </code>');
                        $xhtml_replace = array('', '', '<code style="color:', '</code', ' ', '', '');
                        $html = str_replace ($html_search, $xhtml_replace, $html);
                        $php_syntax = true;
                    }

ADD AFTER
Code: [Download] [Hide] [Select]
                    else
                    {
                        $text = html_entity_decode($text);
                        $text = ereg_replace(html_entity_decode("&nbsp;")," ",$text);
                        //Add Span based Code highlighting using style sheets
                        $html = code2html($text,strtoupper($item['params']['syntax']));
                        $code_syntax = true;
                    }

FIND ALL
Code: [Download] [Hide] [Select]
$php_syntax

REPLACE WITH
Code: [Download] [Hide] [Select]
$code_syntax


OPEN templates/mg_themes/
FIND
Code: [Download] [Hide] [Select]
@import url("dock.css");

ADD AFTER
Code: [Download] [Hide] [Select]
@import url("code.css");



NOTE You need to repeat this step for any other installed styles, Also, if you have other style directory's code.css should be copyed to them as well

code.css.zip
Description: Code highlighting style sheet 
Download
Filename: code.css.zip
Filesize: 260 Bytes
Downloaded: 302 Time(s)
phpcode2html.zip
Description: Special Version of phpcode2html designed for use with BBcode there are a few necessary changes to my original code 
Download
Filename: phpcode2html.zip
Filesize: 7.2 KB
Downloaded: 296 Time(s)




 
Last edited by glenflet on Mon 29 Sep, 2008 03:11; edited 2 times in total 
glenflet - View user's profile Send private message  
glenflet [ Sat 27 Sep, 2008 05:13 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
That's nice, thanks for sharing.

I will move this to Icy Phoenix Customizations.



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Sun 28 Sep, 2008 12:03 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Thanks for this mod.  I have installed and get this message
Spoiler: [ Show ]


I looked at my bbcode. php and it's this line and not sure this a typo or not.
Spoiler: [ Show ]


Let me know,

Thank you,
xmenfile



 
xmenfile - View user's profile Send private message  
xmenfile [ Sun 28 Sep, 2008 17:04 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Sorry that was a small error on my part

I losted the escape charaters when I copyied the code into the post.

change
Code: [Download] [Hide] [Select]
$html = ereg_replace("<br />n", "&nbsp;</span></li>n<li class="code-row"><span class="code-row-text">",$html);

to
Code: [Download] [Hide] [Select]
$html = ereg_replace("<br />n", "&nbsp;</span></li>n<li class="code-row"><span class="code-row-text">",$html);

also change your next line to the following it will be incorret as well
Code: [Download] [Hide] [Select]
$html = "<li class="code-row"><span class="code-row-text">" . $html;




 
glenflet - View user's profile Send private message  
glenflet [ Mon 29 Sep, 2008 03:07 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
I've Recently Made Several improvements to PHP Code 2 Html, so if your useing it you may want to update to this version it has no known errors.

phpcode2html.zip
Description: PHP Code 2 Html Ver. 1.2 
Download
Filename: phpcode2html.zip
Filesize: 7.23 KB
Downloaded: 231 Time(s)




 
glenflet - View user's profile Send private message  
glenflet [ Mon 06 Oct, 2008 10:18 ]
Reply with quote    Download Post  
Post Re: PHP Based Code To HTML With BBcode 
 
Thanks for sharing, I'm sure CODING dedicated forums will appreciate this.

Good job.



 
Mighty Gorgon - View user's profile Send private message  
Mighty Gorgon [ Fri 10 Oct, 2008 13:14 ]
Display posts from previous:    

HideWas this topic useful?

Post new topic  Reply to topic  Page 1 of 1