[Script] AHK No Comments

Post your working scripts, libraries and tools for AHK v1.1 and older
Awannaknow
Posts: 78
Joined: 01 Oct 2013, 08:07

[Script] AHK No Comments

17 Nov 2013, 14:50

Just drop file/folders and all comments in .ahk files will be removed. Close with Escape Keyboard Key. Destination files in the same folder as source files. Source Files not modified. New files named : SourceFileName(NoComments).ahk
Image
Back up your files just in case.

Code: Select all

/*
------------- AHK No Comments ---------------
			  By awannaknow
	http://www.autohotkey.net/~awannaknow/
AHK_L 32 UTF8
-is surely AHK Classic Compatible. Has to be tested Yet !
-----------------------------------------------------
*/
/*
------------- Friday 14th of October 2011 ----------

I Passed by and liked what Nazzal had done : AHKCaser
http://www.autohotkey.com/forum/topic72146.html&highlight=ahkcaser
I used his idea to make this one.
			That's my second Software !
-----------------------;-) ----------------------------
Just drop file/folders and all comments in .ahk files will be removed. Close with Escape Keyboard Key.
Destination files in the same folder as source files.
Source Files not modified.
New files named :
SourceFileName(NoComments).ahk

Back up your files just in case.
------------------------------------------------------
*/ 
/*
----------------------------- Credits -----------------------------
Special thanks to all knowledgeable Members which helped me, to say the least, or participated in any way or from which I borrowed code from their previous works, to get it done.
Truly, I only bring the idea and all the real work was done by  :
Frankie, [VxE], Morpheus
A special thanks to rbrtryn which did most of the work and even find this app name.
----- Feel free to contact me if I forgot to mention your name -----
*/
/*
----------------------------- AutoHotKey -----------------------------
Of course nothing of this would have been possible without AutoHotKey aka AHK :
						http://www.autohotkey.com
						Created by  : Chris Mallet
						And all new Versions/Branch
							created by Lexicos
*/
#NoEnv  
#Persistent  
#SingleInstance Force  
SetWorkingDir %A_ScriptDir%  
SendMode Input  
Gui, +AlwaysOnTop -Border  
Gui, Font, s13  
Gui, Color, Olive  
Gui, Add, Pic, x10 y15 icon2, %A_AhkPath%  
Gui, Add, Text, x+0 y-10 cwhite w295 h45 gGuiMove +Center, `nAHK No Comments 
Gui, Font, s10  
Gui, Add, Text, xp y+0 h30 Wp cWhite gGuiMove  vDrop +Center, Drop files and folders  
Gui, Show, y0 w355 h65 NoActivate, AHK No Comments  
Return  
GuiMove:  
   PostMessage, 0xA1, 2,,, A  
   Return  
GuiDropfiles:  
   Gui, Color, 88B000 
   Gui, -E0x10  
   GuiControl, ,Dropped, Please wait . . .  
   Loop, Parse, A_GuiEvent, `n, `r 
   {  
        If (! InStr(FileExist(A_Loopfield), "D")) { 
           If (! RegExMatch(A_LoopField, ".ahk$")) 
               Continue 
           AHKFileNoComments := RegExReplace(A_LoopField, "(\\.*)\.", "$1(NoComments).") 
           Strip(A_LoopField, AHKFileNoComments) 
        }  
        Else {  
            Loop % A_LoopField "\*.ahk",0,1  
            {  
               If (! RegExMatch(A_LoopFileFullPath, ".ahk$"))  
                  Continue  
               AHKFileNoComments := RegExReplace(A_LoopFileFullPath, "(\\.*)\.", "$1(NoComments).")  
               Strip(A_LoopFileFullPath, AHKFileNoComments)  
            }  
         }  
   }  
   Gui, Color, Olive  
   GuiControl, ,Drop, Drop files and folders  
   Gui, +E0x10  
Return  
     
     
GuiEscape:  
   ExitApp  
   
   
Strip( in, out )  
    {  
        Loop Read, %in%, %out%  
        {  
            TwoChars := SubStr(LTrim(A_LoopReadline), 1, 2)  
            If (TwoChars = "/*") { 
                BlockComment := True  
                Continue  
            }  
            If (TwoChars = "*/") { 
                BlockComment := False  
                ReadLine := RegExReplace(A_LoopReadLine, "\*/\s*")  
                If (Trim(ReadLine) <> "") { 
                    FileAppend %ReadLine% `n  
                }  
                Continue  
            }  
            If (BlockComment) { 
                Continue  
            }  
            If (InStr(A_LoopReadline, ";")) { 
                ReadLine := RegExReplace(A_LoopReadline, "^;.*$|\s+;.*$")  
                If (Trim(ReadLine) <> "") {  
                    FileAppend %ReadLine% `n  
                }  
                Continue  
            }  
            FileAppend %A_LoopReadLine% `n  
        }  
    }
------------- AHK No Comments ---------------
AHK_L 32 UTF8
-is surely AHK Classic Compatible. Has to be tested Yet !
-----------------------------------------------------
------------- Friday 14th of October 2011 ----------

I Passed by and liked what Nazzal had done : AHKCaser
Nazzal AHKCaser: AHKCaser [ AutoHotkey keywords caser ]
I used his idea to make this one.
That's my second Software !
-----------------------;-) ----------------------------
----------------------------- Credits -----------------------------
Special thanks to all knowledgeable Members which helped me, to say the least, or participated in any way or from which I borrowed code from their previous works, to get it done.
Truly, I only bring the idea and all the real work was done by :
Frankie, [VxE], Morpheus
A special thanks to rbrtryn which did most of the work and even find this app name.
----- Feel free to contact me if I forgot to mention your name -----
----------------------------- AutoHotKey -----------------------------
Of course nothing of this would have been possible without AutoHotKey aka AHK :
AutoHotkey
Created by : Chris Mallet
And all new Versions/Branch
created by Lexicos
And all the Other Creators with their respective Branches . . .
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: [Script] AHK No Comments

17 Nov 2013, 17:07

what would prompt one to remove all comments? in other words, there must be an application for this script. this application is not apparent to me at the moment. ergo, my inquiry. :ugeek:
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: [Script] AHK No Comments

18 Nov 2013, 14:13

In my case I have a script that generates a list of all lib files, sub-scripts, & includes that a given script depends on to run. I parse the files line by line and it is much easier to just strip out all comments before processing each file. I don't save the file once the comments are stripped, just process it.

Relayer
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: [Script] AHK No Comments

18 Nov 2013, 17:36

What you are doing with loops and parsing can be done with slightly more complex regex operations.

Code: Select all

Script_File := RegExReplace(Script_File, "ms`a)^\s*/\*.*?^\s*\*/\s*")	; Removes /* ... */
Script_File := RegExReplace(Script_File, "(?<!``)`;.*")			; Remove Everything after a ; unless the ; is escaped ie `;
Script_File := RegExReplace(Script_File, "m`a)^\s*$^\s*$")		; Remove double blank lines created by this process.
I am pretty sure it could all be done in a single RegEx if it needed to be.

http://ahkscript.org/boards/viewtopic.php?f=6&t=96
This is a link to a script I wrote that does extensive parsing of AHK files. You might find some useful bits of code in this script.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: [Script] AHK No Comments

19 Nov 2013, 00:53

you mean that tome of a script i painstakingly saved in my archives could be replaced by a single "slightly" more complex RegEx operation? :lol: :ugeek:
FanaticGuru wrote:What you are doing with loops and parsing can be done with slightly more complex regex operations.

Code: Select all

Script_File := RegExReplace(Script_File, "ms`a)^\s*/\*.*?^\s*\*/\s*")	; Removes /* ... */
Script_File := RegExReplace(Script_File, "(?<!``)`;.*")			; Remove Everything after a ; unless the ; is escaped ie `;
Script_File := RegExReplace(Script_File, "m`a)^\s*$^\s*$")		; Remove double blank lines created by this process.
I am pretty sure it could all be done in a single RegEx if it needed to be.

http://ahkscript.org/boards/viewtopic.php?f=6&t=96
This is a link to a script I wrote that does extensive parsing of AHK files. You might find some useful bits of code in this script.

FG
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Script] AHK No Comments

20 Nov 2013, 05:12

For line comments, the regex should be something like [ \t]+;.* (untested).
A comment flag that appears on the same line as a command is not considered to mark a comment unless it has at least one space or tab to its left.
Source: #CommentFlag
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: [Script] AHK No Comments

20 Nov 2013, 17:05

An improved single RegExReplace that removes comment blocks, comment lines, inline comments and blank lines.

Code: Select all

FileRead, Script_File, Calculator.ahk					;  Read AHK Script File into String

Script_File := RegExReplace(Script_File, "ms`a)(^\s*/\*.*?^\s*\*/\s*\v|(?-s)(^\s*;.*\R|[ \t]+;.*)|^\s+?\v)")	; Removes /* ... */ and ; ... and blank lines

MsgBox % Script_File
I used a MsgBox to display for testing but a MsgBox cannot display long strings in their entirety so large scripts will not display completely.

Once you strip out the comments you can do what you want with the Script_File string.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: [Script] AHK No Comments

20 Nov 2013, 17:54

fanatistic! i'll test and post should i see any issues, inter alia. :ugeek:
FanaticGuru wrote:An improved single RegExReplace that removes comment blocks, comment lines, inline comments and blank lines.

Code: Select all

FileRead, Script_File, Calculator.ahk					;  Read AHK Script File into String

Script_File := RegExReplace(Script_File, "ms`a)(^\s*/\*.*?^\s*\*/\s*\v|(?-s)(^\s*;.*\R|[ \t]+;.*)|^\s+?\v)")	; Removes /* ... */ and ; ... and blank lines

MsgBox % Script_File
I used a MsgBox to display for testing but a MsgBox cannot display long strings in their entirety so large scripts will not display completely.

Once you strip out the comments you can do what you want with the Script_File string.

FG

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Drugwash and 195 guests