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