nitdoc: better display of code in comments thanks to highlight.js
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 21 Feb 2014 05:02:20 +0000 (00:02 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Feb 2014 18:28:09 +0000 (13:28 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

share/nitdoc/css/Nitdoc.GitHub.css
share/nitdoc/js/lib/nit.js [new file with mode: 0644]
share/nitdoc/js/plugins/github.js

index ee847cc..bba519c 100644 (file)
@@ -253,3 +253,54 @@ a.nitdoc-github-update {
        color: orange;\r
        cursor: pointer;\r
 }\r
+\r
+.nitdoc-dialog h4 {\r
+       font-weight: bold;\r
+}\r
+\r
+/* hljs */\r
+\r
+.hljs.nitcode {\r
+       padding-left: 10px;\r
+}\r
+\r
+.hljs-comment, .hljs-comment-block {\r
+       color: #777;\r
+}\r
+\r
+.hljs-keyword {\r
+       color: #000;\r
+       font-weight: bold;\r
+}\r
+\r
+.hljs-title {\r
+       font-weight: bold;\r
+}\r
+\r
+.hljs-module {\r
+       color: #3762E4;\r
+}\r
+\r
+.hljs-class .hljs-title {\r
+       color: #3762E4;\r
+}\r
+\r
+.hljs-type {\r
+       color: #3762E4;\r
+}\r
+\r
+.hljs-string {\r
+       color: #8F1546;\r
+}\r
+\r
+.hljs-subst {\r
+       color: #9E6BEB;\r
+}\r
+\r
+.hljs-fun .hljs-title {\r
+       color: #3762E4;\r
+}\r
+\r
+.hljs-char, .hljs-number {\r
+       color: #009999;\r
+}\r
diff --git a/share/nitdoc/js/lib/nit.js b/share/nitdoc/js/lib/nit.js
new file mode 100644 (file)
index 0000000..eac858c
--- /dev/null
@@ -0,0 +1,146 @@
+/* This file is part of NIT ( http://www.nitlanguage.org ).
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+   Documentation generator for the nit language.
+   Generate API documentation in HTML format from nit source code.
+*/
+
+define(['highlight'], function(hljs) {
+       hljs.registerLanguage('nit', function(hljs) {
+var METHOD_RE = '[a-z_]\\w*[!?=]?|[-+~]\\@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?';
+       var KEYWORDS = {
+               keyword: 'abort abstract and as assert break class continue do else end enum extern for fun ' +
+               'if import in init interface intern intrude is isa isset label loop module new nullable not ' +
+               'once or protected private redef return self super then type universal var ' +
+               'when while writable',
+               literal: "true false null"
+       };
+       var COMMENT = {
+               className: 'comment',
+               begin: '#', end: '$',
+       };
+       var SUBST = {
+               className: 'subst',
+               begin: '{', end: '}',
+               keywords: KEYWORDS
+       };
+       var STRING = {
+               className: 'string',
+               contains: [hljs.BACKSLASH_ESCAPE, SUBST],
+               variants: [
+                       {begin: /"/, end: /"/},
+               ]
+       };
+       var CHAR = {
+               className: 'char',
+               contains: [hljs.BACKSLASH_ESCAPE, SUBST],
+               begin: /'/, end: /'/,
+       };
+       var TYPE = {
+               className: 'type',
+               begin: '[A-Z]\\w*'
+       }
+       var PARAMS = {
+               className: 'params',
+               begin: '\\(', end: '\\)',
+               keywords: KEYWORDS,
+               contains: [TYPE]
+       };
+       var RET_TYPE = {
+               className: 'rettype',
+               begin: ':', end: '$|do|is|=',
+               keywords: 'nullable',
+               returnEnd: true,
+               contains: [TYPE]
+       }
+       var DO_BLOCK = {
+               className: 'block',
+               begin: 'do', end: '$|end',
+               keywords: KEYWORDS
+       }
+       var IS_BLOCK = {
+               className: 'modifiers',
+               begin: 'is', end: '$',
+               keywords: KEYWORDS
+       }
+       var CONTAINS = [
+               STRING,
+               CHAR,
+               COMMENT,
+               TYPE,
+               {
+                       className: 'module',
+                       beginKeywords: 'module', end: '$',
+                       contains: [
+                               hljs.inherit(hljs.TITLE_MODE, {begin: '[a-z_]\\w*'}),
+                               COMMENT
+                       ]
+               },
+               {
+                       className: 'import',
+                       begin: '(intrude )?import', end: '$',
+                       keywords: 'intrude import',
+                       contains: [
+                               {
+                                       className: 'module',
+                                       begin: '[a-z_]\\w*'
+                               },
+                               COMMENT
+                       ]
+               },
+               {
+                       className: 'class',
+                       begin: '(redef |private |protected )?(abstract )?(class|interface)', end: '$',
+                       keywords: 'redef private protected abstract class interface',
+                       contains: [
+                               hljs.inherit(hljs.TITLE_MODE, {begin: '[A-Z]\\w*'}),
+                               {
+                                       className: 'super',
+                                       begin: '\\bsuper', end: '$',
+                                       keywords: 'super',
+                                       contains: [TYPE]
+                               },
+                               COMMENT
+                       ]
+               },
+               {
+                       className: 'fun',
+                       begin: '(redef |private |protected )?(fun|init|type)\\b', end: '$',
+                       keywords: KEYWORDS,
+                       contains: [
+                               PARAMS,
+                               RET_TYPE,
+                               DO_BLOCK,
+                               IS_BLOCK,
+                               {
+                                       className: 'title',
+                                       begin: '\\b[a-zA-Z_][a-zA-Z_]*\\b'
+                               },
+                               COMMENT
+                       ]
+               },
+               {
+                       className: 'number',
+                       begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b',
+                       relevance: 0
+               }
+       ];
+       SUBST.contains = CONTAINS;
+
+       return {
+               keywords: KEYWORDS,
+               contains: CONTAINS
+       };
+       });
+});
index d292c69..84ad4f2 100644 (file)
 define([\r
        "jquery",\r
        "github-api",\r
+       "highlight",\r
        "marked",\r
+       "nit",\r
        "plugins/modalbox",\r
        "plugins/github/loginbox",\r
        "plugins/github/commentbox",\r
-       "utils",\r
-], function($, GithubAPI) {\r
+       "utils"\r
+], function($, GithubAPI, hljs, marked) {\r
        var GithubUser = function(login, password, repo, branch) {\r
                this.login = login;\r
                this.password = password;\r
@@ -485,7 +487,16 @@ define([
 \r
                /* internals */\r
 \r
+               _initMarked: function() {\r
+                       var renderer = new marked.Renderer();\r
+                       renderer.code = function(code) {\r
+                               return '<pre class="nitcode hljs">' + hljs.highlight('nit', code).value + '</pre>';\r
+                       }\r
+                       renderer.codespan = function(code) {\r
+                               return '<code class="nitcode hljs">' + hljs.highlight('nit', code).value + '</code>';\r
+                       }\r
                        marked.setOptions({\r
+                               renderer: renderer,\r
                                gfm: true,\r
                                tables: true,\r
                                breaks: true,\r