plugin.js 4.1 KB

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