How to delete all comments in a script?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

How to delete all comments in a script?

Post by carno » 23 Jan 2022, 03:06

I know how to extract a comment section in an AHK script:

Code: Select all

RegExMatch(script, "s)/\*(.*?)\*/", match)
MsgBox % match
However, I would very much appreciate if someone let me how to (write a script to) completely delete all comments (/* ... */) in a given script and come up with a script with no comments.


swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to delete all comments in a script?

Post by swagfag » 23 Jan 2022, 05:55

theres more to parsing comments than just checking /* and ;, particularly if u actually care about not mangling the script in the process
u may be able to rip ahk2exe's guts and repurpose them https://github.com/AutoHotkey/Ahk2Exe/blob/master/ScriptParser.ahk#L33-L175

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: How to delete all comments in a script?

Post by carno » 23 Jan 2022, 08:26

swagfag wrote:
23 Jan 2022, 05:55
theres more to parsing comments than just checking /* and ;, particularly if u actually care about not mangling the script in the process
u may be able to rip ahk2exe's guts and repurpose them https://github.com/AutoHotkey/Ahk2Exe/blob/master/ScriptParser.ahk#L33-L175
I agree. Both above scripts seem to mangle if not careful.

garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: How to delete all comments in a script?

Post by garry » 23 Jan 2022, 08:34

a small basic example to begin , should keep lines 01-14

Code: Select all

B:="/*", E:="*/" ,Z:=""
;---------------
gosub,a1
Loop,parse,e4,`n,`r
 {
 x:= A_LoopField
 x=%x%
 stringmid,p,x,1,1
 if (p=";")
   continue 
 If InStr(x,B)
   H1=1
 If InStr(x,E)
   {
   H1=0
   continue
   }
 if (H1="1")
   continue
 z .= x . "`r`n"
 }
msgbox,%z%
z=
return
;----------------------
A1:
e4=
(
Line_01
Line_02
Line_03
/*fff  ggg  hhh */
Line_04
Line_05
/*
comment
comment
comment
*/
Line_06
Line_07
/*  comment
comment
comment */
Line_08
Line_09;no comment
Line_10
;comment
Line_11
Line_12   ; comment ( this not removed  )
    ;   comment
Line_13
Line_14
)
return
;=======================================================================

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to delete all comments in a script?

Post by swagfag » 23 Jan 2022, 09:37

lines 01-14 are not all code lines. only the lines

Code: Select all

Line_01
Line_02
Line_03
Line_06
Line_07
should have been kept
image.png
image.png (30.43 KiB) Viewed 741 times

garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: How to delete all comments in a script?

Post by garry » 23 Jan 2022, 10:28

not sure but it seems work ...

Code: Select all

B:="/*", E:="*/" ,Z:=""
F1=%a_scriptdir%\test21.ahk
;---------------
gosub,a1
Loop,parse,e4,`n,`r
 {
 x:= A_LoopField
 x=%x%
 stringmid,p,x,1,1
 if (p=";")
   continue 
 If InStr(x,B)
   H1=1
 If InStr(x,E)
   {
   H1=0
   continue
   }
 if (H1="1")
   continue
 z .= x . "`r`n"
 }
ifexist,%f1%
  filedelete,%f1%
fileappend,%z%`r`n,%f1%,utf-8
;fileappend,%e4%`r`n,%f1%,utf-8
try
 run,notepad "%f1%"
try
 run,%f1%
z=
return
;----------------------
A1:
e4=
(Ltrim comments join`r`n `%
#warn
setworkingdir,%a_scriptdir%
e:=""
e .= "Line_01`r`n" 
e .= "Line_02`r`n"
e .= "Line_03`r`n"
/*fff  ggg  hhh */
e .= "Line_04`r`n"
e .= "Line_05`r`n"   
/*
comment
comment
comment
*/
e .= "Line_06`r`n"
e .= "Line_07`r`n"
/*  comment
comment
comment */
e .= "Line_08`r`n"
e .= "Line_09`r`n"  
e .= "Line_10`r`n"
aac=10               ;- this comment is removed from ltrim comments also the others ';'
;comment
e .= "Line_11`r`n"
e .= "Line_12`r`n"   
    ;   comment
e .= "Line_13`r`n"
e .= "Line_14`r`n"
msgbox, 262208,TEST,aac=%aac%`n%e%
e=
return
)
return
;=======================================================================

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: How to delete all comments in a script?

Post by carno » 23 Jan 2022, 16:40

Thanks, garry! I still remember your great and original YouTube contributions. :D

garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: How to delete all comments in a script?

Post by garry » 23 Jan 2022, 16:56

@carno thank you , I just know basics , I learn from professional scripters here in ahk-forum
I was lost because youtube always changed , I use yt-dlp.exe , ffmpeg additional needed to download m3u8 file in same folder or for MP3
https://github.com/yt-dlp/yt-dlp#update
https://ffmpeg.org/download.html#build-windows
for youtube use like :
MP4 :
RunWait, %comspec% /k yt-dlp %url% --encoding UTF-8 -o "%foldername%\`%(title)s$`%(uploader)s$`%(id)s.`%(ext)s"
MP3 :
RunWait, %comspec% /k yt-dlp %url% -x --audio-format mp3 --audio-quality 320k --youtube-skip-dash-manifest --embed-thumbnail -o "%fdmp3%\`%(title)s$`%(uploader)s$`%(id)s.`%(ext)s" ;- needs ffmpeg.exe
or download a M3U8 file :
RunWait, %comspec% /k yt-dlp %url% -o %foldername%\%filename%.mp4 ;- needs ffmpeg.exe

Post Reply

Return to “Ask for Help (v1)”