Automatic Menus Old Version
This is just in case it might be useful, to whom I'm not quite sure. For a discussion of the latest AutoMenu stuff follow the link.
2003-06-14
See 2003-06-13 for a description.
Problems
This section discusses what is wrong with the current implementation and what might be done to remedy its deficiencies.
- Links
- Absolute URLs work fine but relative links are more awkward. Because a link is specified in the same way regardless of the location of the file in which it is inserted this system has the limitation that all files must have the same relationship to the targets. It might be thought that we could simply express the target URLs relative the the root of the web site; the problem is that we have no obvious way of determining the root of the web site. We cannot assume that the root is simply / because many, or most, personal web sites are sub-directories of the host root. For instance the index page of my site on Tele2 is at http://home.c2i.net/kwhitefoot/index.html, the root of the site is at http://home.c2i.net/ but the root of my part of it is at http://home.c2i.net/kwhitefoot. But I can't hardcode the kwhitefoot partly because I cannot guarantee that it will be correct if I copy the web site to another host (or partition it among several hosts, see PartitionWebSite) but also because such a scheme means that I would have to have a corresponding location on my local disk, not terribly practical in Windows where file system links are either not available (Win98) or not easily managed (Win2k, WinXP). This last objection of course only applies when you want the local web to function directly from the disk, if you use a local web server you can arrange for the site it to live in any virtual directory you like for local viewing purposes.
- Browser Compatibility
- The current version only works in IE.
Solutions
- Links
- The simplest solution I can think of is for each document to know its own position in the web relative to the root of the site. This can be specified by a link element. Just define a new value for the rel attribute. I chose "root". Set the href attribute to the relative path of the root directory. Scripts can then look for this link element and use it to determine the path to a linked document. The best thing would be to use a rev link and give the path from the current document to the root; however, html editors can often maintain link addresses automaticaly so that when you move a document all the relative links are adjusted so that they point at the original targets, Front Page at least cannot cope with rev links, it still thinks that they are forward links and adjusts them accordingly. So we must use rel links and do the work ourselves in the scripts. So imagine that we have a link in our links array that points at a document in folder x and that we are creating the href anchor in a document in folder y. Then the rel link looks like: The link to the document in x looks like x/doc.html. We can simply concatenate the two addresses to get URL relative to the current document: ../x/doc.html. So the script has to do two things: find the href attribute of a link element having attribute rel="root" and concatenate this with any relative URLs before adding them to the newly created anchors.
2003-06-13
Finally thought of a name: Automenu. Menu rather than link because it creates little in-line popups when the matched text is associated with more than one URL.
I think I might, at last, have invented something useful. One of the reasons that this web site is created using Emacs-Wiki is that I am much too lazy to write HTML and to keep all the links in order. The problem is that even though Emacs-Wiki is good at maintaining the links within the Wiki it isn't terribly convenient when it comes to external links or links to non-Wiki parts of the site. A solution is Automenu.
So what is it?
It is simply a list of words or phrases associated with URLs together with a program that searches a web page and replaces text that matches the phrases with anchors. A refinement is that if a phrase is associated with a list of URLs then a menu is created that appears when the mouse hovers over the link.
To use it you simply
- create an array for the phrases and URLs in a Javascript file,
- add a reference to this file in the head of each document,
- add a reference to the automenu script,
- add a call to the show function either as the onload event of the body of the document or in a script element after the body element.
That's it. Now, if your document contains matches for the phrases it will now contain links to the urls. For a demonstration you need look no further than this document, all of the external links in this document were inserted by Automenu.
Just view the source and pick up the script files and the popup.css file to see the gory details.
2003
Can't think of a good name for this gadget. Anyway the basic idea is to search the text nodes of a document for keywords and replace them with links or images.The first version is text1.htm. This was created just to explore the idea.
Requirements for the next version
If this is to be useful I think that what it will be useful for is adding links to documents without having to bother to do it manually.
Here is how it might be achieved:
- Replace the list of words
- Use a dictionary of arrays instead where the key is a word or phrase and the associated array contains a a URL to be used as a link href and another to be used as am img src. Either could be null.
- Sort the list of phrases
- sort the phrases into order by length so that we try to replace longer phrases first.
- Sorting by length is easy
- just get each phrase in turn and add it to a list. Find the list by looking up the length in an array. No need for Quick Sort, Bubble Sort or Any Other Sort.
- Split the loop
- The function that does the replace must be split into a control loop and a function that replaces a single phrase with a node and returns an array consisting of the node and the remaining text to either side.
- Use a queue
- the replacement process is controlled by a queue. The initial text is placed in the queue. Then a loop is run that removes the head of the queue. If the removed item is a node it is inserted before the original node in the document if not it is split into text, new node, text and all this items pushed to the head of the queue. Looks remarkably like a stack, just have to be careful to make sure we understand which end is which.
- If necessary
- for long text strings and remote URLs the replacement might take a long time. During this time the original and replacement are both visible. We can use the data property of the text node to avoid this by setting it using the remining text after a split. This can be further optimised by recognizing when it needs to be done: only when the last item in the queue changes, that is when the queue becomes empty.
No comments:
Post a Comment