Microsoft Small Basic

Program Listing: TXK231
' User Ed Article List 0.1
' Copyright © 2014-2015 Nonki Takahashi. The MIT License.
' Last update 2015-07-27
'
TextWindow.Title = "User Ed Blog Article List 0.1"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
WQ = Text.GetCharacter(34)
LT = "<"
Not = "False=True;True=False;"
site = "http://blogs.msdn.com"
outfile = Program.Directory + "\UserEdArticleList.html"
folder = "/b/user_ed/archive/tags/small+basic/default.aspx?PageIndex="
nPosts = 0
maxPage = 1
outbuf = LT + "html>" + CRLF + LT + "body>" + CRLF + LT + "ul>" + CRLF
block = ""
For page = 1 To maxPage
buf = Network.GetWebPageContents(site + folder + page)
p = 1
eod = "False"
While Not[eod]
param = "tag=h4;class=post-name;"
FindTag()
If tag = "" Then
eod = "True"
Else
Stack.PushValue("local", buf)
Stack.PushValue("local", p)
buf = tag
p = 1
param = "tag=a;"
FindTag()
p = Stack.PopValue("local")
buf = Stack.PopValue("local")
GetAttrAndText()
ConvertText()
block = block + LT + "li>"
block = block + txt + LT + "/a>" + CRLF
param = "tag=span;class=value;"
FindTag()
If tag = "" Then
eod = "True"
Else
GetAttrAndText()
block = block + "(" + txt + ")" + LT +"/li>" + CRLF
EndIf
nArticle = nArticle + 1
If Math.Remainder(nArticle, 4) = 0 Then
TextWindow.Write(block + CRLF + nArticle + " articles")
outbuf = outbuf + block
block = ""
Program.Delay(700)
EndIf
EndIf
block = block + CRLF
EndWhile
If page = 1 Then
param = "tag=a;class=last;"
FindTag()
If tag = "" Then
param = "tag=a;class=page;"
tag = LT + "a href=" + WQ + "=1" + WQ + ">"
While tag <> ""
lastTag = tag
FindTag()
EndWhile
tag = lastTag
EndIf
GetAttrAndText()
maxPage = Text.GetSubTextToEnd(attr["href"], Text.GetIndexOf(attr["href"], "=") + 1)
EndIf
EndFor
block = block + CRLF + LT + "/ul>" + CRLF
block = block + LT + "/body>" + CRLF
block = block + nArticle + " articles" + CRLF + LT + "/html>" + CRLF
If Math.Remainder(nArticle, 4) = 0 Then
TextWindow.WriteLine(CRLF)
Else
TextWindow.WriteLine(block)
EndIf
outbuf = outbuf + block
' The following line could be harmful and has been automatically commented.
' File.WriteContents(outfile, outbuf)
Sub ConvertText
' convert &*; to unicode character
' param txt
' return txt
While Text.IsSubText(txt, "&")
c = Text.GetIndexOf(txt, "&")
l = Text.GetIndexOf(Text.GetSubTextToEnd(txt, c), ";")
kw = Text.GetSubText(txt, c + 1, l - 2)
If Text.StartsWith(kw, "#") Then
txtMid = Text.GetCharacter(Text.GetSubTextToEnd(kw, 2))
ElseIf kw = "quot" Then
txtMid = Text.GetCharacter(34)
Else
txtMid = ""
EndIf
txtLeft = Text.GetSubText(txt, 1, c - 1)
txtRight = Text.GetSubTextToEnd(txt, c + l)
txt = Text.Append(txtLeft, Text.Append(txtMid, txtRight))
EndWhile
EndSub
Sub FindTag
' find tag from html buffer
' param["tag"] - tag name
' param["class"] - class name
' param p - pointer for buffer
' param buf - html buffer
' return tag - found tag
pSave = p
tag = ""
findNext = "True"
While findNext
findNext = "False" ' tag may be not found
pTag = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), "<" + param["tag"])
If 0 < pTag Then
lTag = Text.GetLength(param["tag"]) + 1
pTag = p + pTag - 1
len = Text.GetIndexOf(Text.GetSubTextToEnd(buf, pTag), "/" + param["tag"] + ">")
If param["class"] = "" Then
len = len + lTag
tag = Text.GetSubText(buf, pTag, len)
findNext = "False" ' found the tag
ElseIf 0 < len Then
findNext = "True" ' tag may have different class
len = len + lTag
attr = "class=" + WQ + param["class"] + WQ
pAttr = pTag + lTag + 1
lAttr = Text.GetLength(attr)
If Text.GetSubText(buf, pAttr, lAttr) = attr Then
tag = Text.GetSubText(buf, pTag, len)
findNext = "False" ' found the tag
EndIf
p = pTag + len
EndIf
EndIf
EndWhile
If tag = "" Then
p = pSave
EndIf
EndSub
Sub GetAttrAndText
' get attributes and text from given tag
' param tag - given tag
' return attr[] - array of attributes in the tag
' return txt - text in the tag
pTag = Text.GetIndexOf(tag, " ") + 1
pEnd = Text.GetIndexOf(tag, ">")
attr = ""
While pTag <= pEnd
pEq = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), "=")
If 0 < pEq Then
pEq = pTag + pEq - 1
If Text.GetSubText(tag, pEq + 1, 1) = WQ Then
pWQ = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pEq + 2), WQ)
If 0 < pWQ Then
pWQ = pEq + 2 + pWQ - 1
attr[Text.GetSubText(tag, pTag, pEq - pTag)] = Text.GetSubText(tag, pEq + 2, pWQ - pEq - 2)
pTag = pWQ + 2
EndIf
EndIf
Else
pTag = pEnd + 1
EndIf
EndWhile
len = Text.GetLength(tag)
txt = ""
While pTag <= len
pL = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), "<")
If pL = 0 Then
txt = Text.Append(txt, Text.GetSubTextToEnd(tag, pTag))
pTag = len + 1
Else
pL = pTag + pL - 1
txt = Text.Append(txt, Text.GetSubText(tag, pTag, pL - pTag))
pR = Text.GetIndexOf(Text.GetSubTextToEnd(tag, pTag), ">")
If 0 < pR Then
pTag = pTag + pR
Else
pTag = len + 1
EndIf
EndIf
EndWhile
EndSub