Skip to content

Commit ec741f9

Browse files
committed
Syntax coloring
Added simple syntax coloring using highlight.js. * Lightweight. * Automatic detection of language.
1 parent 606a554 commit ec741f9

File tree

6 files changed

+161
-5
lines changed

6 files changed

+161
-5
lines changed

css/zerobin.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ float:right;
7171
width:60%;
7272
}
7373

74-
div#aboutbox a { color: #94a3b4; }
75-
76-
textarea#message,div#cleartext,.replymessage {
74+
div#aboutbox a { color: #94a3b4; }
75+
textarea#message,div#cleartext,.replymessage, code {
7776
clear:both;
7877
color:black;
7978
background-color:#fff;
@@ -90,6 +89,7 @@ box-sizing:border-box;
9089
width:100%;
9190
}
9291

92+
9393
div#status {
9494
clear:both;
9595
padding:5px 10px;
@@ -164,7 +164,7 @@ position:relative;
164164
top:2px;
165165
}
166166

167-
div#expiration, div#language, div#opendisc {
167+
div#expiration, div#language, div#opendisc, div#syntaxcoloringoption {
168168
background-color:#414D5A;
169169
padding:6px 8px;
170170
margin:0px 5px 0px 0px;;

index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function deletePaste($pasteid)
121121
All optional data will go to meta information:
122122
expire (optional) = expiration delay (never,10min,1hour,1day,1month,1year,burn) (default:never)
123123
opendiscusssion (optional) = is the discussion allowed on this paste ? (0/1) (default:0)
124+
syntaxcoloring (optional) = should this paste use syntax coloring when displaying.
124125
nickname (optional) = son encoded SJCL encrypted text nickname of author of comment (containing keys: iv,salt,ct)
125126
parentid (optional) = in discussion, which comment this comment replies to.
126127
pasteid (optional) = in discussion, which paste this comment belongs to.
@@ -172,6 +173,14 @@ function deletePaste($pasteid)
172173
if ($opendiscussion!='0') { $meta['opendiscussion']=true; }
173174
}
174175

176+
// Show we use syntax coloring when displaying ?
177+
if (!empty($_POST['syntaxcoloring']))
178+
{
179+
$syntaxcoloring = $_POST['syntaxcoloring'];
180+
if ($syntaxcoloring!='0' && $syntaxcoloring!='1') { $error=true; }
181+
if ($syntaxcoloring!='0') { $meta['syntaxcoloring']=true; }
182+
}
183+
175184
// You can't have an open discussion on a "Burn after reading" paste:
176185
if (isset($meta['burnafterreading'])) unset($meta['opendiscussion']);
177186

@@ -223,6 +232,7 @@ function deletePaste($pasteid)
223232

224233
unset($storage['expire_date']); // Comment do not expire (it's the paste that expires)
225234
unset($storage['opendiscussion']);
235+
unset($storage['syntaxcoloring']);
226236

227237
// Make sure paste exists.
228238
$storagedir = dataid2path($pasteid);

js/highlight.pack.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/highlight.styles/monokai.css

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Monokai style - ported by Luigi Maselli - http://grigio.org
3+
*/
4+
5+
pre code {
6+
display: block; padding: 0.5em;
7+
background: #272822;
8+
}
9+
10+
pre .tag,
11+
pre .tag .title,
12+
pre .keyword,
13+
pre .literal,
14+
pre .change,
15+
pre .winutils,
16+
pre .flow,
17+
pre .lisp .title,
18+
pre .clojure .built_in,
19+
pre .nginx .title,
20+
pre .tex .special {
21+
color: #F92672;
22+
}
23+
24+
pre code {
25+
color: #DDD;
26+
}
27+
28+
pre code .constant {
29+
color: #66D9EF;
30+
}
31+
32+
pre .class .title {
33+
color: white;
34+
}
35+
36+
pre .attribute,
37+
pre .symbol,
38+
pre .symbol .string,
39+
pre .value,
40+
pre .regexp {
41+
color: #BF79DB;
42+
}
43+
44+
pre .tag .value,
45+
pre .string,
46+
pre .subst,
47+
pre .title,
48+
pre .haskell .type,
49+
pre .preprocessor,
50+
pre .ruby .class .parent,
51+
pre .built_in,
52+
pre .sql .aggregate,
53+
pre .django .template_tag,
54+
pre .django .variable,
55+
pre .smalltalk .class,
56+
pre .javadoc,
57+
pre .django .filter .argument,
58+
pre .smalltalk .localvars,
59+
pre .smalltalk .array,
60+
pre .attr_selector,
61+
pre .pseudo,
62+
pre .addition,
63+
pre .stream,
64+
pre .envvar,
65+
pre .apache .tag,
66+
pre .apache .cbracket,
67+
pre .tex .command,
68+
pre .prompt {
69+
color: #A6E22E;
70+
}
71+
72+
pre .comment,
73+
pre .java .annotation,
74+
pre .python .decorator,
75+
pre .template_comment,
76+
pre .pi,
77+
pre .doctype,
78+
pre .deletion,
79+
pre .shebang,
80+
pre .apache .sqbracket,
81+
pre .tex .formula {
82+
color: #75715E;
83+
}
84+
85+
pre .keyword,
86+
pre .literal,
87+
pre .css .id,
88+
pre .phpdoc,
89+
pre .title,
90+
pre .haskell .type,
91+
pre .vbscript .built_in,
92+
pre .sql .aggregate,
93+
pre .rsl .built_in,
94+
pre .smalltalk .class,
95+
pre .diff .header,
96+
pre .chunk,
97+
pre .winutils,
98+
pre .bash .variable,
99+
pre .apache .tag,
100+
pre .tex .special,
101+
pre .request,
102+
pre .status {
103+
font-weight: bold;
104+
}
105+
106+
pre .coffeescript .javascript,
107+
pre .javascript .xml,
108+
pre .tex .formula,
109+
pre .xml .javascript,
110+
pre .xml .vbscript,
111+
pre .xml .css,
112+
pre .xml .cdata {
113+
opacity: 0.5;
114+
}

