Skip to content

Commit a04af27

Browse files
committed
Add compile button
1 parent b4784c2 commit a04af27

File tree

13 files changed

+201
-36
lines changed

13 files changed

+201
-36
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
**.swo
22
**.swp
33
node_modules/
4-
**/.DS_Store
4+
**.DS_Store

config/config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"cookie" : {
77
"secret" : "nzalv26mhkp3h5jzc7p30tdh3lj9rpyxliyxic97hjeyf6t691golpgwr8j1siyn"
88
},
9-
"host" : "localhost:3000"
9+
"host" : "http://localhost:3000",
10+
"clsi" : {
11+
"host" : "http://localhost:3002",
12+
"token" : "1bd993d7d739eca8740f9919d8748352"
13+
}
1014
}

index.js

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var fs = require("fs");
22
var express = require("express");
33
var connect = require("connect");
4+
var http = require("http");
5+
var url = require("url");
6+
var request = require("request");
47
var MergeServer = require("./server/merge");
58

69
var config = JSON.parse(fs.readFileSync(__dirname + "/config/config.json"));
@@ -11,7 +14,6 @@ var app = express.createServer();
1114

1215
app.use(connect.cookieParser(config.cookie.secret));
1316
app.use(connect.cookieSession({ key: "scribtex-dropbox" }));
14-
app.use(connect.static("public"));
1517

