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