Online Book Reader

Home Category

Beautiful Code [337]

By Root 5070 0
directions is constructed by concatenating the user input to the base URI for Yahoo! Maps.

Post-action

The resulting URI is passed to the function emacspeak-w3-extract-table-by-match along with a search pattern Start to:

Retrieve the content using Emacs W3.

Apply an XSLT transform to extract the table containing Start.

Render this table using Emacs W3's HTML formatter.

Unlike the query parameters, the layout of the results page does change about once a year, on average. But keeping this tool current with Yahoo! Maps comes down to maintaining the post-action portion of this utility. In over eight years of use, I have had to modify it about half a dozen times, and given that the underlying platform provides many of the tools for filtering the result page, the actual lines of code that need to be written for each layout change is minimal.

The emacspeak-w3-extract-table-by-match function uses an XSLT transformation that filters a document to return tables that contain a specified search pattern. For this example, the function constructs the following XPath expression:

(/descendant::table[contains(., Start)])[last( )]

This effectively picks out the list of tables that contain the string Start and returns the last element of that list.

Seven years after this utility was written, Google launched Google Maps to great excitement in February 2005. Many blogs on the Web put Google Maps under the microscope and quickly discovered the query parameters used by that application. I used that to build a corresponding Google Maps tool in Emacspeak that provides similar functionality. The user experience is smoother with the Google Maps tool because the start and end locations can be specified within the same parameter. Here is the code for the Google Maps wizard:

Code View: Scroll / Show All

(defun emacspeak-websearch-emaps-search (query &optional use-near)

"Perform EmapSpeak search. Query is in plain English."

(interactive

(list

(emacspeak-websearch-read-query

(if current-prefix-arg

(format "Find what near %s: "

emacspeak-websearch-emapspeak-my-location)

"EMap Query: "))

current-prefix-arg))

(let ((near-p ;; determine query type

(unless use-near

(save-match-data (and (string-match "near" query) (match-end 0)))))

(near nil)

(uri nil))

(when near-p ;; determine location from query

(setq near (substring query near-p))

(setq emacspeak-websearch-emapspeak-my-location near))

(setq uri

(cond

(use-near

(format emacspeak-websearch-google-maps-uri

(emacspeak-url-encode

(format "%s near %s" query near))))

(t (format emacspeak-websearch-google-maps-uri

(emacspeak-url-encode query)))))

(add-hook 'emacspeak-w3-post-process-hook 'emacspeak-speak-buffer)

(add-hook 'emacspeak-w3-post-process-hook

#'(lambda nil

(emacspeak-pronounce-add-buffer-local-dictionary-entry

"mi" " miles ")))

(browse-url-of-buffer

(emacspeak-xslt-xml-url

(expand-file-name "kml2html.xsl" emacspeak-xslt-directory)

uri))))

A brief explanation of the code follows:

Parse the input to decide whether it's a direction or a search query.

In case of search queries, cache the user's location for future use.

Construct a URI for retrieving results.

Browse the results of filtering the contents of the URI through the XSLT filter kml2html, which converts the retrieved content into a simple hypertext document.

Set up custom pronunciations in the results to pronounce mi as "miles."

Notice that, as before, most of the code focuses on application-specific tasks. Rich spoken output is produced by creating the results as a well-structured HTML document with the appropriate Aural CSS rules producing an audio-formatted presentation.

31.3.3. The Web Command Line and URL Templates

With more and more services becoming available on the Web, another useful pattern emerged by early 2000: web sites started creating smart client-side interaction via Java-Script. One typical use of such scripts was to construct URLs on the clientside for accessing specific pieces

Return Main Page Previous Page Next Page

®Online Book Reader