Feature request: merge function!

David Bovill david.bovill at gmail.com
Fri Jan 18 09:52:24 EST 2019


That would be great Mark :)

Andre the general function I use for munging text uses regular expressions
as my head exploded long ago with too many custom string munging functions
:)

*command* fedwiki_MergeCurlyArray @templateArray, itemID, curlyArray
>
*put* fedwiki_GetStoryItemText (templateArray, itemID) into itemText
>
text_MergeCurly itemText, curlyArray

fedwiki_SetStoryItemText templateArray, itemID, itemText

*end* fedwiki_MergeCurlyArray
>


> *command* text_MergeCurly @someText, curlyArray
>
*repeat* for each key curlyLabel in curlyArray
>
*put* curlyArray [curlyLabel] into curlyReplacement
>
*put* "(\{\{\s*" & curlyLabel & "\s*\}\})" into someReg
>
text_StripReg someText, someReg, curlyReplacement

*end*
> *repeat*

*end* text_MergeCurly


*command* text_StripReg @wikiText, someReg, pReplaceText
>
*local* refStart, refEnd
>
*put* 0 into indexNum
>
*repeat*

*get* matchchunk (wikiText, someReg, sNum, eNum)
>
*if* sNum is not a number
> *then*

*put* the result into testResult
>
*-- breakpoint*

*exit* *repeat*
> *-- regExp bug???*

*end*
> *if*

*if* it is true
> *then*

*-- this bit not needed (remove for speed)*

*-- put char refStart to refEnd of wikiText into someTest*

*put* sNum into stripResultArray [indexNum]["sNum"]
>
*put* eNum into stripResultArray [indexNum]["eNum"]
>
*add* 1 to indexNum
>
*--*

*-- delete char sNum to eNum of wikiText*

*put* pReplaceText into char sNum to eNum of wikiText
>
*else*

*exit*
> *repeat*

*end*
> *if*

*end*
> *repeat*

*return* stripResultArray
>
*end* text_StripReg


I'm sure it could be greatly improved. What I like about the regExp
approach is the portability between languages. It also allows the slow
building up of quality regExp for specific purposes without constantly
changing the underlying code. I'm sure there are faster and leaner
approaches though.

An extension to merge() along the lines that Mark suggested would cut down
on quite a lot of the use-cases i've had over the years - and naturally if
would be faster than doing it in script.


On Fri, 18 Jan 2019 at 12:53, Mark Waddingham via use-livecode <
use-livecode at lists.runrev.com> wrote:

> On 2019-01-18 09:01, David Bovill via use-livecode wrote:
> > I would love to be able to change the characters that merge uses -
> > especially from “[[..]]” to “{{...}}”.
> >
> > Quite a lot of templating uses curly brackets - and I especially want
> > to
> > use it for wiki style templates which can’t use square brackets as they
> > are
> > used for internal links.
> >
> > Not sure of the most elegant syntax, but we could add some Paramus
> > without
> > breaking anything.
>
> The current string scanning which the merge function relies on 5
> codeunits (not characters!):
>
>    - expression start ('[')
>    - expression finish (']')
>    - script start ('<')
>    - script finish ('>')
>    - script marker ('?')
>
> With constraints:
>    - expression start != expression end
>    - script start != script finish != script marker
>    - expression start != script start
>    - expression finish != script finish
>
> So the easiest / simplest least-likelihood-of-breaking-anything approach
> would be to add an optional parameterization to the merge function
> consisting of a string between 2 and 5 codeunits:
>
>    - string of 2 codeunits: change expression start / finish, leave
> script markers the same
>    - string of 3 codeunits: change script start / finish / marker, leave
> expression markers the same
>    - string of 4 codeunits: change expression start / finish and script
> start / finish, leave script marker the same
>    - string of 5 codeunits: change all characters
>
> In all cases it would need to be an error if the above constraints don't
> hold.
>
> For example...
>
> To get David's wiki-safe option you'd just parameterize by "{}".
>
> Similarly, merge is not completely XML safe - <?xml version="1.0"> is a
> valid XML directive *and* a value LCS command call (xml version = "1.0")
> - so to make that better you could parameterize by "<>|" (<| not being a
> valid sequence except in CDATA, which you can elect not to use by entity
> escaping chars instead).
>
> Warmest Regards,
>
> Mark.
>
> --
> Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
> LiveCode: Everyone can create apps
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list