Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: Adding different colored PHP code in article  (Read 5420 times)

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Adding different colored PHP code in article
« on: December 13, 2007, 03:04:58 pm »

Hey dude's, I have searched the forum for an hour now in search for Mikas MOD that makes it possible to add code to an article as an example, and have it show like it was actual code, and also colored differently... I have not found anything yet, and I need it. anyone know what I'm talking about ?

If so please aid me in my quest...

Mikas site is fecked up, i think the MOD is there aswell, but you can't get off of his index page, no matter what...
Logged
"It's only dead fish that goes with the flow... "
Updated

stjepan

  • Full Member
  • ***
  • Karma: 3
  • Posts: 136
Re: Adding different colored PHP code in article
« Reply #1 on: December 13, 2007, 09:02:26 pm »

I believe it is called PHP Syntax Highlighter.  Unfortunately, I don't have it and there's no way to pull it out of Mika's site, so maybe somebody has in somewhere on the disk...

hafing

  • Full Member
  • ***
  • Karma: 1
  • Posts: 174
Re: Adding different colored PHP code in article
« Reply #2 on: December 14, 2007, 01:29:16 pm »

P-man did you find a solution / answer to your question.
I am interested to know how one can highlight codes in a webpage. It can come handy.
If you have found answer could you post your solution here?
 8)
Logged
I can be 2fast and 2curious

bramsyuur

  • Hero Member
  • *****
  • Karma: 23
  • Posts: 873
    • http://snews.vietbee.net
Re: Adding different colored PHP code in article
« Reply #3 on: December 14, 2007, 03:12:00 pm »

@Patric and Hafing:
I've created, long time ago, this article about the colorized php code (obiously is a spanish translation about the original mod posted on Mika's site).
You can see it here: http://snews.vietbee.net/mods/resaltado-de-cdigo-en-php/
My Spanish sNews Site is really outdated and this mod is for the 1.5 version, but I think that runs in 1.6 without a problem.
If I get time, I'll try to post it here in mod section.
Thanks,
bram.
Logged
La comunidad sNews en tu idioma!
Comunidad sNews en Español

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #4 on: December 17, 2007, 02:46:28 pm »

Mhm, great Bram, and Bob. Thought it might be located at Mika's fecked up site, but it's not possible to get it there, so I'm greatful to you guy's... Domo Arigatoo, Bram-san, and Bob, thank'ya very much ;)
Logged
"It's only dead fish that goes with the flow... "
Updated

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #5 on: December 18, 2007, 12:51:55 pm »

Worked perfectomundo, guy's... I'm almost masturbate'able in mind now ;) Looksie: http://p-ahlqvist.com/blog/highlite-code-test-article/
Wowsa, that was a christmas gift, god knows how much I've tried to reach Mika regarding this one...

Oups... one thing though... I have all my code being green for some odd reason, but Bram, your's is different colored... Have I missed something in translation perhaps ?

Just to be clear, I did this... entered this function in snews.php:
Code: [Select]
//MOD - HIGHLIGHT CODE
function highlight($text) {
$match = array('#<code>(.*?)</code>#se');
$replace = array("highlight_string(stripslashes('$1'), true)");
$code = preg_replace($match, $replace, $text);
$html = array(
'<font'=>'<span',
'style=\"color: \"'=>'style="color: ',
'</font>'=>'</span>',
'<code><span style="color: #000000">'=>'<code>',
'</span>n</code>'=>'</code>',
);
foreach ($html as $old =>$new){$code = str_replace($old, $new, $code);}
return $code;
}

And I changed in function center:
Code: [Select]
//file_include(str_replace('[break]', '', $text), $shorten);
//MOD -Highlite code modification
file_include(highlight(str_replace('[break ]', '', $text)), $shorten);

CSS for this:
Code: [Select]
code {font: normal 1em "Lucida Sans Unicode",serif;margin:0.2em;}
.code_con{border:1px solid #eee; background:#f5f5ec;width:450px;margin:1em 1em 1em 2.5em;padding:1em;}

The class only for containment of code block.

Then I add the snippets of choice inside the <code> brackets and it should be colored, eh ? Well it's not in my end, and therefor I suspect I've missed out on something, which just might have depended on my lacking in spanish ;)

Please, enlighten me, dudes... 'Coz this looks like crap although I'm on the way... Is there anything else in my CSS that messes this up, perhaps...
« Last Edit: December 18, 2007, 01:52:57 pm by Patric Ahlqvist »
Logged
"It's only dead fish that goes with the flow... "
Updated

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #6 on: December 18, 2007, 02:45:35 pm »

