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