Data Translators

with a letter and can contain only alphanumeric characters, hyphens (-), and underscores (_). • title describes the translator in no more than 40 characters.
242KB taille 3 téléchargements 266 vues
17

CHAPTER 17

Data Translators

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

What is a data translator? Data translators translate specialized markup—such as server-side includes, conditional JavaScript statements, or other non-HTML code such as ePerl, PHP3, JSP, CFML, or ASP—into HTML code that can be read and displayed by Dreamweaver. In Dreamweaver 3 you can translate attributes within tags as well as entire tags or blocks of code. Translated tags or blocks of code must be enclosed in locked regions to preserve the original markup. Translated attributes do not require locks, which makes inspecting the tags that contain them a simpler process. All data translators—block/tag or attribute—are HTML files. The HEAD of a data translator contains two or more JavaScript functions: one that specifies what the translator does and the files it acts on, and one that performs the actual translation and any number of supporting functions. The BODY of a data translator is empty. Data translation—especially for entire tags or blocks of code—might involve complex operations that either cannot be done with JavaScript or that would be more efficiently done with C. If you are familiar with C or C++, you should also read “C-Level Extensibility” on page 221

171

How data translators work Dreamweaver handles all translator files the same way, regardless of whether they translate entire tags or only attributes. At startup, Dreamweaver reads all the files in the Configuration/Translators folder and calls the getTranslatorInfo() function to obtain information about the translator. Dreamweaver ignores any file in which getTranslatorInfo() does not exist or contains an error that causes it to be undefined. Note: To prevent JavaScript errors from interfering with startup, errors in any translator file are reported only after all translators are loaded. For more information on debugging translators, see “Finding bugs in your translator” on page 195.

Dreamweaver also calls the translateMarkup() function in all applicable translator files (as specified in the Translation preferences) whenever the user may have added new—or changed existing—content that needs translation. Dreamweaver therefore calls translateMarkup() when the user:

• Opens a file in Dreamweaver. • Switches back to the Document window after making changes in the HTML panel.

• Changes the properties of an object in the current document. • Inserts an object (using either the Objects panel or the Insert menu). • Refreshes the current document after making changes to it in another application.

• • • • • • • • •

172

Chapter 17

Applies a template to the document. Pastes or drags content into or within the Document window. Saves changes to a dependent file. Invokes a command, behavior, property inspector, or other extension that sets the innerHTML or outerHTML property of any tag object or the data property of any comment object. Chooses File > Convert > 3.0 Browser Compatible. Chooses Modify > Layout Mode > Convert Tables to Layers. Chooses Modify > Layout Mode > Convert Layers to Tables. Chooses Modify > Translate > translatorName. Changes a tag or attribute in the Quick Tag Editor and presses Tab or Enter.

The data translator API There are three custom functions in the data translator API. The functions in the data translator API differ from the functions in the main JavaScript API in three ways:

• They are not methods of the dreamweaver, dom, or site object. • They are significant only in the context of data translator files. That is, Dreamweaver automatically calls the translateMarkup() function if it is defined in a data translator file, whereas in any other extension file a function named translateMarkup() acts like a user-defined function—you have to call it explicitly.

• You are responsible for writing the body of each function and returning a value, if required. This is the opposite of how the functions in the main API work: those you call and pass arguments to, and Dreamweaver generates return values, if any. Here Dreamweaver calls the functions and passes arguments to them, and you generate return values, if any. getTranslatorInfo() Description

Provides information about the translator and the files it can act upon. Arguments

None.

Data Translators

173

Returns

An array of strings. The elements of the array must appear in the following order:



translatorClass uniquely identifies the translator. This string must begin with a letter and can contain only alphanumeric characters, hyphens (-), and underscores (_).



title



nExtensions specifies the number of file extensions to follow. If nExtensions is 0, the translator can run on any file. If nExtensions is 0, nRegExps is the

describes the translator in no more than 40 characters. This string appears in the Modify > Translate menu.

next element in the array.

174

Chapter 17



extension specifies a file extension (for example, "htm" or "SHTML") that this translator can work with. This string is case insensitive and should not contain a leading period. The array should contain the same number of extension elements as are specified in nExtensions.



nRegExps nRegExps



regExps specifies a regular expression to check for. The array should contain the same number of regExps elements as are specified in nRegExps, and at least one of the regExps must match a piece of the document’s HTML source code before the translator can act on a file.



runDefault specifies the default preference for running this translator. Possible values are "allFiles", "noFiles", "byExtension", and "byExpression". If you set runDefault to "byExtension" but do not specify any extensions (see extension, above), the effect is the same as setting "allFiles". If you set runDefault to "byExpression" but do not specify any expressions (see regExps, above), the effect is the same as setting "noFiles".



priority

specifies the number of regular expressions that will follow. If is 0, runDefault is the next element in the array.

specifies the default priority for running this translator. The priority is a number between 0 and 100. If you do not specify a priority, the default priority will be 100. 0 is highest priority and 100 is lowest. When multiple translators apply to a document, this setting controls the order in which the translators are applied. The highest priority is applied first. When multiple translators have the same priority, then they are applied in alphabetical order by translatorClass.

Example

The following instance of getTranslatorInfo() gives information about a translator for server-side includes: function getTranslatorInfo(){ var transArray = new Array(11); transArray[0] = "SSI"; transArray[1] = "Server-Side Includes"; transArray[2] = "4"; transArray[3] = "htm"; transArray[4] = "stm"; transArray[5] = "html"; transArray[6] = "shtml"; transArray[7] = "2"; transArray[8] = ":

[home] [products] [services] [support] [about us] [help]

A simple block/tag translator example To understand translation a little better, it’s helpful to look at a translator that is written entirely in JavaScript (that is, one that does not rely on a C library for any functionality). The following translator would be more efficient if written in C, but the JavaScript version is simpler—which makes it perfect for demonstrating how translators work.

Data Translators

185

Like most translators, this one is designed to mimic server behavior. Assume that your Web server is configured to replace the KENT tag with a different picture of an engineer depending on the day of the week, the time of day, and the user’s platform. The translator does the same thing, only locally. Kent Tag Translator <meta http-equiv="Content-Type" content="text/html; charset="> <script language="JavaScript"> /********************************************************** * The getTranslatorInfo() function provides information * * about the translator, including its class and name, * * the types of documents that are likely to contain the * * markup to be translated, the regular expressions that * * a document containing the markup to be translated * * would match, and the default Translation preference * * (whether the translator should run on all files, no * * files, in files with the specified extensions, or in * * files matching the specified expressions). * **********************************************************/ function getTranslatorInfo(){ //Create a new array with 6 slots in it returnArray = new Array(6); returnArray[0] = "DREAMWEAVER_TEAM"// The translatorClass returnArray[1] = "Kent Tags"// The title returnArray[2] = "0" // The number of extensions returnArray[3] = "1"// The number of expressions returnArray[4] = "