Jump to content

php highlight AutoIT syntax/language?


 Share

Recommended Posts

since i did not find what i was looking for i starting coding myself:

ToDo:

- highlight predefined functions

- highlight comments

- highlight other AI stuff.

- make it compatible with PHPbb and PowerInvision Board (2 different files?)

- make it secure for html/sql injections..

Bugs:

- Escapes one to many /

function autoit_first_pass($string) {
    // replace /n with <br>
    $string = nl2br($string);

        // Array for entities (under testing!!)
#       $ai_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
#       $ai_entities_replace = array('<', '>', '"', ':', '[', ']', '(', ')', '{', '}');
        // Replace enities (under testing!!)
#       $string = preg_replace($ai_entities_match, $ai_entities_replace, $string);

    // Replace 2 spaces with "  " so non-tabbed code indents without making huge long lines.
    $string = str_replace("  ", "  ", $string);
    // Replace 2 spaces with "  " so non-tabbed code indents without making huge long lines.
    $string = str_replace("  ", "  ", $string);
    // now Replace 2 spaces with "  " to catch odd #s of spaces.
    $string = str_replace("  ", "  ", $string);
    // Replace tabs with "   " so tabbed code indents sorta right without making huge long lines.
    $string = str_replace("\t", "   ", $string);
    // now Replace space occurring at the beginning of a line
    $string = preg_replace("/^ {1}/m", ' ', $string);


    /****** bbcode replace ******/
    // define what to be replaced via the preg_replace function (read php manual)
    $aibb = array('`\[autoit\](.+?)\[/autoit\]`is');
      // define what to be replaced by via the preg_replace function (read php manual)
    $replaceaibb = array('<span style="display: block; padding: 5px; border: 1px dashed; background-color: #CCCCCC;">\\1</span>');

    /****** keyword replace ******/
    // Define keywords as array (AutoIT 3)
    $aikeywords = array("continueloop", "and", "byref", "case", "const", "dim", "do", "else", 
                "elseif", "endfunc", "endif", "endselect", "exit", "exitloop", "for",
                "func", "global", "if", "local", "next", "not", "or", "redim", "return",
                "select", "step", "then", "to", "until", "wend", "while");
    // Run a loop for each keyword
    foreach($aikeywords as $k => $keyword) { 
    $aikeywordspattern[$k] = "/\b($keyword)\b/is";
    $replaceaikeywords[$k] = '<span style="font-weight: bold; color: #0000FF;">\\1</span>'; 
    } 

    /****** macro replace ******/
    // Define macros as array (AutoIT 3)
    $aimacros = array("@appdatacommondir","@appdatadir","@autoitexe","@autoitversion","@commonfilesdir",
            "@compiled","@computername","@comspec","@cr","@crlf","@desktopcommondir","@desktopdepth","@desktopdir",
            "@desktopheight","@desktoprefresh","@desktopwidth","@documentscommondir","@error","@extended",
            "@favoritescommondir","@favoritesdir","@gui_ctrlhandle","@gui_ctrlid","@gui_winhandle","@homedrive",
            "@homepath","@homeshare","@hour","@inetgetactive","@inetgetbytesread","@ipaddress1","@ipaddress2",
            "@ipaddress3","@ipaddress4","@lf","@logondnsdomain","@logondomain","@logonserver","@mday","@min",
            "@mon","@mydocumentsdir","@numparams","@osbuild","@oslang","@osservicepack","@ostype","@osversion",
            "@programfilesdir","@programscommondir","@programsdir","@scriptdir","@scriptfullpath","@scriptname",
            "@sec","@startmenucommondir","@startmenudir","@startupcommondir","@startupdir","@sw_disable",
            "@sw_enable","@sw_hide","@sw_maximize","@sw_minimize","@sw_restore","@sw_show","@sw_showdefault",
            "@sw_showmaximized","@sw_showminimized","@sw_showminnoactive","@sw_showna","@sw_shownoactivate",
            "@sw_shownormal","@systemdir","@tab","@tempdir","@username","@userprofiledir","@wday","@windowsdir",
            "@workingdir","@yday","@year");
    // Run a loop for each macro
    foreach($aimacros as $m => $macro) {
    $aimacrospattern[$m] = "/($macro)/is";
    $replaceaimacros[$m] = '<span style="font-weight: bold; color: #FF33FF;">\\1</span>';
    }

    // Replace the BBCODE
CONTENT
$string = preg_replace($aibb, $replaceaibb , $string); // Replace keywords with highlighted keywords $string = preg_replace($aikeywordspattern, $replaceaikeywords, $string); // Replace macros with highlighted macros $string = preg_replace($aimacrospattern, $replaceaimacros, $string); return $string; }

here is a preview:

Posted Image

Edited by mastrboy

 -

Link to comment
Share on other sites

I think that mastrboy is looking to display AutoIt code on a webpage using PHP. mastrboy would like the webpage to look as "colorful" as the SciTE4AutoIt3 editor. (but I might be wrong)

You are absolutly right :lmao:

It would be so much readable for others, rather than copying it into SCiTE and then reading the code.

But the question remains, does this exist? or is it going to be apart of my new webpage project ? ;)

