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