bbCode [code] modification RC3

Tutorials for <a href="http://www.phpbbstyles.com/viewtopic.php?t=356">eXtreme Styles mod</a> for phpBB 2.0.

Moderator: Moderators

bbCode [code] modification RC3

Postby baz-x on Mon May 09, 2005 6:34 pm

I guess I'm going to rule you down :D - joke :)
I'm very proud to announce that I got the desired bbCode
Code: Select all
modification running.

[span style="font-size: 16px; color: #00bb00;"][b]bbCode [code] mod[/b][/span]
[span style="font-size: 14px;"]Release Candidate 3[/span]

Demo and "Download":
» none at the moment

PS: I know the code isn't the best, but I'm not so familiar with preg_match and preg_replace (tried this one first but haven't got that one running). I'm trying to improve it. I will improve it :)

[b]Please note:[/b]
- This snippet is currently for Aphrodite and its green version only. If you want it for your style just request it.
- If you do want to display download counter then set $hide['downloads'] to 0.
- If you want to completely disable download function then set $hide['download'] to 1.
- If you want to disable hide/show button then set $hide['hide'] to 1.
- Syntax highlighting works in another way than normal code. As of this disadvantage there is no highlighting possible when using php syntax highlighter ...

[b][color=darkred]Requirements:[/color][/b]
For this snippet you need to have XS Mod running. It's also possible to use this without XS mod but it needs core hacking (which I haven't done yet).

[b][color=darkgreen]Features[/color][/b]
- Number at the beginning of each line
- Download function (+download counter)
- Hide ( hide="1") (+button) :: this one will hide code per default
- Filename ( filename="snippet.txt")
- Starting number ( start="5")
- Highlighting ( highlight="6")
- Syntax highlighting ( syntax="php")

[b][color=darkgreen]Upcoming Features[/color] (maybe)[/b]
- nothing in list

[b][color=darkgreen]Changes since RC2[/color][/b] (files: xs_code_functions.tpl)
- took CA's version of replacing htmlspecialchars()ed characters with original ones
[u]these changes are marked red[/u]

All examples together will get this one:
[code][code filename="snippet.php" start="5" syntax="php" hide="1"]<?php
   echo "Hello world";
?>[/code][/code]
or
[code][code filename="snippet.txt" start="5" highlight="6" hide="1"]<?php
   echo "Hello world";
?>[/code]


In overall_header.tpl add this at the very beginning of your file:
Code: Select all
<?php

ob_start();

?>


and this at the very bottom of your file (also overall_header.tpl):
Code: Select all
<?php

$temppage = ob_get_contents();
ob_end_clean();

global $HTTP_GET_VARS, $HTTP_SERVER_VARS;

if ( !isset($HTTP_GET_VARS['dlcode']) && (!strpos('viewtopic.', $HTTP_SERVER_VARS['REQUEST_URI']) || !strpos('privmsgs.', $HTTP_SERVER_VARS['REQUEST_URI'])) )
{
   print($temppage);
}
?>


In privmsgs_read_body.tpl, posting_preview.tpl and privmsgs_preview.tpl, xs/xs_topic.tpl add this at the very beginning of your file:
Code: Select all
<!-- INCLUDE xs/xs_code_functions.tpl -->


And add this code at the end of style.css
[code filename="style.css" hide="1"].post-text .code-header {
margin: 0;
margin-bottom: 5px;
padding-left: 18px;
padding-bottom: 3px;
border-bottom: solid 1px #E0E0E0;
font-size: 11px;
line-height: 1.2em;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: normal;
color: #808080;
background: #FFF url(images/bg_code_header.gif) top left no-repeat;
}

