Batch2ubb, a batch code posting colorizer script (CMD & GAWK), updated on 7.30
This is something I made back when I was learning AWK. There were always some unresolved problems, so I never posted it. Recently I haven’t been able to get online for a while, so I rewrote it and added a calling batch file that supports parameters. The multi-parameter calling framework is the great work of Willsort; I copied it directly. For details, see: A brief discussion of batch parameter issues http://www.cn-dos.net/forum/viewthread.php?tid=17785.
The main functionality is handled by the GAWK script. Clipboard operations depend on winclip.exe, and the syntax file was simplified from the batch syntax file of EmEditor that I use. download link for the required executable files:
winclip http://www.dmst.aueb.gr/dds/sw/outwit/outwit-bin-1.26.zip
gawk http://www.klabaster.com/progs/gawk32.zip
As for how to use this batch file, let me explain several calling methods:
1、Run it directly by double-clicking.
2、Call it from the command line; use Batch2ubb /h to view the specific parameter usage.
3、Create a shortcut in sendto and call it from there.
4、Create a shortcut and assign a hotkey to call it.
For method 1, it will convert the code in the clipboard into UBB code and write it back to the clipboard.
For method 3, the usage is: Start, Run, enter sendto, create a shortcut in the sendto directory pointing to Batch2ubb.cmd, then right-click the batch file you want to convert and use Send To on the shortcut you created; the converted code will be stored in the clipboard. This method is suitable for converting existing batch files.
For method 4, the usage is: create a shortcut pointing to Batch2ubb.cmd on the desktop or in the Start menu and assign a hotkey to it (for the latter location, it takes effect after reboot, or on the desktop hold down F5 for ten seconds for it to take effect), or let software such as hoekey do it for you. Then copy the code you want to post, press the hotkey, and you can paste it into the posting window. And you can also choose whether to enable the /n parameter in the shortcut target.
Also, please modify the name in the timestamp statement in Batch2ubb.awk to your own name. Questions or suggestions are welcome in replies; I’ll do my best to improve it.
Batch2ubb.cmd
Batch2ubb.awk
All files packaged for download are in the attachment:
[ Last edited by 无奈何 on 2006-7-30 at 23:53 ]
This is something I made back when I was learning AWK. There were always some unresolved problems, so I never posted it. Recently I haven’t been able to get online for a while, so I rewrote it and added a calling batch file that supports parameters. The multi-parameter calling framework is the great work of Willsort; I copied it directly. For details, see: A brief discussion of batch parameter issues http://www.cn-dos.net/forum/viewthread.php?tid=17785.
The main functionality is handled by the GAWK script. Clipboard operations depend on winclip.exe, and the syntax file was simplified from the batch syntax file of EmEditor that I use. download link for the required executable files:
winclip http://www.dmst.aueb.gr/dds/sw/outwit/outwit-bin-1.26.zip
gawk http://www.klabaster.com/progs/gawk32.zip
As for how to use this batch file, let me explain several calling methods:
1、Run it directly by double-clicking.
2、Call it from the command line; use Batch2ubb /h to view the specific parameter usage.
3、Create a shortcut in sendto and call it from there.
4、Create a shortcut and assign a hotkey to call it.
For method 1, it will convert the code in the clipboard into UBB code and write it back to the clipboard.
For method 3, the usage is: Start, Run, enter sendto, create a shortcut in the sendto directory pointing to Batch2ubb.cmd, then right-click the batch file you want to convert and use Send To on the shortcut you created; the converted code will be stored in the clipboard. This method is suitable for converting existing batch files.
For method 4, the usage is: create a shortcut pointing to Batch2ubb.cmd on the desktop or in the Start menu and assign a hotkey to it (for the latter location, it takes effect after reboot, or on the desktop hold down F5 for ten seconds for it to take effect), or let software such as hoekey do it for you. Then copy the code you want to post, press the hotkey, and you can paste it into the posting window. And you can also choose whether to enable the /n parameter in the shortcut target.
Also, please modify the name in the timestamp statement in Batch2ubb.awk to your own name. Questions or suggestions are welcome in replies; I’ll do my best to improve it.
Batch2ubb.cmd
Posted by 无奈何 on: 2006-07-30 23:44
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- :: Batch2ubb.cmd -V0.30 -- batch to UBB code
- :: 无奈何@cn-dos.net - updated on: 2005-7-30 - CMD & GAWK
- :: Usage: Batch2ubb
- :: Supported files: - gawk.exe winclip.exe Batch2ubb.awk batch.esy
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- @echo off
- setlocal
- set path=%path%;%cd%;%~dp0
- set error=
- rem File integrity check.
- for %%i in (gawk.exe winclip.exe batch.esy batch2ubb.awk) do (
- @if "%%~$PATH:i" == "" (
- echo Error: required file "%%i" is missing.
- set error=Error: program files are incomplete.
- ) else ( set %%~ni="%%~$PATH:i" )
- )
- if defined error goto error
- rem Test clipboard data type.
- winclip -i |findstr /r ".*_.*TEXT" >nul
- if ERRORLEVEL 1 set error=Error: the clipboard is empty or contains non-text data!
- rem With no parameters, get clipboard data and copy the converted result to the clipboard.
- if "%~1" == "" (
- if defined error goto error
- winclip -p |gawk -v line=1 -f %Batch2ubb% |winclip -c
- goto end
- )
- rem If there is only one parameter, try handling it as an input file; if it does not exist, handle it as a parameter.
- if "%~2" == "" (
- if exist "%~1" (
- gawk -v line=1 -f %Batch2ubb% "%~1" |winclip -c
- goto end
- )
- )
- set line=1
- set input=
- set out=
- :ParseLoop
- if "%~1" == "" goto Start
- if "%~1" == "/?" goto SwitchH
- rem Process parameters and jump to the corresponding label.
- for %%s in (i I n N o O h H) do if "%~1"=="/%%s" goto Switch%%s
- set error=Error: incorrect parameter format - "%1"!
- goto error
- :SwitchI
- set "input=%~2"
- if not exist "%input%" set error=Warning: file "%input%" does not exist. & goto error
- goto Next2Arg
- :SwitchO
- set "out=%~2"
- if not defined out set error=Warning: please specify an output file. & goto error
- goto Next2Arg
- :SwitchN
- set line=0
- goto NextArg
- :Next2Arg
- shift
- :NextArg
- shift
- goto ParseLoop
- rem Handle different cases according to different parameters.
- :Start
- if defined input (
- if defined out (
- gawk -v line=%line% -f %Batch2ubb% "%input%" >"%out%"
- ) else (
- gawk -v line=%line% -f %Batch2ubb% "%input%" |winclip -c
- )
- ) else (
- if defined out (
- if defined error goto error
- winclip -p |gawk -v line=%line% -f %Batch2ubb% >"%out%"
- ) else (
- if defined error goto error
- winclip -p |gawk -v line=0 -f %Batch2ubb% |winclip -c
- )
- )
- goto end
- :error
- echo.%error%
- echo.
- :SwitchH
- echo.Batch to UBB code.
- echo.Batch2ubb
- echo.
- echo. /i Specify the file to convert; may include a path; by default input is taken from the clipboard.
- echo. /o Specify the output file; may include a path; by default output goes to the clipboard.
- echo. /n Generate UBB code without line numbers; by default it includes line numbers.
- echo. /h Show this brief help, equivalent to /?.
- echo. This program depends on the following files: gawk.exe winclip.exe Batch2ubb.awk batch.esy.
- echo.
- :end
Batch2ubb.awk
###########################################################
# Batch2ubb.awk -V0.2 -- batch to UBB code script
# 无奈何 - 2005-7-2 -- GAWK script
###########################################################
#Read the configuration file and get keywords
BEGIN {
IGNORECASE=1
while ((getline<"batch.esy") >0){
if ($0!~/^*;/ && $0!~/^$/) {
if ($0~/^*#LINECOMMENT=/){
LineComment=substr($0,index($0,"=")+1)
} else if ($0~/^*#VARIABLE=/){
Variable=substr($0,index($0,"=")+1)
} else if ($0~/^*#PARAMETER=/){
Parameter=substr($0,index($0,"=")+1)
} else if ($0~/^*#LABEL=/){
Label=substr($0,index($0,"=")+1)
} else if ($0~/^*#KEYWORD/){
color=substr($0,index($0,"=")+1)
} else {
keyword=color
}
}
}
print ""
if (line) print ""
}
#Main program starts
{
before=""
after=""
#Handle comment lines and label lines
if (($0~/^*::/) || ($0~/^*REM\>/)){
gsub(/=.*]/,"=" LineComment "]",before)
$0=before $0 after
}else if ($0~/^*:+/){
gsub(/=.*]/,"=" Label "]",before)
$0=before $0 after
} else {
temp=$0
#Split out keywords
gsub(/+/,"□",temp)
gsub(/(^□|□$)/,"",temp)
len=split(temp,word,"□+")
for (i=1;i<=len;i++){
# The COLOR keyword overlaps with UBB color characters, handle it separately
if (word~/COLOR/){
if(!fcolor){
gsub(/=.*]/,"=" keyword "]",before)
gsub("\\<" word "\\>",before "&" after,$0)
delete word
fcolor=1
} else
delete word
}
#Delete duplicate keywords
for (j=i+1;j<=len;j++){
if (word==word)
delete word
}
}
for (i=1;i<=len;i++){
for (aa in keyword){
if (aa==toupper(word)){
gsub(/=.*]/,"=" keyword "]",before)
gsub("\\<" word "\\>",before "&" after,$0)
}
}
}
#Handle details such as variables and command parameters
gsub(/=.*]/,"=" Variable "]",before)
gsub (/%+*%?/,before "&" after,$0)
gsub (/!+*!*/,before "&" after,$0)
gsub(/=.*]/,"=" Parameter "]",before)
gsub (/ \/+/,before "&" after,$0)
gsub (/(\(|\))/,"&",$0)
gsub (/(\")/,"&",$0)
}
if (line) print "- " $0
else print $0
}
END {
if (line) printf "
"
#Add timestamp, modify the author name yourself
print "" strftime("\t无奈何发表于: %Y-%m-%d %H:%M") ""
print "
"
}
All files packaged for download are in the attachment:
[ Last edited by 无奈何 on 2006-7-30 at 23:53 ]
Recent Ratings for This Post
( 3 in total)
Click for details
Attachments
☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

DigestI