<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
		<meta name="generator" content="Adobe GoLive 6"/>
		<title>URLize Text Sample Extension</title>
	</head>

	<body>
		<hr/>
		<p>ADOBE SYSTEMS INCORPORATED</p>
		<p>Copyright 2000-2002 Adobe Systems Incorporated<br/>
			All Rights Reserved</p>
		<p>NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms of the Adobe license agreement accompanying it. If you have received this file from a source other than Adobe, then your use, modification, or distribution of it requires the prior written permission of Adobe.</p>
		<hr/>
		<p><b>Extension Name:</b> URLize</p>
		<p><b>Folder name:</b> URLize</p>
		<p>Menu: Extensions &gt; URLize Text</p>
		<hr/>
		<p><b>Description:</b></p>
		<p>Adds a menu with two items which will convert a list of text items in email, url, or ftp format into links. For example: <code>http://www.adobe.com</code></p>
		<p>is converted to:</p>
		<blockquote>
			<p>&lt;a href=&quot;http://www.adobe.com&quot;&gt;http://www.adobe.com&lt;/a&gt;</p>
		</blockquote>
		<p>And <code>someone@adobe.com</code></p>
		<p>is converted to:</p>
		<blockquote>
			<p>&lt;a href=&quot;mailto:someone@adobe.com&quot;&gt;someone@adobe.com&lt;/a&gt;</p>
		</blockquote>
		<p>URLize text &gt; URLizeOne converts one selected URL or email text to a link</p>
		<p>URLize text &gt; URLizePage converts all URL and email text on the page to links</p>
		<hr/>
		<p><b>Module Definition:</b></p>
		<jsxmodule debug info="$$$/extendscript/urlize/info=This extension converts a list of text items in email, url, or ftp format into links." name="URLize" version="1.0"/> 
		<p><b>Scripts:</b></p>
		


		<script><![CDATA[
//--------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------

// define global variables here


//--------------------------------------------------------------------
// CALLBACK FUNCTIONS
//--------------------------------------------------------------------


//---------------------------------------------------------
// menuSetup - called before displaying a menu item element 
//   with the dynamic attribute
//
// Description: Allows enabling/disabling of a check mark or 
//   menu item.

function menuSetup(menuItem)		
{
	// check to see if a document is open
	if (document == undefined)
	{
		menuItem.enabled = false ;
		return;
	}
	
	// check to see if it is an html page
	if (document.element == undefined)
	{
		menuItem.enabled = false;
		return;
	}
	
	// if a document is open and it's a page then enable the menu
	menuItem.enabled = true;
}	


//---------------------------------------------------------
// menuSignal - called when the user chooses a menu item.
//
// Description: Use it to respond to the user's choice of menu item.

function menuSignal(menuItem) 		
{
	switch (menuItem.name) 
	{
	case "URLizePage":	
		URLizePage();
		break;
	case "URLizeOne":		
		URLizeOne();
		break;
 	default:				
 		alert("No such menu Item.");
	}
}


//--------------------------------------------------------------------
// add your own functions here
//--------------------------------------------------------------------

//--------------------------------------------------------------------
// URLizeOne - 
// process a selection
// howMany is "one" or "many" which is used to control 
// an alert in URLizeTextElement()

function URLizeOne()
{
    //make frontmost window display layout view
    document.view = "layout";
    
	switch (document.selection.type)
	{
	case "full":	
	case "part":
		var el = document.selection.element;
		URLizeSelection(el);
		break;
	case "none":	
		alert("You haven't made a selection.");
		break;
	case "point":	
		alert("You haven't made a selection.");
		break;
	case "complex":	
		alert("You've selected more than one element");
		break;
	case "outside":	
		alert("You've done the impossible");
		break;
	default:		
		alert("Programmer error in case statement");
	}
}


//--------------------------------------------------------------------
// URLizePage -
// process the entire page

function URLizePage()
{
	if (document) {
		// do main text area
		var ta = document.mainTextArea;
		processTextArea(ta);
	}	
	processTables();
}


//--------------------------------------------------------------------
// findAndCreateLink -
// using a regular expression, find the url link starting at pos.
//
// ta: textArea
// pos: position in textArea. If beginning of textArea, set to -1 before calling.
// returns: pos reset to position after found string

function findAndCreateLink(ta, pos)
{
	// the default boxPlaceHolder is -, which is allowed in our email 
	// and url strings. I picked a space since this not allowed in urlRegExp.
	ta.boxPlaceHolder = " ";
	
	var urlRegExp = new RegExp('(ftp:\/\/\\S+)|(http:\/\/\\S+)|([-_0-9a-zA-Z]+([._][-_0-9a-zA-Z]+)*@\\S+)');
				
	pos = ta.findString(urlRegExp, pos+1, false);
	if (pos > -1) {
		// we have a match
		// select the URL or email address
		var urlLength = RegExp.lastMatch.length;
		ta.select(pos, urlLength);
		
		if (ta.hasStyle("a") == "no") {	
			// at this point we have a selection not wrapped in <a>
			var url = RegExp.lastMatch;
	
			if (RegExp.$3) {
				// change url if this is an email address, $3					
				url = 'mailto:' + url;
			}
			// we have found either a URL or email address	
			if (pos == ta.selectionStart && urlLength == ta.selectionLength) {
				// at this point the url or email is the selection
				ta.applyStyle('a href="' + url + '"');		
			}		
		} // end if (ta.hasStyle)
		pos += urlLength;
	} // end if (pos > -1)
	return pos;
}
	
//--------------------------------------------------------------------
// processTables -
// process textArea objects in all tables in document
	
function processTables()
{
	if (document)
	{
		// do tables 
		for (var i = 0; i < document.element.getSubElementCount('table'); i++) {
			// get the markup object of the table
			var telement = document.element.getSubElement('table', i);

			// if it's a table, get the tablelayout object
			table = telement.layout.table;
			if( table != null ) {
				// caption of table
				if (table.captionPosition != "none") {
					processTextArea(table.captionCell.textArea);
				}

				// each cell of table
				for (var j = 0; j < table.cell.length; j++) {
					processTextArea(table.cell[j].textArea);
				}
			}
			else {
				// must be a grid, but make sure
				if (telement.cool != undefined) {
					for (var k = 0; k < telement.getSubElementCount("td"); k++) {
						tdelement = telement.getSubElement( "td", k );
						if( tdelement.content != undefined ) {
							processTextArea(tdelement.textArea);
						}
					}
				}
			}
		}
	}
}

//--------------------------------------------------------------------
// processTextArea -
// call findAndCreateLink on whole textArea
// ta: textArea

function processTextArea(ta) 
{
	if (ta) {	
		var pos = -1;
		do {
			pos = findAndCreateLink(ta, pos);
		}
		while (pos > -1);
	} 
}
	
//--------------------------------------------------------------------
// URLizeSelection -
// process current selection
//
// el: currently selected element.
	
function URLizeSelection(el)
{
	if (el) {
		var ta = el.textArea;
		findAndCreateLink(ta, ta.selectionStart - 1);
	}
}
//]]>
</script>
		<hr/>
		<p><b>User Interface Tags:</b></p>
		<p><b>JSXLocale</b> Specify localized versions of strings used in menu items, dialogs, alerts, etc.</p>
		<jsxlocale>
			<table width="180" border="1" cellspacing="2" cellpadding="0">
				<tr>
					<th>US</th>
					<th>DE</th>
					<th>FR</th>
				</tr>
				<tr>
					<td></td>
					<td></td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td></td>
					<td></td>
				</tr>
			</table>
		</jsxlocale>
		<p><b>Menu Bar</b> Add items and menus to the GoLive menu bar.</p>
		<jsxmenubar>
			<jsxmenu name="Extensions" title="Extensions">
				<jsxmenu title="URLize text">
					<jsxitem dynamic="dynamic" name="URLizeOne" title="$$$/extendscript/urlize/convertsSelected=Convert a selected URL or email text to a link"/>
					<jsxitem dynamic="dynamic" name="URLizePage" title="$$$/extendscript/urlize/info=Convert all URL and email text to links"/>
				</jsxmenu>
			</jsxmenu>
		</jsxmenubar>
		<p></p>
	</body>

</html>