plugin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Rich code snippets for CKEditor.
  7. */
  8. 'use strict';
  9. (function() {
  10. // Create a new plugin which registers a custom code highlighter
  11. // based on Prism.js in order to replace the one that comes
  12. // with the Code Snippet plugin.
  13. CKEDITOR.plugins.add('prism', {
  14. requires: 'codesnippet',
  15. init: function(editor) {
  16. var path = this.path;
  17. // Loading the prism.js style file.
  18. // Idea taken from codesnippet/plugin.js code.
  19. // Method is available only if wysiwygarea exists and
  20. // CKEditor is at least version 4.4.
  21. if (editor.addContentsCss) {
  22. editor.addContentsCss(path + 'lib/prism/prism_patched.min.css');
  23. }
  24. // Create a new instance of the highlighter.
  25. var prismHighlighter = new CKEDITOR.plugins.codesnippet.highlighter({
  26. init: function(ready) {
  27. // Load the Prism.js library asynchronously.
  28. CKEDITOR.scriptLoader.load(path + 'lib/prism/prism_patched.min.js', function() {
  29. // Notify the handler that the library has been loaded.
  30. ready();
  31. });
  32. },
  33. // Specify the supported languages.
  34. languages: {
  35. abap: 'ABAP',
  36. actionscript: 'ActionScript',
  37. apacheconf: 'Apache Conf',
  38. applescript: 'AppleScript',
  39. aspnet: 'ASP.NET',
  40. bash: 'Bash',
  41. basic: 'BASIC',
  42. c: 'C',
  43. coffeescript: 'CoffeeScript',
  44. cpp: 'C++',
  45. csharp: 'C#',
  46. css: 'CSS',
  47. d: 'D',
  48. dart: 'Dart',
  49. diff: 'Diff',
  50. docker: 'Docker',
  51. erlang: 'Erlang',
  52. fortran: 'Fortran',
  53. fsharp: 'F#',
  54. git: 'Git',
  55. go: 'Go',
  56. groovy: 'Groovy',
  57. haskell: 'Haskell',
  58. markup: 'HTML',
  59. http: 'HTTP',
  60. ini: 'INI',
  61. java: 'Java',
  62. javascript: 'JavaScript',
  63. lua: 'Lua',
  64. makefile: 'Makefile',
  65. markdown: 'Markdown',
  66. matlab: 'MATLAB',
  67. nginx: 'Nginx',
  68. objectivec: 'Objective-C',
  69. pascal: 'Pascal',
  70. perl: 'Perl',
  71. php: 'PHP',
  72. prolog: 'Prolog',
  73. python: 'Python',
  74. puppet: 'Puppet',
  75. r: 'R',
  76. ruby: 'Ruby',
  77. rust: 'Rust',
  78. sas: 'SAS',
  79. scala: 'Scala',
  80. scheme: 'Scheme',
  81. sql: 'SQL',
  82. swift: 'Swift',
  83. twig: 'Twig',
  84. vim: 'vim',
  85. yaml: 'YAML',
  86. },
  87. highlighter: function(code, language, callback) {
  88. // _self.Prism is a global namespace/object created by Prism.js.
  89. var _prism = _self.Prism;
  90. // Let the Prism.js highlight the code.
  91. var highlightedCode = _prism.highlight(code, _prism.languages[language], language);
  92. // The clever idea below is taken from the 'Line Numbers' plugin
  93. // of Prism. Basically, we want to count the number of newlines (\n)
  94. // in the highlighted code, then create the same number
  95. // of SPAN elements, append them to the highlighted code
  96. // and finally number/label them using prism.css.
  97. var match = highlightedCode.match(/\n(?!$)/g);
  98. var linesNum = match ? match.length + 1 : 1;
  99. var lines = new Array(linesNum + 1);
  100. lines = lines.join('<span></span>');
  101. // Create the SPAN root/wrapper, insert its child SPAN lines,
  102. // then append them to the highlighted code.
  103. var lineNumbersWrapper = '<span class="line-numbers-rows">';
  104. lineNumbersWrapper += lines;
  105. lineNumbersWrapper += '</span>';
  106. highlightedCode += lineNumbersWrapper;
  107. // Return highlighted code.
  108. callback(highlightedCode);
  109. }
  110. });
  111. // From now on, prismHighlighter will be used as a Code Snippet
  112. // highlighter, overwriting the default engine.
  113. editor.plugins.codesnippet.setHighlighter(prismHighlighter );
  114. }
  115. });
  116. })();