js/zerobin.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ function setElementText(element, text) {
166166
}
167167
}
168168

169+
/** Apply syntax coloring to clear text area.
170+
*/
171+
function applySyntaxColoring()
172+
{
173+
if ($('div#cleartext').html().substring(0,11) != '<pre><code>')
174+
{
175+
// highlight.js expects code to be surrounded by <pre><code>
176+
$('div#cleartext').html('<pre><code>'+ $('div#cleartext').html()+'</code></pre>');
177+
}
178+
hljs.highlightBlock(document.getElementById('cleartext'));
179+
$('div#cleartext').css('padding','0'); // Remove white padding around code box.
180+
}
181+
182+
169183
/**
170184
* Show decrypted text in the display area, including discussion (if open)
171185
*
@@ -184,6 +198,10 @@ function displayMessages(key, comments) {
184198
setElementText($('div#cleartext'), cleartext);
185199
urls2links($('div#cleartext')); // Convert URLs to clickable links.
186200

201+
// comments[0] is the paste itself.
202+
203+
if (comments[0].meta.syntaxcoloring) applySyntaxColoring();
204+
187205
// Display paste expiration.
188206
if (comments[0].meta.expire_date) $('div#remainingtime').removeClass('foryoureyesonly').text('This document will expire in '+secondsToHuman(comments[0].meta.remaining_time)+'.').show();
189207
if (comments[0].meta.burnafterreading) {
@@ -312,7 +330,8 @@ function send_data() {
312330
var cipherdata = zeroCipher(randomkey, $('textarea#message').val());
313331
var data_to_send = { data: cipherdata,
314332
expire: $('select#pasteExpiration').val(),
315-
opendiscussion: $('input#opendiscussion').is(':checked') ? 1 : 0
333+
opendiscussion: $('input#opendiscussion').is(':checked') ? 1 : 0,
334+
syntaxcoloring: $('input#syntaxcoloring').is(':checked') ? 1 : 0
316335
};
317336
$.post(scriptLocation(), data_to_send, 'json')
318337
.error(function() {
@@ -332,6 +351,10 @@ function send_data() {
332351

333352
setElementText($('div#cleartext'), $('textarea#message').val());
334353
urls2links($('div#cleartext'));
354+
355+
// FIXME: Add option to remove syntax highlighting ?
356+
if ($('input#syntaxcoloring').is(':checked')) applySyntaxColoring();
357+
335358
showStatus('');
336359
}
337360
else if (data.status==1) {
@@ -376,6 +399,7 @@ function stateNewPaste() {
376399
$('div#language').hide(); // $('#language').show();
377400
$('input#password').hide(); //$('#password').show();
378401
$('div#opendisc').show();
402+
$('div#syntaxcoloringoption').show();
379403
$('button#newbutton').show();
380404
$('div#pasteresult').hide();
381405
$('textarea#message').text('');
@@ -403,6 +427,7 @@ function stateExistingPaste() {
403427
$('div#language').hide();
404428
$('input#password').hide();
405429
$('div#opendisc').hide();
430+
$('div#syntaxcoloringoption').hide();
406431
$('button#newbutton').show();
407432
$('div#pasteresult').hide();
408433
$('textarea#message').hide();

tpl/page.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<script src="js/rawdeflate.js#"></script>
99
<script src="js/rawinflate.js#"></script>
1010
<script src="js/zerobin.js?{$VERSION|rawurlencode}#"></script>
11+
<link type="text/css" rel="stylesheet" href="js/highlight.styles/monokai.css#">
12+
<script src="js/highlight.pack.js#"></script>
1113

1214
<!--[if lt IE 10]>
1315
<style> body {padding-left:60px;padding-right:60px;} div#ienotice {display:block;} </style>
@@ -68,6 +70,10 @@ <h3>{$VERSION}</h3>
6870
<input type="checkbox" id="opendiscussion" name="opendiscussion" />
6971
<label for="opendiscussion">Open discussion</label>
7072
</div>
73+
<div id="syntaxcoloringoption" class="button" style="display:none;">
74+
<input type="checkbox" id="syntaxcoloring" name="syntaxcoloring" />
75+
<label for="syntaxcoloring">Syntax coloring</label>
76+
</div>
7177
</div>
7278
<div id="pasteresult" style="display:none;">
7379
<div id="deletelink"></div>

0 commit comments

Comments
 (0)