1618
app.get("/session/new", function(req, res, next) {
1719
dbox.request_token(function(status, dboxRes) {
@@ -44,10 +46,21 @@ function redirectToLogin(req, res, next) {
4446

4547
app.get(/^\/files\/(.*)/, function(req, res, next) {
4648
var filePath = req.params[0];
49+
50+
var oauth_token, oauth_token_secret;
51+
if (req.query.access_token && req.query.access_token_secret) {
52+
oauth_token = req.query.access_token;
53+
oauth_token_secret = req.query.access_token_secret;
54+
} else {
55+
oauth_token = req.session.dropbox_oauth_token;
56+
oauth_token_secret = req.session.dropbox_oauth_token_secret;
57+
}
58+
4759
var client = dbox.createClient({
48-
oauth_token : req.session.dropbox_oauth_token,
49-
oauth_token_secret : req.session.dropbox_oauth_token_secret
60+
oauth_token : oauth_token,
61+
oauth_token_secret : oauth_token_secret
5062
});
63+
5164
client.get(filePath, function(status, reply, metadata) {
5265
try {
5366
metadata = JSON.parse(metadata);
@@ -60,11 +73,11 @@ app.get(/^\/files\/(.*)/, function(req, res, next) {
6073
headers = {
6174
"Content-Type" : metadata["mime_type"],
6275
"X-Revision-Id" : metadata["rev"],
63-
"X-Icon" : metadata["icon"]
76+
"X-Icon" : metadata["icon"],
77+
"Last-Modified" : metadata["modified"]
6478
}
6579
}
6680

67-
console.log(filePath, status, reply);
6881
res.writeHead(status, headers);
6982
res.write(reply || ""); // Sometimes reply is undefined. Blank file?
7083
res.end();
@@ -109,7 +122,9 @@ app.put(/^\/files\/(.*)/, function(req, res, next) {
109122
if (metadata) {
110123
headers = {
111124
"Content-Type" : metadata.mime_type,
112-
"X-Revision-Id" : metadata.rev
125+
"X-Revision-Id" : metadata.rev,
126+
"X-Icon" : metadata["icon"],
127+
"Last-Modified" : metadata["modified"]
113128
}
114129
}
115130

@@ -156,6 +171,60 @@ app.propfind(/^\/files\/(.*)/, function(req, res, next) {
156171
});
157172
});
158173

174+
app.all(/^\/clsi\/(.*)/, function(req, res, next) {
175+
var clientUrl = "/" + req.params[0];
176+
177+
if (clientUrl == "/clsi/compile" && req.method == "POST") {
178+
// Insert the token and access params
179+
var body = "";
180+
req.on("data", function(chunk) { body += chunk });
181+
req.on("end", function() {
182+
function badRequest() {
183+
res.writeHead(400);
184+
res.end();
185+
}
186+
187+
try {
188+
var json = JSON.parse(body);
189+
} catch (e) {
190+
badRequest();
191+
return;
192+
}
193+
194+
if (!json.compile) {
195+
json.compile = {};
196+
}
197+
198+
json.compile.token = config.clsi.token;
199+
200+
if (!json.compile.resources) {
201+
json.compile.resources = [];
202+
}
203+
204+
for (var i = 0; i < json.compile.resources.length; i++) {
205+
var resource = json.compile.resources[i];
206+
if (resource.url) {
207+
var resourceUrl = url.parse(resource.url, true);
208+
resourceUrl.query["access_token"] = req.session.dropbox_oauth_token;
209+
resourceUrl.query["access_token_secret"] = req.session.dropbox_oauth_token_secret;
210+
resource.url = url.format(resourceUrl);
211+
}
212+
}
213+
214+
request({
215+
url : config.clsi.host + clientUrl,
216+
headers : req.headers,
217+
method : "POST",
218+
body : JSON.stringify(json)
219+
}).pipe(res);
220+
});
221+
} else {
222+
req.pipe(request(config.clsi.host + clientUrl)).pipe(res);
223+
}
224+
});
225+
226+
app.use(connect.static("public"));
227+
159228
var merge = new MergeServer({
160229
mount : "/merge"
161230
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version" : "0.0.1",
44
"dependencies" : {
55
"express" : "2.5.9",
6+
"request" : "2.9.202",
67
"dbox" : "0.4.3"
78
}
89
}

public/editor/compile-window.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
define(["components/pdf-window-view/pdf-window-view"], function(PdfWindowView) {
2+
var query = {};
3+
_.each( location.search.slice(1).split("&"), function(part) {
4+
query[part.split("=")[0]] = part.split("=")[1];
5+
});
6+
7+
var pdfUrl, logUrl;
8+
if (query.success == "true") {
9+
pdfUrl = "http://localhost:3002/output/" + query.compileId + "/output.pdf";
10+
}
11+
logUrl = "/clsi/output/" + query.compileId + "/output.log";
12+
13+
var view = new PdfWindowView({
14+
pdfUrl : pdfUrl,
15+
logUrl : logUrl
16+
});
17+
18+
$(document.body).append(view.el);
19+
view.render();
20+
});
21+

public/editor/compiled.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
5+
<title>
6+
Compiled document
7+
</title>
8+
9+
<link rel="stylesheet" href='/editor/bootstrap/bootstrap/css/bootstrap.css'></link>
10+
11+
<script src='/editor/support/jquery-1.7.2.min.js'></script>
12+
<script src='/editor/support/jquery-ui-1.8.20.min.js'></script>
13+
<script src='/editor/bootstrap/js/bootstrap-tab.js'></script>
14+
<script src='/editor/support/underscore-min.js'></script>
15+
<script src='/editor/support/backbone-min.js'></script>
16+
<script src='/editor/support/backbone-customisations.js'></script>
17+
<script src='/editor/support/require.js' data-main="compile-window"></script>
18+
19+
</head>
20+
21+
<body class="no-bounce">
22+
<script type="text/template" id="pdf-tabs-template">
23+
<ul class="nav nav-tabs full-page-pdf-tabs" id="pdf-tabs">
24+
<a class="btn btn-primary" href="{{ pdfSrc }}">Download</a>
25+
<li>
26+
<a href="#log" data-toggle="tab">Log</a>
27+
</li>
28+
29+
<li class="active">
30+
<a href="#pdf" data-toggle="tab">PDF</a>
31+
</li>
32+
</ul>
33+
34+
<div class="tab-content">
35+
<div class="pdf-tab-pane tab-pane active" id="pdf">
36+
<embed src="{{ pdfSrc }}"></embed>
37+
</div>
38+
<div class="log-tab-pane tab-pane" id="log">Log</div>
39+
</div>
40+
</script>
41+
</body>
42+
</html>

public/editor/components/compile/compile.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ define([
22
"components/base",
33
"components/compile/clsi_client",
44
"components/dialog/dialog",
5+
"components/text-editor/text-editor",
56
"components/compile/views/new-window-view",
67
"lib/path_util"
7-
], function(Base, CLSIClient, Dialog, NewWindowView, PathUtil) {
8+
], function(Base, CLSIClient, Dialog, TextEditor, NewWindowView, PathUtil) {
89
var CompileState = Backbone.Model.extend();
910

1011
var Compile = Base.extend({
@@ -34,35 +35,48 @@ define([
3435
var resources = [];
3536
var options = {};
3637

37-
editor.get("openFileView").transferContentToModel();
3838

39-
var rootResourcePath;
40-
if (editor.get("openFile").get("content").match(/\\documentclass/)) {
41-
rootResourcePath = editor.get("openFile").get("path")
42-
} else {
43-
rootResourcePath = editor.get("mainFile");
39+
if (!editor.get("openFile")) {
40+
this.enableCompiling();
41+
return;
4442
}
4543

44+
var rootResourcePath;
45+
editor.get("openFiles").each(function(file) {
46+
if (file.get("content") && file.get("content").match(/\\documentclass/)) {
47+
rootResourcePath = file.get("path")
48+
}
49+
});
50+
4651
if (!rootResourcePath) {
4752
this.showNoRootResourceDialog();
4853
this.enableCompiling();
4954
return;
5055
}
5156

57+
// Lets gather the resources by their ids so that we can override existing ones
58+
// We'll group them into an array later.
59+
var resources = {};
5260
editor.get("rootDirectory").eachFile(function(file) {
53-
if (file === editor.get("openFile")) {
54-
resources.push({
61+
resources[file.id] = {
62+
path : file.get("path"),
63+
url : PathUtil.join(editor.get("host"), editor.get("fileBaseUrl"), file.get("path")),
64+
modified : file.get("modified")
65+
};
66+
});
67+
// Override resources with content from open files
68+
_.each(editor.components.tabs.getOpenFileViews(), function(fileView) {
69+
if (fileView instanceof TextEditor) {
70+
var file = fileView.model;
71+
fileView.transferContentToModel();
72+
resources[file.id] = {
5573
content : file.get("content"),
5674
path : file.get("path")
57-
});
58-
} else {
59-
resources.push({
60-
path : file.get("path"),
61-
url : PathUtil.join(editor.get("origin"), editor.get("fileBaseUrl"), file.get("path")),
62-
modifed : file.get("lastModified")
63-
});
75+
};
6476
}
6577
});
78+
// Flatten our resource down to an array.
79+
resources = _.map(resources, function(value, key) { return value });
6680

6781
var self = this;
6882
CLSIClient.compile(resources, {
@@ -97,12 +111,12 @@ define([
97111
enableCompiling : function() {
98112
$("#compile-button").removeClass("disabled");
99113
var self = this;
100-
$("#compile-button").on("click", function() { self.compile() });
114+
$("#compile-button").on("click.compile", function() { self.compile() });
101115
},
102116

103117
disableCompiling : function() {
104118
$("#compile-button").addClass("disabled");
105-
$("#compile-button").off("click");
119+
$("#compile-button").off("click.compile");
106120
},
107121

108122
showNoRootResourceDialog : function() {

public/editor/components/compile/views/new-window-view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ define(["lib/path_util"], function(PathUtil) {
55
},
66

77
openWindow : function(compileId, pdfExists) {
8-
var url = PathUtil.join(editor.get("projectBaseUrl"), "compiled");
8+
var url = editor.get("compileWindowUrl");
99
url += "?compileId=" + compileId;
1010
url += "&success=" + pdfExists;
11-
this.compileWindow = window.open(url, "scribtex-compiled");
11+
this.compileWindow = window.open(url, "compile-window");
1212
},
1313

1414
closeWindow : function() {

public/editor/components/tabs/tabs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ define(["components/base", "lib/path_util"], function(Base, PathUtil) {
5959

6060
// Remove it from the places the open files and tabs are stored.
6161
delete this.openTabs[file.id];
62-
editor.get("openFiles").remove(file);
62+
editor.get("openFiles").remove(tab.file);
6363

6464
// If the tab is currently selected we open either the previous of
6565
// next tab (unless there are no more tabs!)
@@ -99,6 +99,10 @@ define(["components/base", "lib/path_util"], function(Base, PathUtil) {
9999
tab.tabEl.find("a").tab("show");
100100
},
101101

102+
getOpenFileViews : function(file) {
103+
return _.map(this.openTabs, function(tab) { return tab.fileView });
104+
},
105+
102106
_removeEventListeners : function(tabEl) {
103107
tabEl.find("a").off("click.tabs");
104108
tabEl.find("a").off("shown.tabs");

0 commit comments

Comments
 (0)