Skip to content

Commit db7424e

Browse files
committed
Continue #29, playpens are now runnable
1 parent 0ac0301 commit db7424e

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

src/theme/book.css

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,22 @@ h5 {
299299
.light pre {
300300
position: relative;
301301
}
302-
.light pre > i {
302+
.light pre > .buttons {
303303
position: absolute;
304304
right: 5px;
305305
top: 5px;
306306
color: #364149;
307307
cursor: pointer;
308308
}
309-
.light pre > i :hover {
309+
.light pre > .buttons :hover {
310310
color: #008cff;
311311
}
312+
.light pre > .buttons i {
313+
margin-left: 8px;
314+
}
315+
.light pre > .result {
316+
margin-top: 10px;
317+
}
312318
.light .sidebar {
313319
background-color: #fafafa;
314320
color: #364149;
@@ -372,16 +378,22 @@ h5 {
372378
.coal pre {
373379
position: relative;
374380
}
375-
.coal pre > i {
381+
.coal pre > .buttons {
376382
position: absolute;
377383
right: 5px;
378384
top: 5px;
379385
color: #a1adb8;
380386
cursor: pointer;
381387
}
382-
.coal pre > i :hover {
388+
.coal pre > .buttons :hover {
383389
color: #3473ad;
384390
}
391+
.coal pre > .buttons i {
392+
margin-left: 8px;
393+
}
394+
.coal pre > .result {
395+
margin-top: 10px;
396+
}
385397
.coal .sidebar {
386398
background-color: #292c2f;
387399
color: #a1adb8;
@@ -445,16 +457,22 @@ h5 {
445457
.navy pre {
446458
position: relative;
447459
}
448-
.navy pre > i {
460+
.navy pre > .buttons {
449461
position: absolute;
450462
right: 5px;
451463
top: 5px;
452464
color: #c8c9db;
453465
cursor: pointer;
454466
}
455-
.navy pre > i :hover {
467+
.navy pre > .buttons :hover {
456468
color: #2b79a2;
457469
}
470+
.navy pre > .buttons i {
471+
margin-left: 8px;
472+
}
473+
.navy pre > .result {
474+
margin-top: 10px;
475+
}
458476
.navy .sidebar {
459477
background-color: #282d3f;
460478
color: #c8c9db;
@@ -518,16 +536,22 @@ h5 {
518536
.rust pre {
519537
position: relative;
520538
}
521-
.rust pre > i {
539+
.rust pre > .buttons {
522540
position: absolute;
523541
right: 5px;
524542
top: 5px;
525543
color: #c8c9db;
526544
cursor: pointer;
527545
}
528-
.rust pre > i :hover {
546+
.rust pre > .buttons :hover {
529547
color: #e69f67;
530548
}
549+
.rust pre > .buttons i {
550+
margin-left: 8px;
551+
}
552+
.rust pre > .result {
553+
margin-top: 10px;
554+
}
531555
.rust .sidebar {
532556
background-color: #3b2e2a;
533557
color: #c8c9db;

src/theme/book.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,20 @@ $( document ).ready(function() {
140140

141141
$("code.language-rust").each(function(i, block){
142142

143+
var code_block = $(this);
144+
var pre_block = $(this).parent();
143145
// hide lines
144-
var lines = $(this).html().split("\n");
146+
var lines = code_block.html().split("\n");
145147
var first_non_hidden_line = false;
146148
var lines_hidden = false;
147149

148150
for(var n = 0; n < lines.length; n++){
149151
if($.trim(lines[n])[0] == hiding_character){
150152
if(first_non_hidden_line){
151-
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].substr(1) + "</span>";
153+
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)#/, "$1") + "</span>";
152154
}
153155
else {
154-
lines[n] = "<span class=\"hidden\">" + lines[n].substr(1) + "\n" + "</span>";
156+
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)#/, "$1") + "\n" + "</span>";
155157
}
156158
lines_hidden = true;
157159
}
@@ -162,25 +164,65 @@ $( document ).ready(function() {
162164
first_non_hidden_line = true;
163165
}
164166
}
165-
$(this).html(lines.join(""));
167+
code_block.html(lines.join(""));
166168

167169
// If no lines were hidden, return
168170
if(!lines_hidden) { return; }
169171

170172
// add expand button
171-
$(this).parent().prepend("<i class=\"fa fa-expand\"></i>");
173+
pre_block.prepend("<div class=\"buttons\"><i class=\"fa fa-expand\"></i></div>");
172174

173-
$(this).parent().find("i").click(function(e){
175+
pre_block.find("i").click(function(e){
174176
if( $(this).hasClass("fa-expand") ) {
175177
$(this).removeClass("fa-expand").addClass("fa-compress");
176-
$(this).parent().find("span.hidden").removeClass("hidden").addClass("unhidden");
178+
pre_block.find("span.hidden").removeClass("hidden").addClass("unhidden");
177179
}
178180
else {
179181
$(this).removeClass("fa-compress").addClass("fa-expand");
180-
$(this).parent().find("span.unhidden").removeClass("unhidden").addClass("hidden");
182+
pre_block.find("span.unhidden").removeClass("unhidden").addClass("hidden");
181183
}
182184
});
183185
});
184186

185187

188+
// Process playpen code blocks
189+
$(".playpen").each(function(block){
190+
var pre_block = $(this);
191+
// Add play button
192+
var buttons = pre_block.find(".buttons");
193+
if( buttons.length === 0 ) {
194+
pre_block.prepend("<div class=\"buttons\"></div>");
195+
buttons = pre_block.find(".buttons");
196+
}
197+
buttons.prepend("<i class=\"fa fa-play play-button\"></i>");
198+
199+
buttons.find(".play-button").click(function(e){
200+
run_rust_code(pre_block);
201+
});
202+
});
203+
204+
186205
});
206+
207+
208+
function run_rust_code(code_block) {
209+
var result_block = code_block.find(".result");
210+
if(result_block.length === 0) {
211+
code_block.append("<code class=\"result hljs language-bash\"></code>");
212+
result_block = code_block.find(".result");
213+
}
214+
215+
result_block.text("Running...");
216+
217+
$.ajax({
218+
url: "https://play.rust-lang.org/evaluate.json",
219+
method: "POST",
220+
crossDomain: true,
221+
dataType: "json",
222+
contentType: "application/json",
223+
data: JSON.stringify({version: "stable", optimize: "0", code: code_block.find(".language-rust").text() }),
224+
success: function(response){
225+
result_block.text(response.result);
226+
}
227+
});
228+
}

src/theme/stylus/themes/base.styl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pre {
1111
position: relative;
1212
}
13-
pre > i {
13+
pre > .buttons {
1414
position: absolute;
1515
right: 5px;
1616
top: 5px;
@@ -21,6 +21,14 @@
2121
:hover {
2222
color: $sidebar-active;
2323
}
24+
25+
i {
26+
margin-left: 8px;
27+
}
28+
}
29+
30+
pre > .result {
31+
margin-top: 10px;
2432
}
2533

2634
color: $fg

0 commit comments

Comments
 (0)