JsTemplate/Link.js
[gnome.introspection-doc-generator] / JsTemplate / Link.js
1 //<script type="text/javascript">
2
3 XObject  = imports.XObject.XObject;
4
5 /**
6  * Generic Template Link handler..
7  * 
8  * 
9  * 
10  */
11
12 /** Handle the creation of HTML links to documented symbols.
13         @constructor
14 */
15 Link = XObject.define(
16     /*
17     * constructor
18     */ 
19     function () {
20         
21     }, 
22     Object,
23     {
24         
25         alias : "",
26         src : "",
27         file : "",
28         text : "",
29         innerName : "",
30         classLink : false,
31         targetName : "",
32     
33         
34         
35         target : function(targetName) {
36             if (typeof(targetName) != 'undefined') this.targetName = targetName;
37             return this;
38         },
39         inner : function(inner) {
40             if (typeof(inner) != 'undefined') this.innerName = inner;
41             return this;
42         },
43         withText : function(text) {
44             if (typeof(text) != 'undefined') this.text = text;
45             return this;
46         },
47         toSrc : function(filename) {
48             if (typeof(filename) != 'undefined') this.src = filename;
49             
50             return this;
51         },
52         toSymbol : function(alias) {
53             if (typeof(alias) != 'undefined') {
54                 this.alias = new String(alias);
55             }
56             return this;
57         },
58         toClass : function(alias) {
59             this.classLink = true;
60             return this.toSymbol(alias);
61         },
62         toFile : function(file) {
63             if (typeof(file) != 'undefined') this.file = file;
64             return this;
65         },
66         
67         toString : function() {
68             var linkString;
69             var thisLink = this;
70
71             if (this.alias) {
72                 linkString = this.alias.replace(/(^|[^a-z$0-9_#.:-])([|a-z$0-9_#.:-]+)($|[^a-z$0-9_#.:-])/i,
73                     function(match, prematch, symbolName, postmatch) {
74                         var symbolNames = symbolName.split("|");
75                         var links = [];
76                         for (var i = 0, l = symbolNames.length; i < l; i++) {
77                             thisLink.alias = symbolNames[i];
78                             links.push(thisLink._makeSymbolLink(symbolNames[i]));
79                         }
80                         return prematch+links.join("|")+postmatch;
81                     }
82                 );
83             }
84             else if (this.src) {
85                 linkString = thisLink._makeSrcLink(this.src);
86             }
87             else if (this.file) {
88                 linkString = thisLink._makeFileLink(this.file);
89             }
90
91             return linkString;
92         },
93         
94         
95         
96         
97         
98         
99         
100         /** Create a link to a snother symbol. */
101         _makeSymbolLink : function(alias) {
102             
103             // look for '/' in alias..
104             if (/\//.test(alias)) {
105                 var bits = alias.split('/');
106                 var ret = "";
107                 for(var i=0; i < bits.length; i++) {
108                     if (i > 0) {
109                         ret +="/";
110                     }
111                     ret += this._makeSymbolLink(bits[i]);
112                 }
113                 return ret;
114                 
115             }
116             
117             
118             
119             var linkBase = Link.base+  imports.JSDOC.BuildDocs.BuildDocs.symbolsDir;
120             var linkTo = Link.symbolSet.getSymbol(alias);
121             var linkPath;
122             var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
123             
124             // is it an internal link?
125             if (alias.charAt(0) == "#") {
126                 linkPath = alias;
127                 fullLinkPath = alias;
128             
129             // if there is no symbol by that name just return the name unaltered
130             } else if (!linkTo) {
131                 
132                 if (typeof(Link.builtins[alias]) != 'undefined') {
133                     return "<a href=\""+ Link.builtins[alias]+"\""+target+">"+alias+"</a>";
134                  }
135                 
136                 return this.text || alias;
137             
138             
139             // it's a symbol in another file
140             } else {
141
142                 if (!linkTo.is("CONSTRUCTOR") && !linkTo.isNamespace) { // it's a method or property
143                     linkPath = escape(linkTo.memberOf) || "_global_";
144                     linkPath += imports.JSDOC.Options.Options.ext + "#" + Link.symbolNameToLinkName(linkTo);
145                 }
146                 else {
147                     linkPath = escape(linkTo.alias);
148                     linkPath += imports.JSDOC.Options.Options.ext + (this.classLink? "":"#" + Link.hashPrefix + "constructor");
149                 }
150                 //linkPath = linkBase + linkPath;
151                 fullLinkPath = linkBase + linkPath;
152             }
153             
154             var linkText = this.text || alias;
155             
156             var link = {linkPath: linkPath, linkText: linkText, fullLinkPath: fullLinkPath};
157             
158             //if (typeof JSDOC.PluginManager != "undefined") {
159             //    JSDOC.PluginManager.run("onSymbolLink", link);
160             //}
161             
162             return "<a href=\""+link.fullLinkPath+"\""+target+" roo:cls=\""+link.linkPath+"\">"+link.linkText+"</a>";
163         },
164
165
166         /** Create a link to a source file. */
167         _makeSrcLink : function(srcFilePath) {
168             var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
169                 
170             // transform filepath into a filename
171             var srcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, "."); // was _
172             var lsrcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, ".");
173             var outFilePath = Link.base + '/symbols/' +  srcFile.replace(/.js$/, '') + 
174                 imports.JSDOC.Options.Options.publishExt;
175             
176             if (!this.text) this.text = srcFilePath; //FilePath.fileName(srcFilePath);
177             return "<a href=\""+outFilePath+"\""+target+" roo:cls=\"src/"+lsrcFile+"\">"+this.text+"</a>";
178         },
179
180         /** Create a link to a source file. */
181         _makeFileLink : function(filePath) {
182             var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
183                 
184             var outFilePath =  Link.base + filePath;
185
186             if (!this.text) this.text = filePath;
187             return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
188         }
189 });
190
191
192
193
194 /** prefixed for hashes */
195 Link.hashPrefix = "";
196
197 /** Appended to the front of relative link paths. */
198 Link.base = "";
199
200 Link.symbolNameToLinkName = function(symbol) {
201         var linker = "";
202         if (symbol.isStatic) linker = ".";
203         else if (symbol.isInner) linker = "-";
204         
205         return Link.hashPrefix+linker+symbol.name;
206 }
207
208
209 Link.builtins = {
210     'Object' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object',
211     'Object...' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object',
212     'Function' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function',
213     'String' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String',
214     'Number' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Number',
215     'Boolean' : 'http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Boolean',
216     'HTMLElement' : 'http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-58190037'
217 }
218     
219