Pass a variable that contains a Link on a real link

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Palamar
Posts: 161
Joined: Wed Apr 06, 2005 4:34 pm

Re: Pass a variable that contains a Link on a real link

Post by Palamar »

Gaev, you speak of for example what Whatsapp does that to use bold you have to put an asterisk before and after the word ?. It might be an option, I had not thought about it. Yes.
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Pass a variable that contains a Link on a real link

Post by Gaev »

Palamar:
Whatsapp does that to use bold you have to put an asterisk before and after the word ?. It might be an option, I had not thought about it.
Yes, that would be an optimal middle ground between "user friendly" and "easily recognized links".

And with this approach ...

Code: Select all

*WhateverTextIsHereIsConsideredAlinkValue*
e.g.
*http://www.abc.com?name=juan*
*https://juan.www.abc.com*
*juan.com.ar?age=101*
etc. etc.
Of course, you may choose a less conflicting separator string like ** or ??? or ++ or == ... or even braces like { and } ... anything that is not allowed in a valid link and is not used in normal text.

By the way, having a different starting and ending character (like { and } or ++ and **) will make it easier to write the logic.

If you need help with the logic, let me know what separator character(s) you wish to use.
Palamar
Posts: 161
Joined: Wed Apr 06, 2005 4:34 pm

Re: Pass a variable that contains a Link on a real link

Post by Palamar »

Could this be ** www.example.com **?
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Pass a variable that contains a Link on a real link

Post by Gaev »

Palamar:
Could this be ** http://www.example.com **?
To be sure ...

1) there is a space before AND after the link

2) is the question mark ? part of the link termination ... or just you asking the question :-)

So, the logic would be ...

1) parse the text for "** "

2) For each parsed element ...

a) parse the text for " **"
b) extract "{the link text}" from the first element and then compose HTML in the form ...

Code: Select all

<a href="{the link text}">{the link text}</a>
c) append rest of element to value composed in (b)
d) append value in (c) to "output text"

I will take a shot at this logic later tonight.
Palamar
Posts: 161
Joined: Wed Apr 06, 2005 4:34 pm

Re: Pass a variable that contains a Link on a real link

Post by Palamar »

Yes, there may be a space or not. If the link is from the beginning it will not have space, instead if it is in the middle yes.
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Pass a variable that contains a Link on a real link

Post by Gaev »

Palamar:

I worked on it ast night ... almost there ... just need to add commands to allow for some exception conditions ... hope to complete in a day.

However, as I began testing it, I ran into a problem with the "** " and " **" link wrapper patterns ... you see, when you have something like ...

Code: Select all

Please visit my website ** supermagic.com ** at your convenience
... then, when I do a StrParse on "** ", it considers "** at your convenience" as the start of another link :-((

So, I used "{" and "}" as the link wrappers e.g. ...

Code: Select all

Please visit my website {supermagic.com} at your convenience
... but you can change to other values (as long as they don't get confused with each other).
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Pass a variable that contains a Link on a real link

Post by Gaev »

Palamar:

Here is the requested coe ...

Code: Select all

...[TextInput1] is variable associated with Text Input Box where user enters text
SetVar [gkTextIn] [TextInput1]
...text pattern BEFORE a link
SetVar [linkStartPattern] "{{"
...text pattern AFTER a link
SetVar [linkEndPattern] "}}"     

StrLen "[linkEndPattern]" [linkEndPatternLen]

SetVar "[debugInfo]" "gkTextIn = [gkTextIn]"

... parse on linkStart delimiter
StrParse "[gkTextIn]" "[linkStartPattern]" [linkifyElement]

... first element
SetVar [gkHTMLOut] [linkifyElement(0)]
SetVar [debugInfo] "[debugInfo]<br/>linkifyElement(0) = [linkifyElement(0)]"
... get number of elements
ArrayLen [linkifyElement] [linkifyElementSize]
...SetVar [debugInfo] "[debugInfo]<br/>linkifyElementSize = [linkifyElementSize]"

SetVar [linkifyElementMax] [linkifyElementSize]-1
SetVar [debugInfo] "[debugInfo]<br/>linkifyElementMax = [linkifyElementMax]"
If [linkifyElementMax] >= 1
   ... loop through rest of elements
   Loop 1 "[linkifyElementMax]" [linkifyElementCounter]
        ... fetch current element
        SetVar [thisLinkifyElementText] [linkifyElement([linkifyElementCounter])]
        SetVar [debugInfo] "[debugInfo]<br/>linkifyElement([linkifyElementCounter]) = [thisLinkifyElementText]"
        StrLen "[thisLinkifyElementText]" [thisLinkifyElementTextLen]
        ... get position of [linkEndPattern]
        StrSearch "[linkEndPattern]" "[thisLinkifyElementText]" [linkEndPosition]
        If "[linkEndPosition]" == -1
           ... no [linkEndPattern]; treat everything as text
           SetVar "[thislinkifyElementHTML]" "[linkStartPattern][thisLinkifyElementText]"
           SetVar [debugInfo] "[debugInfo]<br/>thislinkifyElementHTML = [thislinkifyElementHTML]"

        Else
            SetVar [debugInfo] "[debugInfo]<br/>linkEndPosition = [linkEndPosition]"
            ... fetch [thisLinkifyElementPart1]
            StrCopy "[thisLinkifyElementText]" 0 [linkEndPosition] [thisLinkifyElementPart1]
            StrLen "[thisLinkifyElementPart1]" [thisLinkifyElementPart1Len]
            SetVar [debugInfo] "[debugInfo]<br/>thisLlinkifyElementPart1 = [thisLinkifyElementPart1]"
            ... fetch [thisLinkifyElementPart2]
            SetVar "[thisLinkifyElementPart2Start]" [linkEndPosition]+[linkEndPatternLen]
            SetVar [debugInfo] "[debugInfo]<br/>thisLlinkifyElementPart2Start = [thisLinkifyElementPart2Start]"
            SetVar "[thisLinkifyElementPart2Len]" [thisLinkifyElementTextLen]-[thisLinkifyElementPart2Start]
            SetVar [debugInfo] "[debugInfo]<br/>thisLlinkifyElementPart2Len = [thisLinkifyElementPart2Len]"
            StrCopy "[thisLinkifyElementText]" [thisLinkifyElementPart2Start] [thisLinkifyElementPart2Len] [thisLinkifyElementPart2]
            SetVar [debugInfo] "[debugInfo]<br/>thisLlinkifyElementPart2 = [thisLinkifyElementPart2]"
            ... to add http:// or not
            If [thisLinkifyElementPart1Len] < 4
               ... add http://
               ... SetVar "[thisLinkifyElementProtocol]" "http://"
               SetVar "[thisLinkifyElementHREF]" "http://[thisLinkifyElementPart1]"
            Else
                StrCopy "[thisLinkifyElementPart1]" 0 4 [thisLinkifyElementFirst4]
                If [thisLinkifyElementFirst4] != "http"
                   ... add http://
                   ... SetVar "[thisLinkifyElementProtocol]" "http://"
                    SetVar "[thisLinkifyElementHREF]" "http://[thisLinkifyElementPart1]"
                Else
                    ... is http/https
                    SetVar "[thisLinkifyElementHREF]" "[thisLinkifyElementPart1]"
                EndIf
            EndIf
            SetVar [debugInfo] "[debugInfo]<br/>thisLinkifyElementHREF = [thisLinkifyElementHREF]"
            ... compose HTML
            SetVar "[thislinkifyElementHTML]" "<a href='[thisLinkifyElementHREF]'>[thisLinkifyElementPart1]</a>[thisLinkifyElementPart2]"
            SetVar [debugInfo] "[debugInfo]<br/>thislinkifyElementHTML = [thislinkifyElementHTML]"
        EndIf
        ... add to [gkHTMLout]
        SetVar "[gkHTMLOut]" "[gkHTMLOut][thislinkifyElementHTML]"
        SetVar [debugInfo] "[debugInfo]<br/>gkHTMLOut = [gkHTMLOut]"
    EndLoop
EndIf
... show debugInfo
AlertBox "debugInfo" "[debugInfo]" ""
... show html in Container
BeginJS
//Container4 is the object where the resulting HTML is rendered
$('#Container4').html($rootScope.gkHTMLOut);
EndJS
Note that ...

1) I have lots of commands of the form ...

Code: Select all

SetVar [debugInfo] "[debugInfo]<br/>variableName = [variableName]"
... this helps in debugging unexpected results ... once you are done testing, just comment out the AlertBox command that displays the contents of [debugInfo].

2) If you prefer link wrappers other than {{ and }}, you can change the values for [linkStartPattern] and [linkEndPattern].

3) It assumes that ...

a) the variable associated with the Text Input Box is [Textinput1] ... if yours is different, change the first line of the code
b) the formated HTML is going to be displayed in a Container object named Container4 ... if yours is different, change the second last line of code (just before EndJS)

4) I have verified the following test cases ...

Code: Select all

{{abcd.com}}
{{abcd.com}} is worth visiting
Please visit my website at {{abcd.com}}
Please visit my websites at {{abcd.com}} and {{wxyz.edu}}
Please visit my websites at {{abcd.com}} and {{wxyz.edu}}too
Please visit my website at {{www.abcd.com}}
Please visit my website at {{http://www.abcd.com}}
Please visit my website at {{https://www.abcd.com}}
Please visit my website at {{notAValidURL}}
Please visit my website at {{abcd.com}
Please visit my websites at {{abcd.com} and {{wxyz.edu}}
... if you find any HTML not as expected, please let me know.

If there are test cases not addressed here, let me know as well.


Once you are satisfied, the code (which is generic) can be made into a subroutine that can be called from multiple events ... just have to populate ...

a) [gkTextIn] before calling the routine
b) copy the HTML from [gkHTMLOut] after the call.
Palamar
Posts: 161
Joined: Wed Apr 06, 2005 4:34 pm

Re: Pass a variable that contains a Link on a real link

Post by Palamar »

Hi Gaev:
What a beautiful routine! Working perfectly. Embedding without problem. Very useful the AlertBox to show the details of the operation of the routine. In addition I have served the fact that despite not having a link the text will show the same.

Thank you very much!
Locked