.code-header a:link, .code-header a:visited { color: #666; text-decoration: none; border-bottom: dotted 1px #666; }
.code-header a:hover, .code-header a:active { color: #FF1010; text-decoration: none; border-bottom: dotted 1px #FF1010; }

.code-content { padding: 0; }

.code-list {
margin-top: 3px;
margin-right: 0;
margin-bottom: 3px;
}

@media all and (min-width: 0px) { .code-list { margin-left: 40px; } }

li.code-row {
white-space: normal;
border-top: 1px #E0E0E0 solid;
color: #BBB;
wrap-option: emergency;
}
.code-row-text {
color: #444;
}

div.code li.code-row-highlight {
color: #FF1010;
background-color: #F0F0F0;
border-color: #D8D8D8;
}

div.code li.code-row-highlight .code-row-text {
color: #FF1010;
}

li.code-row-first {
border-top: none;
}[/code]

Finally download this .tpl file and save it in templates/ca_aphrodite/xs/
[code filename="xs_code_functions.tpl" highlight="167-187"]<?php

global $lang, $phpbb_root_path, $phpEx, $hide, $HTTP_GET_VARS, $privmsg_id;

$privmsg_id = (isset($privmsg_id) && !empty($privmsg_id)) ? $privmsg_id : 'preview';

$hide['hide'] = 0;
$hide['download'] = 0;
$hide['downloads'] = 1;

$lang['Download_code'] = (isset($lang['Download_code']) && !empty($lang['Download_code'])) ? $lang['Download_code'] : 'download';
$lang['Downloaded_code'] = (isset($lang['Downloaded_code']) && !empty($lang['Downloaded_code'])) ? $lang['Downloaded_code'] : 'downloaded %s time(s)';
$lang['Hide_code'] = (isset($lang['Hide_code']) && !empty($lang['Hide_code'])) ? $lang['Hide_code'] : 'hide';
$lang['Show_code'] = (isset($lang['Show_code']) && !empty($lang['Show_code'])) ? $lang['Show_code'] : 'show';

function code_header($postid, $codeid, $item, $item_ary = array(), $viewtopic = true)
{
global $lang, $phpbb_root_path, $phpEx, $hide;

if ( $postid != 'preview' )
{
($hide['download'] || $hide['downloads']) ? '' : @include($phpbb_root_path . 'cache/code_downloads.' . $phpEx);
$link = ($hide['download']) ? '' : '[<a href="' . append_sid(( ($viewtopic) ? 'viewtopic.' . $phpEx . '?' : 'privmsg.' . $phpEx . '?folder=inbox&amp;mode=read&amp;') . 'p=' . $postid . '&amp;dlcode=' . $codeid .
( !empty($item) ? '&amp;item=' . $item : '' ) .
( !empty($filename) ? '&amp;filename=' . $item_ary['filename'] : '' )) .
'">' . $lang['Download_code'] . '</a>]';
$link .= ($hide['download'] || $hide['downloads']) ? '' : ' - ' . sprintf($lang['Downloaded_code'], isset($code_downloads[$postid][$item]) ? $code_downloads[$postid][$item] : 0) . ' -';
}

$file = ( !empty($item_ary['filename']) ? '(' . $item_ary['filename'] . ') ' : '' );
$code = '<div class="code"><div class="code-header" id="codehdr_' . $codeid . '_' . $item . '" style="position: relative;' . ( ($item_ary['hide']) ? ' display: none;' : '' ) . '">' . $lang['Code'] . ': ' . $file . $link . ' [<a href="javascript:void(0)" onclick="ShowHide(\'codehdr_' . $codeid . '_' . $item . '\', \'codehdr2_' . $codeid . '_' . $item . '\', \'\'); ShowHide(\'code_' . $postid . '_' . $item . '\', \'code2_' . $codeid . '_' . $item . '\', \'\');">' . $lang['Hide_code'] . '</a>]</div>';
$code .= '<div class="code-header" id="codehdr2_' . $codeid . '_' . $item . '" style="position: relative;' . ( ($item_ary['hide']) ? '' : ' display: none;' ) . '">' . $lang['Code'] . ': ' . $file . $link . ' [<a href="javascript:void(0)" onclick="ShowHide(\'codehdr2_' . $codeid . '_' . $item . '\', \'codehdr_' . $codeid . '_' . $item . '\', \'\'); ShowHide(\'code_' . $codeid . '_' . $item . '\', \'code2_' . $codeid . '_' . $item . '\', \'\');">' . $lang['Show_code'] . '</a>]</div>';
$code .= '<div class="code-content" id="code_' . $codeid . '_' . $item . '"' . ( ($item_ary['hide']) ? ' style="display: none;"' : '' ) . '><ol class="code-list" start="' . ( !empty($start) ? $start : '1' ) . '">';

return $code;
}

function code_lines($str, $start = '', $highlight = false, $syntax = false, $preview = false)
{
global $lang, $phpEx;

@include($phpbb_root_path . 'cache/code_downloads.' . $phpEx);

if ( empty($str) )
{
return;
}

$code = '';
$code_ary = explode("\n<br />\n", $str);
$highlight = is_string($highlight) ? explode(',', $highlight) : array();
$j = 0;
$start = intval(!empty($start) ? $start : 1);
$open_php_tag = 0;
$close_php_tag = 0;

for ( $i = 0; $i < sizeof($code_ary); $i++ )
{
if ( $syntax == 'php' )
{
$prefix = ( strpos(' ' . $code_ary[$i], '&lt;?') ) ? '' : '<?php ';
$suffix = ( strpos(' ' . $code_ary[$i], '?&gt;') ) ? '' : '?>';

$code_ary[$i] = str_replace(array('&lt;', '&gt;'), array('<', '>'), $code_ary[$i]);
$code_ary[$i] = highlight_string(strtr($prefix . $code_ary[$i] . $suffix, array_flip(get_html_translation_table(HTML_ENTITIES))), true);
$code_ary[$i] = str_replace(($open_php_tag || !empty($prefix)) ? '<font color="#0000CC">&lt;?php&nbsp;' : '', '<font color="#0000CC">', $code_ary[$i]);
$code_ary[$i] = str_replace(($open_php_tag || !empty($prefix)) ? '<font color="#007700">&lt;?</font><font color="#0000BB">php' : '', '<font color="#0000CC">', $code_ary[$i]);
$code_ary[$i] = str_replace(($close_php_tag || !empty($suffix)) ? '?&gt;' : '', '', $code_ary[$i]);

empty($prefix) ? $open_php_tag++ : ( ($open_php_tag) ? $open_php_tag-- : '' );
empty($suffix) ? $close_php_tag++ : ( ($close_php_tag) ? $close_php_tag-- : '' );
}

$line = $j + $start;
$code .= '<li class="code-row' . ( ($j == 0) ? ' code-row-first' : '' ) . ( (in_array($line, $highlight)) ? ' code-row-highlight' : '' ) . '"><span class="code-row-text">' . $code_ary[$i] . '&nbsp;</span></li>';
$j++;
}

$code .= '</ol></div>';

return $code;
}

$postrow_codes = array();
$postrow_count = ( isset($this->_tpldata['postrow.']) ) ? sizeof($this->_tpldata['postrow.']) : 1;
$postrow_viewtopic = ( isset($this->_tpldata['postrow.']) ) ? 1 : 0;
for ($postrow_i = 0; $postrow_i < $postrow_count; $postrow_i++)
{
if ( $postrow_viewtopic )
{
$postrow_item = &$this->_tpldata['postrow.'][$postrow_i];
$postrow_item['CODE_ID'] = $postrow_item['U_POST_ID'];
}
else
{
$postrow_item['MESSAGE'] = &$this->vars['MESSAGE'];
$postrow_item['U_POST_ID'] = $privmsg_id;
$postrow_item['CODE_ID'] = 'privmsgs_' . $privmsg_id;
}

preg_match_all("#(<div class=\"code\">(<h6>" . $lang['Code'] . ":<\/h6>)|\[code(.*?)\])(.*?)(<\/div>|\[\/code\])#is", $postrow_item['MESSAGE'], $code);
for ( $code_i = 0; $code_i < count($code[0]); $code_i++ )
{
$postrow_codes[$postrow_item['CODE_ID']][$code_i]['str'] = $code[4][$code_i];
$postrow_codes[$postrow_item['CODE_ID']][$code_i]['filename'] = '';

$item = array();
if ( !empty($code[3][$code_i]) )
{
$item_ary = explode(' ', str_replace('[code ', '', str_replace(']', '', $code[3][$code_i])));
foreach ( $item_ary as $command )
{
$split = explode('=', str_replace('"', '', $command));
$item[$split[0]] = $split[1];
}
}

$postrow_item['MESSAGE'] = str_replace($code[1][$code_i], code_header($postrow_item['U_POST_ID'], $postrow_item['CODE_ID'], $code_i, array('filename' => $item['filename'], 'hide' => $item['hide']), ($postrow_viewtopic) ? true : false), $postrow_item['MESSAGE']);

if ( !empty($code[3][$code_i]) )
{
$postrow_item['MESSAGE'] = str_replace($code[5][$code_i], '</div>', $postrow_item['MESSAGE']);
$postrow_codes[$postrow_item['CODE_ID']][$code_i]['filename'] = $item['filename'];

if ( $item['syntax'] == 'php' )
{
$postrow_item['MESSAGE'] = str_replace($code[4][$code_i], code_lines($code[4][$code_i], $item['start'], 0, 'php', ($postrow_item['U_POST_ID'] == 'preview') ? 1 : 0), $postrow_item['MESSAGE']);
}
else
{
$postrow_item['MESSAGE'] = str_replace($code[4][$code_i], code_lines($code[4][$code_i], $item['start'], $item['highlight'], 0, 0), $postrow_item['MESSAGE']);
}
}
else
{
$postrow_item['MESSAGE'] = str_replace($code[4][$code_i], code_lines($code[4][$code_i], $postrow_item['U_POST_ID'], $code_i, '', '', 0, 0), $postrow_item['MESSAGE']);
}
}
}

if ( isset($HTTP_GET_VARS['dlcode']) && !$hide['download'] )
{
$get_id = $HTTP_GET_VARS['dlcode'];
$get_item = isset($HTTP_GET_VARS['item']) ? $HTTP_GET_VARS['item'] : 0;
$filename = ( !empty($postrow_codes[$get_id][$get_item]['filename']) ) ? $postrow_codes[$get_id][$get_item]['filename'] : 'code_' . $get_id . ( ($get_item) ? '_' . $get_item : '' ) . '.txt';

// counting the downloads + preparing string for file
@include($phpbb_root_path . 'cache/code_downloads.' . $phpEx);
$code_downloads[$get_id][$get_item]++;
$str = array();
$str[] = '<?php ';
foreach ( $postrow_codes as $postid => $item_ary )
{
foreach ( $item_ary as $item => $code )
{
$str[] = '$code_downloads[' . $postid . '][' . $item . '] = ' . ( isset($code_downloads[$postid][$item]) ? $code_downloads[$postid][$item] : 0 ) . ';';
}
}
$str[] = ' ?>';

$fp = @fopen($phpbb_root_path . 'cache/code_downloads.' . $phpEx , 'w');
@fputs($fp, implode('', $str));
@fclose($fp);

$download_file = $postrow_codes[$get_id][$get_item]['str'];
$download_file = strip_tags($download_file);
// $download_file = strtr($download_file, array_flip(get_html_translation_table(HTML_ENTITIES)));
if(preg_match_all('/&\#([0-9]+);/', $download_file, $matches) && count($matches))
{
$list = array();

for ( $i = 0; $i < count($matches[1]); $i++ )
{
$list[$matches[1][$i]] = true;
}

$search = array();
$replace = array();

foreach ( $list as $var => $value )
{
$search[] = '&#' . $var . ';';
$replace[] = chr($var);
}

$download_file = str_replace($search, $replace, $download_file);
}
$download_file = str_replace("\n\n", "\n", $download_file);

@header('Content-Type: text/x-delimtext; name="' . $filename . '"');
@header('Content-Disposition: attachment; filename="' . $filename . '"');
@header('Content-Transfer-Encoding: plain/text');
@header('Content-Length: ' . strlen($download_file));

print($download_file);
exit;
}

?>[/code]
Last edited by baz-x on Sat Jun 04, 2005 10:45 am, edited 29 times in total.
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby PostBot on Mon May 09, 2005 6:40 pm

Very well done :D
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby baz-x on Mon May 09, 2005 6:45 pm

It wasn't really hard to get it over to phpBB. I'm using same function for my [php] bbcode at my own CMS, this one only hasn't got highlight_string() function.
Mhm ... there might be a problem updating progress about the snippet when I can't update first post ...
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby PostBot on Mon May 09, 2005 6:46 pm

What do you mean? You can edit your own posts so there should be no problems.
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby baz-x on Mon May 09, 2005 6:48 pm

Not in this forum.
You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You can vote in polls in this forum

Permissions wrong?
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby PostBot on Mon May 09, 2005 6:50 pm

Fixed.
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby baz-x on Mon May 09, 2005 6:56 pm

Really? I still can't edit my own posts ... the permission block looks the same like before.

(btw: @public: new version up at my support board. I got rid of the stupid div streching when line was too long ... waiting for the permission to edit my first post)
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby WinSrev on Mon May 09, 2005 6:59 pm

I seem to get the same information:

[quote=PHPBB Styles]You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You can vote in polls in this forum[/quote]
User avatar
WinSrev
Registered User
Registered User
 
Posts: 691
Joined: Sat Nov 27, 2004 10:44 pm
Location: UK

Postby baz-x on Mon May 09, 2005 7:43 pm

I forgot to mention that this snippet is currently for Aphrodite (+Green) only. It looks for style specific [code] tag (<div class="code"><h6> ...). If someone wants it for their style, I will help.

Update: now looks a little more like CA's code block.
Update available at my support board.
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby PostBot on Mon May 09, 2005 7:52 pm

[quote user="baz-x" post="32600"]Really? I still can't edit my own posts ... the permission block looks the same like before.[/quote]
You can. I've checked it by adding test user to moderator group and moderator group can edit posts in this forum.
[quote user="baz-x" post="32622"]Update: now looks a little more like CA's code block.[/quote]
Very nice :D
You are using completely different method, but result is the same and it works perfectly :D

Do you mind if I add this to next update for Aphrodite style?
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby baz-x on Mon May 09, 2005 7:55 pm

Yes, now I can. It took its time ... thanks for your confidence. I will do my best.
Sure you can. It's just a little snippet (and I saw your method ... I wonder if I can do it the same way (using <list>) - going to try that).
Regards
baz-x [blog] [website]
current projects:
» [span style="color: darkgreen"]Mex[/span] [span style="color: darkred"][ 75% ][/span]
» Nickpage Mod 2.0.0 ME [span style="color: red"][ paused ][/span]
» Style Demo Mod 1.2.2 ME [span style="color: red"][ paused ][/span]
User avatar
baz-x
Moderator
Moderator
 
Posts: 706
Joined: Sun Aug 15, 2004 9:12 pm
Location: Cache Management of Mex

Postby Thatbitextra on Mon May 09, 2005 7:59 pm

Looks the same, but doesn't work as well, sorry. If you highlight the code, the numbers are included, and hence copied.
User avatar
Thatbitextra
Moderator
Moderator
 
Posts: 1821
Joined: Thu Mar 24, 2005 1:11 pm
Location: A place where something is or could be located; a site.

Postby WinSrev on Mon May 09, 2005 8:01 pm

You quoted your previous post, anyway, when you copy code off this forum it also includes the numbers.
User avatar
WinSrev
Registered User
Registered User
 
Posts: 691
Joined: Sat Nov 27, 2004 10:44 pm
Location: UK

Postby Thatbitextra on Mon May 09, 2005 8:03 pm

:oops: missed edit and hit quote, sorry. Please delete.
User avatar
Thatbitextra
Moderator
Moderator
 
Posts: 1821
Joined: Thu Mar 24, 2005 1:11 pm
Location: A place where something is or could be located; a site.

Postby Disturbed One on Mon May 09, 2005 8:04 pm

[quote user="tbsn" post="32629"]You quoted your previous post, anyway, when you copy code off this forum it also includes the numbers.[/quote]If you use FireFox.
High Velocity Media - Accelerated Internet Solutions
http://hvmdesign.com | contact[at]hvmdesign[dot]com
User avatar
Disturbed One
Moderator
Moderator
 
Posts: 2106
Joined: Sat Oct 09, 2004 8:12 pm
Location: Right behind you...

Next

Return to eXtreme Styles mod tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron