cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
pmroz
Super User

Hyperlinks in Text Box or Text Edit Box?

Is there a way to insert a hyperlink into a text box or text edit box?  I know that you can do this in a JMP table with the expression column type, and that you can create a button box in a window that acts like a hyperlink.  I'm also aware of the << markup syntax for text boxes, but that only lets you do bold, underline and italic.

12 REPLIES 12
pmroz
Super User

Re: Hyperlinks in Text Box or Text Edit Box?

Thanks for that suggestion Mark.  However I don't want my users to have to embed tags for URLs.  My boss came up with a regular expression which works pretty well:

// Sample text
text = "Periarthritis is defined as: Inflammation of the tissues around a joint. " ||
"http://www.reference.md/files/D010/mD010489.html  As arthralgia, myalgia, tendonitis " ||
"and other musculoskeletal issues are listed events, periarthritis can be considered listed as well. 
Comment: no good sentinel case — not a new signal; one case also reported somnolence and " ||
"another case reported “yawny” which could mean sleepy *http://www.ncbi.nlm.nih.gov/pubmed/12530994";
url_regex = "\b(https?://){0,}(\w+\.){2,}\w+/?(\w+/?){0,}(\.\w+){0,1}/?\b";
text_list = {};
link_list = {};
keep_going = 1;
istart     = 1;
text_len   = length(text);
i          = 0;
while(keep_going,
	new_text = substr(text, istart);
	link_text = regex(new_text, url_regex);
	if (!is missing(link_text),
	// then
		i++;
		start_pos    = contains(text, link_text, istart);
		text_list[i] = substr(text, istart, (start_pos - istart - 1));
		link_list[i] = link_text;
		istart       = start_pos + length(link_text);
		,
	// else no link found
		i++;
		text_list[i] = new_text;
		keep_going = 0;
	);
);
tb = {};
lb = {};
nt = nitems(text_list);
nw = new window("Test link",
	vb = vlistbox(
	)
);
for(i = 1, i <= nt, i++,
	tb[i] = text box(text_list[i], << set width(400));
	vb << append(tb[i]);
	if (i < nt,
		one_button = evalinsert("\[lb[i] = button box("^link_list[i]^", web("^link_list[i]^"), << underline style(1) )]\");
		eval(parse(one_button));
		vb << append(lb[i]);
	);
);
ih
Super User (Alumni) ih
Super User (Alumni)

Re: Hyperlinks in Text Box or Text Edit Box?

Combining @Byron_JMP's Web Browser Box idea with your regex string you could save a formatted html file with embedded links and display that in your window.  Suddenly you have a lot of control over the text formatting; you could even let users optionally add a friendly name to links syntax like [JMP Website]http://www.jmp.com, similar to some markup syntax.

 

Web Browser Box.PNG

 

Names Default To Here( 1 );

text = "Periarthritis is defined as: Inflammation of the tissues around a joint. " ||
"[A Link]http://www.reference.md/files/D010/mD010489.html  As arthralgia, myalgia, tendonitis " ||
"and other musculoskeletal issues are listed events, periarthritis can be considered listed as well. 
Comment: no good sentinel case — not a new signal; one case also reported somnolence and " ||
"another case reported “yawny” which could mean sleepy *http://www.ncbi.nlm.nih.gov/pubmed/12530994";
t = text;
//wrap links in a string with <a href> tags
findlinks = function({t, recct=0}, {url_regex, rgxmatch = {}, rtn=""}, 
	recct++; 
	if(recct > 100, 
		throw("Recursion to deep: " || char(recct) || " levels.  Still need to parse: '" || t || "'"));
	url_regex = "^(.*?)(\!\[(.+)\])?((https?://){0,}(\w+\.){2,}\w+/?(\w+/?){0,}(\.\w+){0,1}/?)(\b.*)$";
	
	rgxmatch = Regex Match(t, url_regex);
	if( N Items(rgxmatch) == 0, 
		rtn = t, //No link
		if( N Items(rgxmatch) == 10,  //found a link 
			rtn = rgxmatch[2] || 
				"<a href='" || rgxmatch[5] || "'>" || if(is missing(rgxmatch[4]), rgxmatch[5], rgxmatch[4] ) || "</a>" ||
				findlinks(rgxmatch[10], recct),
			throw("Error matching regex in: '" || t || "'")
		)
	);
	return(rtn)
);

//Create a simple html page from a block of text, save it to the temp files, and return the filename
texttohtmlfile = function({t},
	Save text file("$TEMP/Temp HTML file.html", "<html><body>" || findlinks(t) || "</body></html>")
);

//Make a temp html file
fn = texttohtmlfile(text);

//Simple window to display html file, deletes the temp file when it closes
w = New Window("Example",wb = Web Browser Box());
w << On Close( Delete File( fn ) );
wb << Set Size(1000, 200);
wb << Set Auto Stretching( 1, 1 );
wb << Set Max Size( 1000, 1000 );

//Show the html file in the window
wb << Navigate( fn );

Note the unfortunate slash bang in the regex match expression as "\[" begins an 'escaped character' section.

 

Edited to add screenshot and again to fix a typo.

pmroz
Super User

Re: Hyperlinks in Text Box or Text Edit Box?

Very cool approach @ih