Skip to content

Instantly share code, notes, and snippets.

@joedf
Created June 14, 2016 22:28
Show Gist options
  • Save joedf/457c5c13afa6c40a3beebe5af3beb2d0 to your computer and use it in GitHub Desktop.
Save joedf/457c5c13afa6c40a3beebe5af3beb2d0 to your computer and use it in GitHub Desktop.
Quick&Dirty Parser example for the "awesome-AutoHotkey" list.
;
; AutoHotkey Version: 1.1.24.00
; Dev Platform: Windows 10 Home Premium x64
; Author: Joe DF | http://joedf.ahkscript.org | joedf@ahkscript.org
; Date: 18:25 2016/06/14
; Description: Quick&Dirty Parser example for the "awesome-AutoHotkey" list.
#NoEnv
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;<<<<<<<< HEADER END >>>>>>>>>
;rFile:="sampletext.txt"
rFile:=A_Temp "\AHK_AList.tmp"
URLDownloadToFile,https://raw.githubusercontent.com/ahkscript/awesome-AutoHotkey/master/README.md,%rFile%
FileRead, rData, %rFile%
iData:=object(), CSect:="null"
Loop, parse, rData, `n, `r
{
z:=A_LoopField
If !StrLen(z)
continue
if InStr(z,"<br>")
NSect:=1
if (NSect) {
if (SubStr(z,1,3) == "## ") {
NSect:=0
StringReplace,SectN,z, % "## "
iData[SectN]:=Object()
CSect:=SectN
}
}
if StrLen(CSect) {
;/Parsing Main sections
if (SubStr(z,1,1) != "#") {
if StrLen(CSSect) {
if (SubStr(z,1,2) == "* ") {
;/Parsing the individual item
StringGetPos,a,z,[
StringGetPos,b,z,]
CSSectI:=Substr(z,a+2,b-(a+1))
iData[CSect][CSSect][CSSectI]:= z "`n"
} else {
;/Parsing Subsections
iData[CSect][CSSect].d .= z "`n"
}
} else
iData[CSect].d .= z "`n"
} else if (SubStr(z,1,4) == "### ") {
if InStr(z,"></a>") {
StringGetPos,a,z,></a>
CSSect:=SubStr(z,a+4+2)
} else
StringReplace,CSSect,z, % "### "
iData[CSect][CSSect]:=Object()
}
}
}
MsgBox Parsed data is in the "iData" var.
pData:=JSON_Beautify(json_fromobj(iData)," ")
gui, font, s8, Consolas
Gui, Add, Edit, w500 h300, % pData
Gui, Show,, JSON Data
return
GuiClose:
ExitApp
;//////////////////////////////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////////////////////////////
;////////////////////////////// Included Lib Functions ////////////////////////////////
;////////////////////////////////////// Below /////////////////////////////////////////
;//////////////////////////////////////////////////////////////////////////////////////
; Copyright © 2013 VxE. All rights reserved.
; Serialize an object as JSON-like text OR format a string for inclusion therein.
; NOTE: scientific notation is treated as a string and hexadecimal as a number.
; NOTE: UTF-8 sequences are encoded as-is, NOT as their intended codepoint.
json_fromobj( obj ) {
If IsObject( obj )
{
isarray := 0 ; an empty object could be an array... but it ain't, says I
for key in obj
if ( key != ++isarray )
{
isarray := 0
Break
}
for key, val in obj
str .= ( A_Index = 1 ? "" : "," ) ( isarray ? "" : json_fromObj( key ) ":" ) json_fromObj( val )
return isarray ? "[" str "]" : "{" str "}"
}
else if obj IS NUMBER
return obj
; else if obj IN null,true,false ; AutoHotkey does not natively distinguish these
; return obj
; Encode control characters, starting with backslash.
StringReplace, obj, obj, \, \\, A
StringReplace, obj, obj, % Chr(08), \b, A
StringReplace, obj, obj, % A_Tab, \t, A
StringReplace, obj, obj, `n, \n, A
StringReplace, obj, obj, % Chr(12), \f, A
StringReplace, obj, obj, `r, \r, A
StringReplace, obj, obj, ", \", A
StringReplace, obj, obj, /, \/, A
While RegexMatch( obj, "[^\x20-\x7e]", key )
{
str := Asc( key )
val := "\u" . Chr( ( ( str >> 12 ) & 15 ) + ( ( ( str >> 12 ) & 15 ) < 10 ? 48 : 55 ) )
. Chr( ( ( str >> 8 ) & 15 ) + ( ( ( str >> 8 ) & 15 ) < 10 ? 48 : 55 ) )
. Chr( ( ( str >> 4 ) & 15 ) + ( ( ( str >> 4 ) & 15 ) < 10 ? 48 : 55 ) )
. Chr( ( str & 15 ) + ( ( str & 15 ) < 10 ? 48 : 55 ) )
StringReplace, obj, obj, % key, % val, A
}
return """" obj """"
} ; json_fromobj( obj )
/*
* "JSON_Beautify.ahk" by Joe DF (joedf@users.sourceforge.net)
* ______________________________________________________________________
* "Transform Objects & JSON strings into nice or ugly JSON strings."
* Uses VxE's JSON_FromObj()
*
* Released under The MIT License (MIT)
* ______________________________________________________________________
*
*/
JSON_Uglify(JSON) {
if IsObject(JSON) {
return JSON_FromObj(JSON)
} else {
if JSON is space
return ""
StringReplace,JSON,JSON, `n,,A
StringReplace,JSON,JSON, `r,,A
StringReplace,JSON,JSON, % A_Tab,,A
StringReplace,JSON,JSON, % Chr(08),,A
StringReplace,JSON,JSON, % Chr(12),,A
StringReplace,JSON,JSON, \\, % Chr(1),A ;watchout for escape sequence '\\', convert to '\1'
_JSON:="", in_str:=0, l_char:=""
Loop, Parse, JSON
{
if ( (!in_str) && (asc(A_LoopField)==0x20) )
continue
if( (asc(A_LoopField)==0x22) && (asc(l_char)!=0x5C) )
in_str := !in_str
_JSON .= (l_char:=A_LoopField)
}
StringReplace,_JSON,_JSON, % Chr(1),\\,A ;convert '\1' back to '\\'
return _JSON
}
}
JSON_Beautify(JSON, gap:="`t") {
;fork of http://pastebin.com/xB0fG9py
JSON:=JSON_Uglify(JSON)
StringReplace,JSON,JSON, \\, % Chr(1),A ;watchout for escape sequence '\\', convert to '\1'
indent:=""
if gap is number
{
i :=0
while (i < gap) {
indent .= " "
i+=1
}
} else {
indent := gap
}
_JSON:="", in_str:=0, k:=0, l_char:=""
Loop, Parse, JSON
{
if (!in_str) {
if ( (A_LoopField=="{") || (A_LoopField=="[") ) {
_s:=""
Loop % ++k
_s.=indent
_JSON .= A_LoopField "`n" _s
continue
}
else if ( (A_LoopField=="}") || (A_LoopField=="]") ) {
_s:=""
Loop % --k
_s.=indent
_JSON .= "`n" _s A_LoopField
continue
}
else if ( (A_LoopField==",") ) {
_s:=""
Loop % k
_s.=indent
_JSON .= A_LoopField "`n" _s
continue
}
}
if( (asc(A_LoopField)==0x22) && (asc(l_char)!=0x5C) )
in_str := !in_str
_JSON .= (l_char:=A_LoopField)
}
StringReplace,_JSON,_JSON, % Chr(1),\\,A ;convert '\1' back to '\\'
return _JSON
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment