Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-05-2004, 04:39 PM   #1 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Question Syntax Highlighter

Hi, I am building a page, that has among other things, script snipplets and how-to's for a script language built for a game. (it scripts events and does other things for map creation)

I plan to host the raw script in ascii form in the database, but upon displaying it, along with a text box that they can copy from, I want to show the highlighted snipplet.

So, what I need is a function into wich I can submit a string, and have it highlight the syntax for me. Variables in one color, numbers in another, etc, etc.

Problem is, I have no idea how to even start on this, with php. Anyone able to give me some pointers on how I can create this function?

For refference, a sample code I have created for show, looks like this.

Code:
* This comment brought to you by McDonalds. Have you had your break today?
(0:7) When somebody moves into position (24,43),
   (1:4) and they bump into a Furre,
      (3:4) within the rectangle (%position1) - (%position2),
         (4:1) only where the floor is type 26,
            (5:4) place object type @3.
            (5:200) emit message {bunch of text} to whoever set off the trigger.
The Comments are lines that start with *, % denotes variables, and @ denotes a pointer. the strings inside the {}'s i need to highlight, as well as the numbers. And I'd like to make sure the lines are indented properly.

Can anyone help?
Rafkin is offline   Reply With Quote
Old 12-05-2004, 05:04 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
you lost me after the reference part, but have you check out highlight_string()?
__________________
Mike
sde is offline   Reply With Quote
Old 12-05-2004, 05:14 PM   #3 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
that highlights php code, wich is why i was showing the refference, so that you can see, it is not php that I need to highlight, nor C, or C++, nor javascript, or any of the standard programming or scripting langauge.

This script is for a specific game, that I play online, in wich you can create maps and script them to do various things. The scripting langauge is fairly easy to understand and follow, as you can see from the refference.

What I need to do, though, is highlight that scripting language. Wich means building a highlighting function from scratch. Any idea on how I can get started on the right track?

I spend a lot of time, in the forumns for this game, giving help and creating dragonspeak scripts for various people. I am trying to make a website, where I can host some of the more commonly requested scripts, some more advanced scripts, and some tutorials or how-to's to using this script. This is why I would like the highlighter function to highlight the script.
Rafkin is offline   Reply With Quote
Old 12-05-2004, 08:29 PM   #4 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Ok, I just found some information on preg_replace command and regular expressions, wich I had at one time studied in perl, and should be able to re-familiarize myself with in php.

If I have any difficulties, I'll post here. I'm surprised there isn't a section under php on your site regarding regex.

Edit:
Ok, this is not gonna be easy. I started to create some patterns in regex to check against, but realized I was deleting a lot of the text without knowing it.

The difficult part is ignoring anything on a line after a * symbol.

What I want to do is turn this:
Quote:
* This comment brought to you by McDonalds. Have you had your break today?
(0:7) When somebody moves into position (24,43),
(1:4) and they bump into a Furre,
(3:4) within the rectangle (%position1) - (%position2),
(4:1) only where the floor is type 26,
(5:4) place object type @3.
(5:200) emit message {bunch of text} to whoever set off the trigger.
Into this:
Quote:
* This comment brought to you by McDonalds. Have you had your break today?
(0:7) When somebody moves into position (24,43),
(1:4) and they bump into a Furre,
(3:4) within the rectangle (%position1) - (%position2),
(4:1) only where the floor is type 26,
(5:4) place object type @3.
(5:200) emit message {bunch of text} to whoever set off the trigger.
I was trying to use
/^[^\*]*\(([0-5]):([0-9]+)\)/im

but suddenly realized anything before the (x:xx) would be deleted in the proccess because it matched the beginning of the string.

Is there any easier way to do this?

Last edited by Rafkin; 12-05-2004 at 10:23 PM.
Rafkin is offline   Reply With Quote
Old 12-06-2004, 04:26 AM   #5 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
preg_replace is probably the most direct route to go with this. Here's a quick example that works on comments and (0:7) lines:

PHP Code:

  $srch 
= Array(
     
'/^(\*.*)/',
     
'/\((\d*\:\d*)\)/',
  );
  
$replace = Array(
     
"<font color=aqua>\${1}</font>",
     
"(<font color=green>\${1}</font>)",
  );

  
$str preg_replace($srch$replace$foo); 
Instead of trying to create one regex for all replacements you can just keep adding search/replace items to the arrays and it'll be easier to maintain.

-r
idx is offline   Reply With Quote
Old 12-06-2004, 03:25 PM   #6 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Ok, I'll see what I can do with that.. the arrays do make it easier..

also, most scripts will be small, but occasionally I'll get one that is pretty big, such as

http://www.aakanaar.com/furcadia/scripts/clipboard.txt

so question is, should i pass something that big as one string, using the multi-line option in the regex? or should i pass that one line at a time? Wich would be faster, or more optimized?
Rafkin is offline   Reply With Quote
Old 12-06-2004, 05:19 PM   #7 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Quote:
Originally Posted by Rafkin
so question is, should i pass something that big as one string, using the multi-line option in the regex? or should i pass that one line at a time? Wich would be faster, or more optimized?
Without running a few timing tests (after the full highlighter is created) I don't know. For my above snippet, I passed the entire script as a string (without specifying a multi-line match) and it seemed to work.

I'd start off with passing the entire thing as a string and measure the time with the microtime function. (something like the microtime_float example)

Time it for awhile, maybe 10 runs and get an average, then change the script around to read the file line by line and process each separately.

If the preg_replace method seems fast enough on your large file (34.5k), then I wouldn't even worry about going line by line. Although, if you have time and want to test it out, it might be interesting to hear which is faster.. (might only make a difference when the files get very large.)

-r
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
btsethttpseeds / python / syntax error betadoctor All Other Coding Languages 0 09-03-2004 12:04 PM
PHP Syntax Query (What is this ?) JeC PHP 2 06-23-2004 03:49 AM
highlight_string($string) - php syntax highlighting sde PHP 0 07-30-2002 02:19 PM


All times are GMT -8. The time now is 07:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting