How to structure HTML text (tags and attributes) for processing in LiveCode?
Mark Schonewille
m.schonewille at economy-x-talk.com
Sun Jun 12 21:01:51 EDT 2011
/*** Formatting HTML code ***
indentHtml v.0.1.0, 13 June 2011
Adjust kTags to define tags that need indentation.
Parameters:
theHtml: any valid HTML source code;
theTabSpaces: the number of positions occupied by one tab character
theSoftWrapCol: the column (=position number) after which lines are wrapped
theReplaceTabs: replace tabs with spaces (true, false)
theDontBreakLinks: treat links as regular text, don't indent.
If you change kTags, you should be able to format any mark-up language as
long as it uses < and > to define tags.
By Mark Schonewille
Economy-x-Talk
Nijmegen, the Netherlands
http://economy-x-talk.com
For questions and comments, use the contact form on the website
*/
constant kTags = "html,body,head,title,a,b,i,font,h1,h2,h3,h4,h5,div,center,table,tr,td,th,form,select"
local myTagArray
function indentHtml theHtml,theTabSpaces,theSoftWrapCol,theReplaceTabs,theDontBreakLinks
replace cr with empty in theHtml
put 0 into myOldOffset
put 0 into myPreviousOldOffset
put 0 into myLevel
put empty into myTabs
repeat
put offset("<",theHtml,myOldOffset) into myNewOffset -- open tag
if myNewOffset is 0 then exit repeat else add myOldOffset to myNewOffset
put offset(">",theHtml,myNewOffset) into myOldOffset -- close tag
if myOldOffset is 0 then exit repeat else add myNewOffset to myOldOffset
put word 1 of (char myNewOffset+1 to myOldOffset-1 of theHtml) into myTag
put ((myTag is "a" or myTag is "/a") and theDontBreakLinks is true) into myCurrentTagIsLink
///if myTag is "/a" then breakpoint
put char myPreviousOldOffset+1 to myNewOffset-1 of theHtml into myData
if myData is not empty then
put theSoftWrapCol - theTabSpaces*(number of chars of myTabs+1) into myTempWrapCol
put 1 into myLineCounter
if myCurrentTagIsLink or (myPreviousTag is "/a" and theDontBreakLinks is true) then
// put offset("<",myData) into myTagPosition
repeat
if number of chars of line myLineCounter of myData > myTempWrapCol and "<" is not in \
char 1 to myTempWrapCol of line myLineCounter of myData then
put cr & myTabs before word (number of words of (char 1 to myTempWrapCol of line \
myLineCounter of myData) + 1) of line myLineCounter of myData
add 1 to myLineCounter
else
exit repeat
end if
end repeat
if myTag is not "/a" and myPreviousTag is not "/a" then
put myTabs before line 1 of myData
end if
else if theTabSpaces is a number and theTabSpaces > 0 and theSoftWrapCol is a number and \
theSoftWrapCol > 0 then
repeat
if number of chars of line myLineCounter of myData > myTempWrapCol then
put cr before word (number of words of (char 1 to myTempWrapCol of line \
myLineCounter of myData) + 1) of line myLineCounter of myData
add 1 to myLineCounter
else
exit repeat
end if
put myTabs before line myLineCounter-1 of myData
end repeat
if last char of myData is cr then delete last char of myData
put myTabs before the last line of myData
end if
if myCurrentTagIsLink then
put myData after myNewData
else
put myData & cr after myNewData
end if
end if
if (char 1 of myTag is slash and char 2 to -1 of myTag is among the items of kTags) then
subtract 1 from myLevel
put empty into myTabs
repeat myLevel
put tab after myTabs
end repeat
end if
if myLevel < 0 then put 0 into myLevel
if myCurrentTagIsLink then
put char myNewOffset to myOldOffset of theHtml after myNewData
else
put myTabs & char myNewOffset to myOldOffset of theHtml & cr after myNewData
end if
put myOldOffset into myPreviousOldOffset
// if opening tag then after
if myTag is among the items of kTags then
add 1 to myLevel
put empty into myTabs
repeat myLevel
put tab after myTabs
end repeat
end if
put myTag into myPreviousTag
end repeat
put myTabs & char myOldOffset+1 to -1 of theHtml after myNewData
if theTabSpaces is a number and theTabSpaces > 0 and theReplaceTabs is true then
put empty into mySpaces
repeat theTabSpaces
put space after mySpaces
end repeat
replace tab with mySpaces in myNewData
end if
return myNewData
end indentHtml
/*
--
Best regards,
Mark Schonewille
Economy-x-Talk Consulting and Software Engineering
Homepage: http://economy-x-talk.com
Twitter: http://twitter.com/xtalkprogrammer
KvK: 50277553
New: Download the Installer Maker Plugin 1.6 for LiveCode here http://qery.us/ce
*/
More information about the use-livecode
mailing list