it's already done for the PHP language so if anyone want's to give it a try before i do here is something you can use as sort of a template: http://www.isolated-designs.net/stuff/func..._highlight.phps

 -

Link to comment
Share on other sites

i could not find anything after hours of googling so i started myself :lmao:

here is first progress, currently it only highlights keywords, but more to come ;)

ops: i will not implement highlighting of predefined and user-defined function, it's just to much work.

function autoit($string) {
        // replaces \n with <br>
        $string = nl2br($string);

        /****** bbcode replace ******/
        // define what to be replaced via the preg_replace function (read php manual)
        $aibb = array('`\[autoit\](.+?)\[/autoit\]`is');
          // define what to be replaced by via the preg_replace function (read php manual)
        $replaceaibb = array('<span style="padding: 5px; border: 1px dashed; background-color: #CCCCCC;">\\1</span>');

        /****** keyword replace ******/
        // Define keywords as array (AutoIT 3)
        $aikeywords = array("continueloop", "and", "byref", "case", "const", "dim", "do", "else",
                                "elseif", "endfunc", "endif", "endselect", "exit", "exitloop", "for",
                                "func", "global", "if", "local", "next", "not", "or", "redim", "return",
                                "select", "step", "then", "to", "until", "wend", "while");
        // Run a loop for each keyword
        foreach($aikeywords as $k => $keyword) {
        $aikeywordspattern[$k] = "/\b($keyword)\b/is";
        $replaceaikeywords[$k] = '<span style="font-weight: bold; color: #0000FF;">\\1</span>';
        }


        // Replace the BBCODE
CONTENT
$string = preg_replace($aibb, $replaceaibb , $string); $string = preg_replace($aikeywordspattern, $replaceaikeywords, $string); return $string; }

btw; im going to use SCiTE's coloring...

Edited by mastrboy

 -

Link to comment
Share on other sites

but it would look like shit in a database and increase the size.

And if you want to edit the post after it's posted you'l get pure html instead of just autoit code surrounded by bbcode tags :lmao:

Nobody said store it. What I had in mind was using php to run SciTE in the background on the server and have it export a file as HTML. Then in you read the exported HTML file and display it. You can either write a Lua script in SciTE which on startup will do what you want or you might even be able to get away with using SciTE's command line. And since SciTE runs on Windows or Linux, there's a good chance your server can spawn it if you're able to get it on the machine.

Basically all I'm suggesting is using PHP to drive SciTE to do the syntax highlighting instead of re-writing a syntax highlighter from scratch.

Link to comment
Share on other sites

  • Developers

anyone know what font SCiTE is using?

It depends what you have defined for $(font.monospace) in SciTEUser.properties.

The Font.base is used when not using MONOSPACE font else whatever you defened for $(font.monospace)

# Give symbolic names to the set of fonts used in the standard styles.

if PLAT_WIN

font.base=font:Verdana,size:10,$(font.override) font.small=font:Verdana,size:9,$(font.override)

font.comment=font:Tahoma,size:10,$(font.override)

font.code.comment.box=$(font.comment),$(font.override)

font.code.comment.line=$(font.comment),$(font.override)

font.code.comment.doc=$(font.comment),$(font.override)

font.text=font:Verdana,size:10,$(font.override)

font.text.comment=font:Verdana,size:10,$(font.override)

font.embedded.base=font:Verdana,size:10,$(font.override)

font.embedded.comment=font:Comic Sans MS,size:10,$(font.override)

font.vbs=font:Lucida Sans Unicode,size:10,$(font.override)

font.monospace=font:Courier New,size:10

font.override=$(font.monospace)

html.properties contain the syntax highlight info for PHP:

# PHP

# Default

style.hypertext.118=fore:#000033,back:#FFF8F8,eolfilled

# Double quoted String

style.hypertext.119=fore:#007F00,back:#FFF8F8

# Single quoted string

style.hypertext.120=fore:#009F00,back:#FFF8F8

# Keyword

style.hypertext.121=fore:#7F007F,italics,back:#FFF8F8

# Number

style.hypertext.122=fore:#CC9900,back:#FFF8F8

# Variable

style.hypertext.123=fore:#00007F,italics,back:#FFF8F8

# Comment

style.hypertext.124=fore:#999999,$(font.comment),back:#FFF8F8

# One line comment

style.hypertext.125=fore:#666666,italics,$(font.comment),back:#FFF8F8

# PHP variable in double quoted string

style.hypertext.126=fore:#007F00,italics,back:#FFF8F8

# PHP operator

style.hypertext.127=fore:#000000,back:#FFF8F8

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...