Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
Is there any command to get lines inside a braces?
I need to get the lines inside curly braces. The lines may be 1,2....100 (it may be a paragraph in braces). Sometimes brace will with in braces.
I wish to create XML from Latex code.
For example:-
\good{This
is \bad{
an} example
to show
you}
i need :-
<good>This is <bad>an</bad> example to show you</good>
It is straightforward, but tedious, to deconstruct your example text into the format you want. But unless that original format is fixed, each instance would have to be handled individually. For example, just to begin, with your starting text in field 1, and a button with this in its script:
on mouseUp
get fld 1
replace "\" with "<" in it
replace return with space in it
set the itemDel to "{"
put item 1 of it & ">" into temp
put temp after accum
put item 2 of it & ">" after accum
answer accum
end mouseUp
You get the start of your desired output. But the deconstruction is intimately tied to the exact formatting of that text, and any variation will break a general solution. So the question again is this: will the original formatting always be the same? That is, will all the tags be arranged in the same order? I doubt it, no?
All the tags will not be in the same order.
I know its very hard. but i have an idea,
Start from the bottom of the field (last word to first word), so i get the first from last open brace ("{"), and there must be a closing brace next after some words in the field.
put the number of words of field "Field" into p
repeat with i = p down to 1
if the word i of field "Field" contains "{" then
--put replaceText(word i of fld "Field","\\[A-z,a-z]*(?ms)\{[^\}]+\}","good") into word i fld "Field"
put replaceText(word i of fld "Field","(\\[A-z,a-z]*)((?ms)\{[^\}]+\})","\<\1\>\2\<\\\1\>") into word i fld "Field"
// i know this regularexpression will not work, i just want to show my requirement
exit repeat
end if
end repeat
on mouseUp
lock screen
put the number of words of field "Field" into p
repeat with i = p down to 1
put 0 into tSkip
repeat
get wordOffset("}",field "Field",tSkip)
if it = 0 then exit repeat
add it to tSkip
end repeat
if matchText( the word i to p of field "Field", "(?ms)\\([A-Z,a-z]*)(?ms)\{([^\}]+)\}", tag,theValue) then
put "<"&tag&">" &theValue&"</"&tag&">" into the word i to tSkip of field "Field"
end if
end repeat
unlock screen
end mouseUp
But it is too slow. but it may be fast by using function
.
Can anybody help me to change this code to function
function nottex textbuffer
-- walk along string
-- if char is "\" then we start a tag block
-- add a tag to output stream and tack a tag on end
-- if char is "{" we start a text block
-- add char to output stream
-- if char is "}" we are at end of text block
-- add closing version of last tag
put number of chars of textbuffer into stringln
put 0 into depth
put empty into newstring
repeat with x = 1 to stringln
put first char of textbuffer into achar
delete first char of textbuffer --don't need it anymore
switch
case achar = "\"
--build tag
add 1 to depth
put "<" after newstring
put "</" into endtag[depth]
put true into tag
break
case achar = "{"
--close current tag and begin string, save tag's close and depth
put ">" after newstring
put ">" after endtag[depth]
put false into tag
break
case achar = "}"
--need to add this tag's close for this level
put endtag[depth] after newstring
subtract 1 from depth
break
default
put achar after newstring --build new string
if tag then put achar after endtag[depth] --construct closing tag for current level
end switch
end repeat
return newstring
end nottex
on withRegex
local newText
put the text of field "fSource" into TexSource
repeat for each line aLine in TexSource
if matchText( aLine, "\\([^{]+){([^}]+)}", theTag, theValue) then
put format( "<%s>%s</%s>\n", theTag, theValue, theTag) after newText
else
-- Take care here if no match or empty lines
put aLine &cr after newText
end if
end repeat
put newText into field "fResult"
end withRegex
Have fun,
Thierry
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Thierry's code works fine.
very fine.
0.042 secs on my Mac for the sample stack text supplied.
As I said, my code was for the more complex example given in the OP where the string had nested tags.
I thought it may be possible to do this with REGEX as I have seen examples of nested parsing used in a couple of language parsing modules for C I think.
However my REGEX is not good enough to follow them and I am not even sure if LC's REGEX could manage.
The REGEX solution provided here however does not handle the original nested example.
But for the simpler data set which appears to be what you are working with, I'd go with Thierry's code.
jameshale wrote:Thierry's code works fine.
very fine.
0.042 secs on my Mac for the sample stack text supplied.
As I said, my code was for the more complex example given in the OP where the string had nested tags.
I thought it may be possible to do this with REGEX as I have seen examples of nested parsing used in a couple of language parsing modules for C I think.
However my REGEX is not good enough to follow them and I am not even sure if LC's REGEX could manage.
The REGEX solution provided here however does not handle the original nested example.
But for the simpler data set which appears to be what you are working with, I'd go with Thierry's code.
Thanks James.
Did a quick comparison with the results of your solution and mine,
and got the same results except for extra spaces at the end of some lines which are left aside in my case.
Don't know what to do with these extra spaces, but it's easy to put them back in the resultsText if needed.
I write this code in a couple of minutes, based on the sample datas of the stack.
I was not aware of the nested example (not in the stack).
It can be done but need more free time...
Kind regards,
Thierry
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!