/static/scripts/tiny_mce/plugins/bbcode/editor_plugin_src.js

http://n23.googlecode.com/ · JavaScript · 117 lines · 90 code · 15 blank · 12 comment · 2 complexity · fe25dc04a1e88b7738a11bb0c647dba6 MD5 · raw file

  1. /**
  2. * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright Š 2004-2008, Moxiecode Systems AB, All rights reserved.
  6. */
  7. (function() {
  8. tinymce.create('tinymce.plugins.BBCodePlugin', {
  9. init : function(ed, url) {
  10. var t = this, dialect = ed.getParam('bbcode_dialect', 'punbb').toLowerCase();
  11. ed.onBeforeSetContent.add(function(ed, o) {
  12. o.content = t['_' + dialect + '_bbcode2html'](o.content);
  13. });
  14. ed.onPostProcess.add(function(ed, o) {
  15. if (o.set)
  16. o.content = t['_' + dialect + '_bbcode2html'](o.content);
  17. if (o.get)
  18. o.content = t['_' + dialect + '_html2bbcode'](o.content);
  19. });
  20. },
  21. getInfo : function() {
  22. return {
  23. longname : 'BBCode Plugin',
  24. author : 'Moxiecode Systems AB',
  25. authorurl : 'http://tinymce.moxiecode.com',
  26. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/bbcode',
  27. version : tinymce.majorVersion + "." + tinymce.minorVersion
  28. };
  29. },
  30. // Private methods
  31. // HTML -> BBCode in PunBB dialect
  32. _punbb_html2bbcode : function(s) {
  33. s = tinymce.trim(s);
  34. function rep(re, str) {
  35. s = s.replace(re, str);
  36. };
  37. // example: <strong> to [b]
  38. rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
  39. rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
  40. rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
  41. rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[code][color=$1]$2[/color][/code]");
  42. rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[quote][color=$1]$2[/color][/quote]");
  43. rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,"[color=$1]$2[/color]");
  44. rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,"[color=$1]$2[/color]");
  45. rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
  46. rep(/<font>(.*?)<\/font>/gi,"$1");
  47. rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
  48. rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi,"[code]$1[/code]");
  49. rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi,"[quote]$1[/quote]");
  50. rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi,"[code][b]$1[/b][/code]");
  51. rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,"[quote][b]$1[/b][/quote]");
  52. rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi,"[code][i]$1[/i][/code]");
  53. rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi,"[quote][i]$1[/i][/quote]");
  54. rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi,"[code][u]$1[/u][/code]");
  55. rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi,"[quote][u]$1[/u][/quote]");
  56. rep(/<\/(strong|b)>/gi,"[/b]");
  57. rep(/<(strong|b)>/gi,"[b]");
  58. rep(/<\/(em|i)>/gi,"[/i]");
  59. rep(/<(em|i)>/gi,"[i]");
  60. rep(/<\/u>/gi,"[/u]");
  61. rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,"[u]$1[/u]");
  62. rep(/<u>/gi,"[u]");
  63. rep(/<blockquote[^>]*>/gi,"[quote]");
  64. rep(/<\/blockquote>/gi,"[/quote]");
  65. rep(/<br \/>/gi,"\n");
  66. rep(/<br\/>/gi,"\n");
  67. rep(/<br>/gi,"\n");
  68. rep(/<p>/gi,"");
  69. rep(/<\/p>/gi,"\n");
  70. rep(/&nbsp;/gi," ");
  71. rep(/&quot;/gi,"\"");
  72. rep(/&lt;/gi,"<");
  73. rep(/&gt;/gi,">");
  74. rep(/&amp;/gi,"&");
  75. return s;
  76. },
  77. // BBCode -> HTML from PunBB dialect
  78. _punbb_bbcode2html : function(s) {
  79. s = tinymce.trim(s);
  80. function rep(re, str) {
  81. s = s.replace(re, str);
  82. };
  83. // example: [b] to <strong>
  84. rep(/\n/gi,"<br />");
  85. rep(/\[b\]/gi,"<strong>");
  86. rep(/\[\/b\]/gi,"</strong>");
  87. rep(/\[i\]/gi,"<em>");
  88. rep(/\[\/i\]/gi,"</em>");
  89. rep(/\[u\]/gi,"<u>");
  90. rep(/\[\/u\]/gi,"</u>");
  91. rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
  92. rep(/\[url\](.*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
  93. rep(/\[img\](.*?)\[\/img\]/gi,"<img src=\"$1\" />");
  94. rep(/\[color=(.*?)\](.*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
  95. rep(/\[code\](.*?)\[\/code\]/gi,"<span class=\"codeStyle\">$1</span>&nbsp;");
  96. rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<span class=\"quoteStyle\">$1</span>&nbsp;");
  97. return s;
  98. }
  99. });
  100. // Register plugin
  101. tinymce.PluginManager.add('bbcode', tinymce.plugins.BBCodePlugin);
  102. })();