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