Advertisement
Guest User

Shock

a guest
Aug 18th, 2009
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function(){
  3.     var trim = function (string) {
  4.         return string.replace(/(^\s+)|(\s+$)/g, '');
  5.     }
  6.     var re = {
  7.         size  : /^\d+(em|px|%)$/i,
  8.         size4 : /^(\d+(em|px|%) ?){1,4}$/i,
  9.         borderStyle : /^(dotted|dashed|solid|double|groove|ridge|inset|outset|none)$/i,
  10.         colorNames  : /^(aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|purple|red|silver|teal|white|yellow|cyan|magenta)$/,
  11.         rgbColor    : /^(rgb\(\d{1,3}, ?\d{1,3}, ?\d{1,3}\))$/,
  12.         hexColor    : /^(#[a-f0-9]{3}|#[a-f0-9]{6})$/i
  13.     };
  14.     var isColor = function (string) {
  15.         return string.match(re.hexColor) || string.match(re.rgbColor) || string.match(re.colorNames);
  16.     };
  17.     var lib = {
  18.         validStyles : {
  19.             'text-align' : /^center|justify|left|right$/i,
  20.             'border' : function (arg) {
  21.                 var parts = arg.split(' ');
  22.                 var borderStyle = 'none';
  23.                 var borderColor = 'black';
  24.                 var borderWidth = '0';
  25.                 for (var i in parts) {
  26.                     if (parts[i].match(re.borderStyle)) {
  27.                         borderStyle = parts[i].toLowerCase();
  28.                     } else if (parts[i].match(re.size)) {
  29.                         borderWidth = parts[i].toLowerCase();
  30.                     } else if (isColor(parts[i])) {
  31.                         borderColor = parts[i].toLowerCase();
  32.                     }
  33.                 }
  34.  
  35.                 return [borderStyle, borderColor, borderWidth].join(' ');
  36.             },
  37.             'background' : function (arg) {
  38.                 return isColor(arg) ? arg.toLowerCase() : null;
  39.             },
  40.             'display' : /^(block|inline|none)$/i,
  41.             'padding' : re.size4,
  42.             'margin'  : re.size4,
  43.             'float'   : /^(left|right)$/,
  44.             'clear'   : /^(both|left|right)$/,
  45.             'width'   : re.size4,
  46.             'height'  : re.size4
  47.         }
  48.     };
  49.     TeX.addTag({
  50.         name : 'style',
  51.         args : 'unlimited',
  52.         func : function (args) {
  53.             var text = args.pop();
  54.             var allStyles = lib.validStyles;
  55.             var result = '', value;
  56.             for (var style in args) {
  57.                 style = trim(args[style]).split(':');
  58.                 if (style.length != 2) {
  59.                     continue;
  60.                 }
  61.                 var name = style[0].toLowerCase();
  62.                 var cur  = allStyles[name];
  63.                 if (cur) {
  64.                     var type = typeof cur;
  65.                     if (type === 'function') {
  66.                         value = cur(style[1]);
  67.                     } else if (style[1].match(cur)) {
  68.                         value = style[1];
  69.                     }
  70.                     if (value) {
  71.                         result += name + ':' + value + ';';
  72.                     }
  73.                 }
  74.                 value = '';
  75.             }
  76.             return '<span style="' + result + '">' + TeX.markup(text) + '</span>';
  77.         }
  78.     });
  79.  
  80. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement