1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Try code detection, and if detected has plain text,, do not apply code coloration

This commit is contained in:
sam 2012-05-22 19:10:54 +02:00
parent a824bc2a21
commit 7b8108ea6b
3 changed files with 56 additions and 9 deletions

View File

@ -181,9 +181,7 @@ input.hide-upload {
} }
#paste-content.done { #paste-content.linenums {
background-color: white;
padding-top:1em;
padding-left:0; padding-left:0;
} }
@ -360,3 +358,7 @@ canvas {
#faq p { #faq p {
margin:1em; margin:1em;
} }
#force-coloration {
text-decoration:underline;
}

View File

@ -302,7 +302,6 @@ window.zerobin = {
}, },
/** Create a message, style it and insert it in the alert box */ /** Create a message, style it and insert it in the alert box */
message: function(type, message, title, flush, callback) { message: function(type, message, title, flush, callback) {
$(window).scrollTop(0); $(window).scrollTop(0);
if (flush) {$('.alert-'+type).remove();} if (flush) {$('.alert-'+type).remove();}
@ -323,6 +322,28 @@ window.zerobin = {
var bar = {container: $container, elem: $container.find('.bar')}; var bar = {container: $container, elem: $container.find('.bar')};
bar.set = function(text, rate){bar.elem.text(text).css('width', rate);}; bar.set = function(text, rate){bar.elem.text(text).css('width', rate);};
return bar; return bar;
},
/** Return an integer ranking the probability this text is any kind of
source code. */
isCode: function(text){
var code_chars = /[A-Z]{3}[A-Z]+|\.[a-z]|[=:<>{}\[\]$_'"&]| {2}|\t/g;
var comment = /(\/\*|\/\/|#|;).*$/g;
var total = 0;
var size = 0;
text = text.split('\n');
for (var i = 0; i < text.length; i++) {
var line = text[i];
size += line.length;
var match = line.replace(comment, '').match(code_chars);
if (match) {
total += match.length;
}
}
return total * 1000 / size;
} }
}; };
@ -435,6 +456,7 @@ $('.btn-primary').live("click", function(e){
var content = $('#paste-content').text().trim(); var content = $('#paste-content').text().trim();
var key = zerobin.getPasteKey(); var key = zerobin.getPasteKey();
var error = false; var error = false;
if (content && key) { if (content && key) {
/* Load the lib for visual canvas, create one from the paste id and /* Load the lib for visual canvas, create one from the paste id and
@ -476,7 +498,6 @@ if (content && key) {
/* Decrypted content goes back to initial container*/ /* Decrypted content goes back to initial container*/
$('#paste-content').text(content); $('#paste-content').text(content);
content = '';
bar.set('Code coloration...', '95%'); bar.set('Code coloration...', '95%');
@ -522,13 +543,23 @@ if (content && key) {
/* Remap the message close handler to include the clipboard /* Remap the message close handler to include the clipboard
flash reposition */ flash reposition */
$(".close").off().live('click', function(e){ $(".close").die().live('click', function(e){
e.preventDefault(); e.preventDefault();
$(this).parent().fadeOut(reposition); $(this).parent().fadeOut(reposition);
}); });
/** Syntaxic coloration */ /** Syntaxic coloration */
prettyPrint();
if (zerobin.isCode(content) > 100){
$('#paste-content').addClass('linenums');
prettyPrint();
} else {
zerobin.message('info',
"The paste didn't not seem to be code, so it " +
"was not colorized. " +
"<a id='force-coloration' href='#'>Force coloration</a>",
'', true, reposition);
}
/* Class to switch to paste content style with coloration done */ /* Class to switch to paste content style with coloration done */
$('#paste-content').addClass('done'); $('#paste-content').addClass('done');
@ -538,6 +569,7 @@ if (content && key) {
bar.container.hide(); bar.container.hide();
$form.prop('disabled', false); $form.prop('disabled', false);
content = '';
}, 250); }, 250);
@ -599,7 +631,10 @@ if (zerobin.support.localStorage){
// does redirect to the page // does redirect to the page
if (paste.link.replace(/#[^#]+/, '') === window.location.pathname){ if (paste.link.replace(/#[^#]+/, '') === window.location.pathname){
$li.addClass('active'); $li.addClass('active');
$link.click(function(){window.location.reload();}); $link.click(function(){
window.location = $link.attr('href');
window.location.reload();
});
} }
}); });
@ -693,6 +728,16 @@ if (zerobin.support.history && zerobin.paste_not_found){
} }
/* Force text coloration when clickin on link */
$("#force-coloration").live("click", function(e) {
e.preventDefault();
$('#paste-content').addClass('linenums');
$(this).die(e).text('Applying coloration');
prettyPrint();
$(this).remove();
});
}); /* End of "document ready" jquery callback */ }); /* End of "document ready" jquery callback */
})(); /* End of self executing function */ })(); /* End of self executing function */

View File

@ -37,7 +37,7 @@
</div> </div>
<p> <p>
<pre id="paste-content" class="prettyprint linenums"> <pre id="paste-content" class="prettyprint">
<code> <code>
{{ paste.content }} {{ paste.content }}
</code> </code>