77de3ce1171da28b834e4bf1147281671bfc75a4
[nit.git] / share / nitdoc / js / plugins / github / utils.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 /*
20 * Nitdoc.Github comment edition module
21 *
22 * Allows user to modify source code comments directly from the Nitdoc
23 */
24 define([
25 "jquery",
26 "base64",
27 "Markdown.Converter",
28 "plugins/utils",
29 "plugins/github/ui",
30 ], function($, Base64, mkdown, utils) {
31
32 /*
33 * Nitdoc.GitHub.Utils module
34 */
35
36 return {
37 // Extract infos from string location "lib/standard/collection/array.nit:457,1--458,0"
38 parseLocation: function(location) {
39 var parts = location.split(":");
40 var loc = new Object();
41 loc.origin = location;
42 loc.path = parts[0];
43 loc.lstart = parseInt(parts[1].split("--")[0].split(",")[0]);
44 loc.tabpos = parseInt(parts[1].split("--")[0].split(",")[1]);
45 loc.lend = parseInt(parts[1].split("--")[1].split(",")[0]);
46 return loc;
47 },
48
49 // Meld modified comment into file conten
50 mergeComment: function(fileContent, comment, location) {
51 // replace comment in file content
52 var res = new String();
53 var lines = fileContent.split("\n");
54 // copy lines fron 0 to lstart
55 for(var i = 0; i < location.lstart - 1; i++) {
56 res += lines[i] + "\n";
57 }
58 // set comment
59 if(comment && comment != "") {
60 var commentLines = comment.split("\n");
61 for(var i = 0; i < commentLines.length; i++) {
62 var line = commentLines[i];
63 var tab = location.tabpos > 1 ? "\t" : "";
64 res += tab + (line.length > 0 ? "# " : "#") + line + "\n";
65 }
66 }
67 // copy lines fron lend to end
68 for(var i = location.lend - 1; i < lines.length; i++) {
69 res += lines[i];
70 if(i < lines.length - 1) { res += "\n"; }
71 }
72 return res;
73 }
74 }
75 });