Created
September 16, 2014 09:09
-
-
Save Lexikos/1c9ef4859fac57f69e6b to your computer and use it in GitHub Desktop.
Converts HTML content on the Clipboard to BBCode for ahkscript.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Converts HTML content on the Clipboard to BBCode for ahkscript.org | |
; Requires AutoHotkey v1.1 | |
; Produces best results when copying from Internet Explorer | |
if html := GetClipboardHTMLFragment() | |
Clipboard := ConvertHTMLFragmentToBBCode(html) | |
ConvertHTMLFragmentToBBCode(html) | |
{ | |
repl := [[ | |
(Join],[ | |
"<STRONG>(.*?)</STRONG>" , "[b]$1[/b]" | |
"<CODE.*?>(.*?)</CODE>" , "[c]$1[/c]" | |
"<EM>(.*?)</EM>" , "[i]$1[/i]" | |
"<BR.*?>" , "`n" | |
"<A\s.*?href=""(.*?)"".*?>(.*?)</A>" , "[url=$1]$2[/url]" | |
"<UL.*?>(.*?)</UL>" , "[list]$1[/list]`n" | |
"<LI.*?>(.*?)</LI>" , "`n[*]$1" | |
"<SPAN.*?size: (\d+)%;"">(.*?)</SPAN>" , "[size=$1]$2[/size]" | |
)]] | |
for i, r in repl | |
html := RegExReplace(html, "i)" r[1], r[2]) | |
; For debugging missed elements, comment out the following line: | |
html := RegExReplace(html, "<.*?>") | |
return html | |
} | |
GetClipboardHTMLFragment() | |
{ | |
if !DllCall("OpenClipboard", "ptr", 0) | |
return | |
if hdata := DllCall("GetClipboardData", "uint", DllCall("RegisterClipboardFormat", "str", "HTML Format"), "ptr") | |
{ | |
size := DllCall("GlobalSize", "ptr", hdata) | |
if pdata := DllCall("GlobalLock", "ptr", hdata, "ptr") | |
{ | |
data := StrGet(pdata, size, "utf-8") | |
if RegExMatch(data, "Os)<!--StartFragment-->\K.*(?=<!--EndFragment-->)", m) | |
{ | |
html := m.Value | |
} | |
DllCall("GlobalUnlock", "ptr", hdata) | |
} | |
} | |
DllCall("CloseClipboard") | |
return html | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment