Macro ForumFunctionsToQm
;WHEN TO USE
;To quickly transfer macros, functions, etc from a QM forum post to QM.
;Use when they are embedded in the post, not attached as qm file.
;HOW TO USE
;In QM forum select whole or part of the post and copy to the clipboard (Ctrl+C).
;Then switch to QM and run this macro.
;This macro parses clipboard text, creates folder "from forum", and creates the macros/functions there.
;MORE INFO
;The post must contain one or more properly formatted QM code blocks (macros, functions, etc).
;You can copy only part of post. If there is function name or other info above code, include it.
;This macro tries to extract QM item name, type and trigger from the one or two lines above the code.
;If there is no name, gives default name, eg "Macro23". On conflict renames.
;RECOMMENDED TRIGGER
;Add this macro to a menu whose trigger is QM events -> Add to a menu -> Menu bar.
;___________________________________________________________________________________
;out
;get clipboard text in HTML format
str s
s.getclip("HTML Format")
;out s
if(!s.len)
,;ge1
,mes- "The clipboard does not contain text copied from QM forum in HTML clipboard format." "" "x"
;get selected fragment
int i j
i=find(s "StartFragment:")
j=find(s "EndFragment:")
if(i<0 or j<0) goto ge1
i=val(s+i+14)
j=val(s+j+12)
if(!i or !j) goto ge1
s.get(s i j-i)
s-"<HTML><HEAD></HEAD><BODY>"; s+"</BODY></HTML>"
if(!_unicode) s.ConvertEncoding(CP_UTF8 0)
s.replacerx("(?i)(<\w+.*?) style=''.+?''" "$1") ;;Chrome moves CSS to the style attribute, and then HtmlDoc does not add newline in some places
;out s; ret
;parse HTML
HtmlDoc d.InitFromText(s)
;out d.GetHtml; ret
;get text of code blocks
int nCodes
ARRAY(MSHTML.IHTMLElement) ae
d.GetHtmlElements(ae "div")
for i 0 ae.len
,MSHTML.IHTMLElement e=ae[i]
,s=e.className; if(s!="codeblock") continue
,s=e.innerText
,s-"<[45]<58321>[45]>"; s+"<[45]<58322>[45]>"; e.innerText=s ;;mark to find name and trigger later
,nCodes+1
if(!nCodes) mes- "There is no code in the clipboard. In QM forum select text with one or more blocks of properly formatted QM code. It must include the macro name line or at least one other text line above code." "" "x"
;get name, item type and trigger
s=d.GetText
;out s; ret
type TNTI ~text ~name ~trigger ~itype
ARRAY(TNTI) a
ARRAY(str) as
if(!findrx(s "^(?:(Macro|Function|Menu|Toolbar|Member Function|Autotext|TS Menu|T.S. Menu) ([^\r\n]+?) *[])?(?:Trigger ([^\r\n]+?) *[])?\s*<[45]<58321>[45]>(?s)(.+?)<[45]<58322>[45]>" 0 8|4 as)) ret
for i 0 as.len
,TNTI& r=a[]
,r.itype=as[1 i]
,sel r.itype 1
,,case "Member Function" r.itype="Member"
,,case ["Autotext","TS Menu"] r.itype="T.S. Menu"
,r.name=as[2 i]
,r.trigger=as[3 i]
,r.text=as[4 i]
,
,r.text.replacerx("^Code:\s*(?:Copy\b.+?\bHelp(?:[])?)?" "" 4)
,lpstr k=r.text
,rep
,,rep() sel(k[0]) case ',' k[0]=9; k+1; case ';' k[0]=32; k+1; case else break
,,k=strchr(k 10)+1; if(k=1) break
,
,;out F"name='{r.name}' type='{r.itype}' trigger='{r.trigger}'"
,;out r.text
,;out "------"
,if(!r.text.end("[]")) r.text+"[]"
;ret
;create QM items
int ifolder=newitem("from forum" "" "Folder")
for i 0 a.len
,&r=a[i]
,newitem r.name r.text r.itype r.trigger +ifolder 16