I had something like that in the back of my head and tried that:
Code: [Select]
<code>
<?php
// Just a comment
function highlight($text) {
$match = array('#<code>(.*?) #se');
$replace = array("highlight_string(stripslashes('$1'), true)");
$code preg_replace($match$replace$text);
$html = array( '''style="color: '''=>''' <span style="color: #000000">'=>'<code>','</span>n '=>'', ); foreach ($html as $old =>$new){$code str_replace($old$new$code);} return $code; }
?>

</code>

But it looks like number two here: http://p-ahlqvist.com/blog/highlite-code-test-article/
Logged
"It's only dead fish that goes with the flow... "
Updated

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #7 on: December 18, 2007, 02:54:34 pm »

I can, however, see in the source code that the spans are colored differently... mhm...somethings a'foot here... something evil...
Logged
"It's only dead fish that goes with the flow... "
Updated

bramsyuur

  • Hero Member
  • *****
  • Karma: 23
  • Posts: 873
    • http://snews.vietbee.net
Re: Adding different colored PHP code in article
« Reply #8 on: December 18, 2007, 03:24:52 pm »

I can, however, see in the source code that the spans are colored differently... mhm...somethings a'foot here... something evil...
Hey Patric!
Try to using another mark for the code. I mean,
change
Code: [Select]
$match = array('#<code>(.*?)</code>#se');to
Code: [Select]
$match = array('#<source>(.*?)</source>#se');Use, for colorize the code <source> and </source>
The problem was into your example in the demo page... the tag <code> appears on it.
Logged
La comunidad sNews en tu idioma!
Comunidad sNews en Español

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
Re: Adding different colored PHP code in article
« Reply #9 on: December 18, 2007, 03:37:05 pm »

Right now, I'm not in my dev machine.

Find file_include and try this

Code: [Select]
//FILE INCLUSION
function file_include($text, $shorten) {
$fulltext = substr($text, 0, $shorten);
$inc = strpos($fulltext, '</code>');
if ($inc > 0) {
$text = str_replace('<code>', '|&|<code>', $fulltext);
$text = str_replace('</code>', '|&|', $text);
$text = explode('|&|', $text);
$num = count($text); $iarray = array();
for ($i = 0; ; $i++) {
if ($i == $num) {break;}
if (strpos($text, '<code>')>0) {echo $text[$i];} else {$text = str_replace('<code>', '', $text); highlight(trim(strtolower($text[$i])));}}

$fulltext = $num[0].$num[1];}
// END CODE
« Last Edit: December 18, 2007, 03:39:53 pm by Rui Mendes »
Logged
Need a Job on Europe. Linkdin - Facebook / Group

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #10 on: December 19, 2007, 03:34:38 pm »

Thanks guy's, but no melasses... Bram's didn't do anything, and Rui's made the article itself go blank... it was editable, but not visible. :(

I've come to the conclusion that it hasn't got anything to do with CSS (as far as my understanding goes) as I have removed all styles for that segment of the site, and it still show without colors... so snews is doing something...
Logged
"It's only dead fish that goes with the flow... "
Updated

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
Re: Adding different colored PHP code in article
« Reply #11 on: December 22, 2007, 12:57:42 am »

Patric I did it.

No need highlight function, just add red lines in function file_include
Quote
function file_include($text, $shorten) {
   $fulltext = substr($text, 0, $shorten);
   $inc = strpos($fulltext, '</code>');
   if ($inc > 0) {
      $text = str_replace('<code>', '|&| <code>', $fulltext);
      $text = str_replace('</code>', '|&|', $text);
      $list = explode('|&|', $text); $num = count($list);
      for ($i = 0; $i < $num ; $i++) {
          $position=strpos($list[$i], '<code>');
          $txt .= empty($position) ? $list[$i] : highlight_string(stripslashes(str_replace('<code>','',$list[$i])), true);}
      $fulltext = $txt;}

      $inc = strpos($fulltext, '[/include]');
      if ($inc > 0) {

And to use it, just add in your articles
Code: [Select]
<code>
<?php
...
?>

</code>

Try put this example to inside the article
Code: [Select]
<code>
<?php
// Just a comment
function highlight($text) {
$match = array("#(.*?) #se");
$replace = array("highlight_string(stripslashes('$1'), true)");
$code preg_replace($match$replace$text);
$html = array( 'style="color: ''=>'' <span style="color: #000000">'=>'code',
'</span>n '=>'', ); 
foreach (
$html as $old =>$new){$code str_replace($old$new$code);} 
return 
$code;}
?>

</code>
« Last Edit: December 22, 2007, 01:30:17 am by Rui Mendes »
Logged
Need a Job on Europe. Linkdin - Facebook / Group

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
Re: Adding different colored PHP code in article
« Reply #12 on: December 22, 2007, 01:30:59 pm »

:( well, I've replied here: http://snewscms.com/forum/index.php?topic=6535.msg44118#msg44118 There's something wrong in the way my articles are being saved, possibly...
Logged
"It's only dead fish that goes with the flow... "
Updated

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: Adding different colored PHP code in article
« Reply #13 on: December 22, 2007, 05:20:47 pm »

TESTING REPORT
--------------------------------------
Rui and Patric... I can verify that this script is working perfectly in my current project. I just pasted in the red parts... added the dummy script to a new article... and it displays perfectly in all its colored glory. I did this in one of my MEMU projects that also includes "Pages in Categories" with our current SubCategories project... all running in harmony.  ;D

(just waiting for Rui's Subcats Admin panels updates)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU