prism-javadoclike.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function (Prism) {
  2. var javaDocLike = Prism.languages.javadoclike = {
  3. 'parameter': {
  4. pattern: /(^[\t ]*(?:\/{3}|\*|\/\*\*)\s*@(?:arg|arguments|param)\s+)\w+/m,
  5. lookbehind: true
  6. },
  7. 'keyword': {
  8. // keywords are the first word in a line preceded be an `@` or surrounded by curly braces.
  9. // @word, {@word}
  10. pattern: /(^[\t ]*(?:\/{3}|\*|\/\*\*)\s*|\{)@[a-z][a-zA-Z-]+\b/m,
  11. lookbehind: true
  12. },
  13. 'punctuation': /[{}]/
  14. };
  15. /**
  16. * Adds doc comment support to the given language and calls a given callback on each doc comment pattern.
  17. *
  18. * @param {string} lang the language add doc comment support to.
  19. * @param {(pattern: {inside: {rest: undefined}}) => void} callback the function called with each doc comment pattern as argument.
  20. */
  21. function docCommentSupport(lang, callback) {
  22. var tokenName = 'doc-comment';
  23. var grammar = Prism.languages[lang];
  24. if (!grammar) {
  25. return;
  26. }
  27. var token = grammar[tokenName];
  28. if (!token) {
  29. // add doc comment: /** */
  30. var definition = {};
  31. definition[tokenName] = {
  32. pattern: /(^|[^\\])\/\*\*[^/][\s\S]*?(?:\*\/|$)/,
  33. lookbehind: true,
  34. alias: 'comment'
  35. };
  36. grammar = Prism.languages.insertBefore(lang, 'comment', definition);
  37. token = grammar[tokenName];
  38. }
  39. if (token instanceof RegExp) { // convert regex to object
  40. token = grammar[tokenName] = { pattern: token };
  41. }
  42. if (Array.isArray(token)) {
  43. for (var i = 0, l = token.length; i < l; i++) {
  44. if (token[i] instanceof RegExp) {
  45. token[i] = { pattern: token[i] };
  46. }
  47. callback(token[i]);
  48. }
  49. } else {
  50. callback(token);
  51. }
  52. }
  53. /**
  54. * Adds doc-comment support to the given languages for the given documentation language.
  55. *
  56. * @param {string[]|string} languages
  57. * @param {Object} docLanguage
  58. */
  59. function addSupport(languages, docLanguage) {
  60. if (typeof languages === 'string') {
  61. languages = [languages];
  62. }
  63. languages.forEach(function (lang) {
  64. docCommentSupport(lang, function (pattern) {
  65. if (!pattern.inside) {
  66. pattern.inside = {};
  67. }
  68. pattern.inside.rest = docLanguage;
  69. });
  70. });
  71. }
  72. Object.defineProperty(javaDocLike, 'addSupport', { value: addSupport });
  73. javaDocLike.addSupport(['java', 'javascript', 'php'], javaDocLike);
  74. }(Prism));