From: Alexandre Terrasa Date: Fri, 21 Feb 2014 05:02:20 +0000 (-0500) Subject: nitdoc: better display of code in comments thanks to highlight.js X-Git-Tag: v0.6.4~2^2~1 X-Git-Url: http://nitlanguage.org nitdoc: better display of code in comments thanks to highlight.js Signed-off-by: Alexandre Terrasa --- diff --git a/share/nitdoc/css/Nitdoc.GitHub.css b/share/nitdoc/css/Nitdoc.GitHub.css index ee847cc..bba519c 100644 --- a/share/nitdoc/css/Nitdoc.GitHub.css +++ b/share/nitdoc/css/Nitdoc.GitHub.css @@ -253,3 +253,54 @@ a.nitdoc-github-update { color: orange; cursor: pointer; } + +.nitdoc-dialog h4 { + font-weight: bold; +} + +/* hljs */ + +.hljs.nitcode { + padding-left: 10px; +} + +.hljs-comment, .hljs-comment-block { + color: #777; +} + +.hljs-keyword { + color: #000; + font-weight: bold; +} + +.hljs-title { + font-weight: bold; +} + +.hljs-module { + color: #3762E4; +} + +.hljs-class .hljs-title { + color: #3762E4; +} + +.hljs-type { + color: #3762E4; +} + +.hljs-string { + color: #8F1546; +} + +.hljs-subst { + color: #9E6BEB; +} + +.hljs-fun .hljs-title { + color: #3762E4; +} + +.hljs-char, .hljs-number { + color: #009999; +} diff --git a/share/nitdoc/js/lib/nit.js b/share/nitdoc/js/lib/nit.js new file mode 100644 index 0000000..eac858c --- /dev/null +++ b/share/nitdoc/js/lib/nit.js @@ -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 + }; + }); +}); diff --git a/share/nitdoc/js/plugins/github.js b/share/nitdoc/js/plugins/github.js index d292c69..84ad4f2 100644 --- a/share/nitdoc/js/plugins/github.js +++ b/share/nitdoc/js/plugins/github.js @@ -24,12 +24,14 @@ define([ "jquery", "github-api", + "highlight", "marked", + "nit", "plugins/modalbox", "plugins/github/loginbox", "plugins/github/commentbox", - "utils", -], function($, GithubAPI) { + "utils" +], function($, GithubAPI, hljs, marked) { var GithubUser = function(login, password, repo, branch) { this.login = login; this.password = password; @@ -485,7 +487,16 @@ define([ /* internals */ + _initMarked: function() { + var renderer = new marked.Renderer(); + renderer.code = function(code) { + return '
' + hljs.highlight('nit', code).value + '
'; + } + renderer.codespan = function(code) { + return '' + hljs.highlight('nit', code).value + ''; + } marked.setOptions({ + renderer: renderer, gfm: true, tables: true, breaks: true,