-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplesmartypants.user.js
57 lines (43 loc) · 1.87 KB
/
simplesmartypants.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// ==UserScript==
// @name Simple SmartyPants
// @namespace http://www.aditya-mukherjee.com
// @description Derives base idea from SmartyPants, but is much simpler in implementation I'm guessing.
// @include *.blogger.com/post-*
// ==/UserScript==
/*
Licensed to Aditya Mukherjee under Creative Commons 2.5 Works
<http://creativecommons.org/licenses/by-nc-nd/2.5/>
*/
function $()
{
for( var i = 0, node; i < arguments.length; i++ )
if( node = document.getElementById( arguments[i] ) )
return node;
}
function smartyPants(){
function smarty(text){
text = text.replace(/'(?=\d)/g, '‘'); //left curly before any digit
text = text.replace(/\B'(?=\w)/g, '‘'); //left curly before any letter when preceded by a space
text = text.replace(/(\S)'(?=\w)*/g, '$1’'); //right curly when followed by alphanumeric, space or new line
text = text.replace(/^"(?=\w)/g, '“'); //1 for double
text = text.replace(/"$|(\S)"/g, '$1”'); //2 for double
text = text.replace(/\.\.\./g, '…'); //ellipses
text = text.replace(/-{1,2}/g, '–'); //en dash
text = text.replace(/-{3}/g, '—'); //em dash
return text;
}//nested function to keep alive
ta = $('textarea').value;
GM_log(ta);
ta = ta.replace(/(<([^>]+)>)/ig,"");
words = ta.split(' ');
for(i=0;i<words.length;i++){
GM_log(words[i]+' -- '+smarty(words[i]));
$('textarea').value = $('textarea').value.replace(words[i], smarty(words[i]));
}
}
smarty = document.createElement('span');
smarty.setAttribute('style','color:black;border-bottom:1px solid #BDB76b');
smarty.id = 'htmlbar_Smarty';
smarty.innerHTML = 'Smarty';
smarty.addEventListener('click', function(){smartyPants();}, true);
window.addEventListener('load',function(){$('htmlbar').insertBefore(smarty, $('htmlbar_PreviewAction'));}, true);