|
 |
|
 |
12-05-2004, 04:39 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 20
|
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?
|
|
|
12-05-2004, 05:04 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,475
|
you lost me after the reference part, but have you check out highlight_string()?
__________________
Mike
|
|
|
12-05-2004, 05:14 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 20
|
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.
|
|
|
12-05-2004, 08:29 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 20
|
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.
|
|
|
12-06-2004, 04:26 AM
|
#5 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
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
|
|
|
12-06-2004, 03:25 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 20
|
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?
|
|
|
12-06-2004, 05:19 PM
|
#7 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
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
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:20 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|