Merge: Nitweb snippets
authorJean Privat <jean@pryen.org>
Thu, 23 Jun 2016 20:21:37 +0000 (16:21 -0400)
committerJean Privat <jean@pryen.org>
Thu, 23 Jun 2016 20:21:37 +0000 (16:21 -0400)
Documentation of this PR is [here](http://nitweb.moz-code.org/docdown?snippet=IyBIZWxsbyBHaXRodWIKClRoaXMgaXMgYSBkb2N1bWVudGF0aW9uIHNuaXBwZXQuCllvdSBjYW4gdXNlIGl0IHRvIGRvY3VtZW50IHRoaW5ncyBvbiB0aGUgZmx5IGFuZCBzaGFyZSBpdCB3aXRoIG90aGVyIE5pdCB1c2Vycy4KCiMjIEJhc2ljcwoKQmFzaWNhbGx5LCBEb2Nkb3duIGFsbG93cyB5b3UgdG8gd3JpdGUgbWFyZG93biBkb2N1bWVudGF0aW9uIHVzaW5nIHRoZSBbbWFya2Rvd25dKGh0dHBzOi8vZGFyaW5nZmlyZWJhbGwubmV0L3Byb2plY3RzL21hcmtkb3duLykgZm9ybWF0LgoKU29tZSBleGFtcGxlczoKCn5%2BfnJhdwoqIHRoaXMKKiBpcyBhCiogbGlzdAp%2Bfn4KCj4gVGhpcyBpcyBhIGNpdGF0aW9uLgoKIyMgV3JpdGluZyBuaXQgY29kZQoKRG9jZG93biBkZXBlbmRzIG9uIHRoZSBuaXQgaGlnaGxpZ2h0ZXIgc28geW91IGNhbiB3cml0ZSBOaXQgd2l0aGluIHlvdXIgZG9jIHNuaXBwZXQ6Cgp%2Bfn4KcHJpbnQgImhlbGxvIHdvcmxkIgp%2Bfn4KCiMjIExpbmtpbmcgbW9kZWwgZW50aXRpZXMKCkRvY2Rvd24gdXNlcyBgW1t3aWtpbGlua3NdXWAgdG8gZW5oYW5jZSB0aGUgbWFya2Rvd24gbGFuZ3VhZ2UgZm9yIGRvY3VtZW50YXRpb24uCgpVc2luZyB3aWtpbGlua3MgeW91IGNhbiBjcmVhdGUgbGlua3MgdG8gdGhlIGRvY3VtZW50YXRpb24gb2YgYSBNRW50aXR5IGxpa2UgdG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vdXIgc25pcHBldCBmb3IgaW5jb25zaXN0ZW5jaWVzLgoKSXQgaXMgYWJsZSB0byBsb2NhdGUgdW5rbm93biBjb21tYW5kczoKCn5%2BfnJhdwpbW2xvbDogbG9sXV0Kfn5%2BCgpbW2xvbDogbG9sXV0KClVua25vd24gZW50aXRpZXM6Cgp%2Bfn5yYXcKW1tkb2M6IGxvbF1dCn5%2BfgoKW1tkb2M6IGxvbF1dCgpBbmQgZXZlbiB3YXJuIHlvdSB3aGVuIGluY2x1ZGluZyBlbXB0eSBkb2N1bWVudGF0aW9uOgoKfn5%2BcmF3CltbZG9jOiBjb3JlJENvbXBhcmFibGUkT2JqZWN0OjpTRUxGXV0Kfn5%2BCgpbW2RvYzogY29yZSRDb21wYXJhYmxlJE9iamVjdDo6U0VMRl1d).

PS: Review only the last 3 commits, others are from #2193.

Pull-Request: #2194
Reviewed-by: Jean Privat <jean@pryen.org>

share/nitweb/index.html
share/nitweb/javascripts/docdown.js [new file with mode: 0644]
share/nitweb/javascripts/model.js
share/nitweb/javascripts/nitweb.js
share/nitweb/views/docdown.html [new file with mode: 0644]
src/doc/doc_commands.nit
src/nitweb.nit
src/web/api_docdown.nit [new file with mode: 0644]
src/web/web.nit

index 337750b..c615d41 100644 (file)
        <body>
                <nav class='navbar navbar-default navbar-fixed-top'>
                        <div class='container-fluid'>
-                               <div class='col-xs-3 navbar-header'>
-                                       <a class='navbar-brand' ng-href='/'>Nitdoc</a>
+                               <div class='col-xs-3'>
+                                       <div class='navbar-header'>
+                                               <a class='navbar-brand' ng-href='/'>Nitdoc</a>
+                                       </div>
+                                       <ul class="nav navbar-nav">
+                                               <li><a href="/docdown?edit=true">DocDown</a></li>
+                                       </ul>
                                </div>
                                <div class='col-xs-7'>
                                        <form ng-controller='SearchCtrl as searchCtrl' >
@@ -64,5 +69,6 @@
                <script src='/javascripts/entities.js'></script>
                <script src='/javascripts/ui.js'></script>
                <script src='/javascripts/index.js'></script>
+               <script src='/javascripts/docdown.js'></script>
        </body>
 </html>
diff --git a/share/nitweb/javascripts/docdown.js b/share/nitweb/javascripts/docdown.js
new file mode 100644 (file)
index 0000000..365b8c9
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.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.
+ */
+
+(function() {
+       angular
+               .module('docdown', ['model', 'ngSanitize'])
+
+               .controller('DocdownCtrl', ['$routeParams', '$sce', '$scope', '$location', 'DocDown', function($routeParams, $sce, $scope, $location, DocDown) {
+
+                       this.updateSnippet = function() {
+                               this.updateLink();
+                               this.updateHtml();
+                       }
+
+                       this.updateLink = function() {
+                               $scope.link = $location.protocol()+ '://' + $location.host() + ':' +
+                                       $location.port() + $location.path() + '?snippet=' +
+                                       encodeURIComponent(btoa($scope.markdown));
+                       }
+
+                       this.updateHtml = function() {
+                               DocDown.postMarkdown($scope.markdown,
+                                       function(data) {
+                                               $scope.html = $sce.trustAsHtml(data);
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.editMode = function(isEdit) {
+                               $scope.edit = isEdit;
+                       }
+
+                       $scope.markdown = 'Type some markdown...';
+                       if($location.search().snippet) {
+                               $scope.markdown = atob($location.search().snippet);
+                       }
+                       $scope.edit = false;
+                       if($location.search().edit) {
+                               $scope.edit = Boolean($location.search().edit);
+                       }
+
+                       this.updateSnippet();
+               }])
+})();
index 8812a7a..3accab2 100644 (file)
                                },
                        }
                }])
+
+               .factory('DocDown', [ '$http', function($http) {
+                       return {
+                               postMarkdown: function(md, cb, cbErr) {
+                                       $http.post(apiUrl + '/docdown', md)
+                                               .success(cb)
+                                               .error(cbErr);
+                               }
+                       }
+               }])
 })();
index 99034a7..7fa1142 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 (function() {
-       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'angular-loading-bar', 'entities', 'index'])
+       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'angular-loading-bar', 'entities', 'docdown', 'index'])
        .config(['cfpLoadingBarProvider', function(cfpLoadingBarProvider) {
                cfpLoadingBarProvider.includeSpinner = false;
        }])
                                controller: 'IndexCtrl',
                                controllerAs: 'indexCtrl'
                        })
+                       .when('/docdown', {
+                               templateUrl: 'views/docdown.html',
+                               controller: 'DocdownCtrl',
+                               controllerAs: 'docdownCtrl'
+                       })
                        .when('/doc/:id', {
                                templateUrl: 'views/doc.html',
                                controller: 'EntityCtrl',
diff --git a/share/nitweb/views/docdown.html b/share/nitweb/views/docdown.html
new file mode 100644 (file)
index 0000000..75fe6df
--- /dev/null
@@ -0,0 +1,33 @@
+<div class='container-fluid'>
+       <div class='page-header'>
+               <h2>Docdown snippets</h2>
+               <p class='text-muted'>Sharable documentation snippets.</p>
+               <div class="input-group">
+                       <span ng-if='edit' class="input-group-btn">
+                               <button class='btn btn-success' ng-click='docdownCtrl.editMode(false)'>
+                                       <span class='glyphicon glyphicon-link' /> View
+                               </button>
+                       </span>
+                       <span ng-if='!edit' class="input-group-btn">
+                               <button class='btn btn-success' ng-click='docdownCtrl.editMode(true)'>
+                                       <span class='glyphicon glyphicon-edit' /> Edit
+                               </button>
+                       </span>
+                       <input class='form-control' type='text' ng-model='link' />
+               </div>
+       </div>
+       <div class='row'>
+               <div ng-show='edit' class='col-xs-6'>
+                       <div class='card'>
+                               <textarea ng-model='markdown' ng-model-options='{ debounce: 100 }' ng-change='docdownCtrl.updateSnippet()' class='form-control' rows='20'></textarea>
+                       </div>
+               </div>
+               <div ng-class='edit ? "col-xs-6" : "col-xs-12"'>
+                       <div class='card'>
+                               <div class='card-body nitdoc'>
+                                       <div ng-bind-html='html' />
+                               </div>
+                       </div>
+               </div>
+       </div>
+</div>
index e69bb38..1537704 100644 (file)
@@ -19,8 +19,6 @@
 # * `nitdoc` wikilinks like `[[doc: MEntity::name]]`
 module doc_commands
 
-import doc_base
-
 # A command aimed at a documentation tool like `nitdoc` or `nitx`.
 #
 # `DocCommand` are generally of the form `command: args`.
@@ -57,6 +55,8 @@ interface DocCommand
                        return new CallCommand(command_string)
                else if command_string.has_prefix("code:") then
                        return new CodeCommand(command_string)
+               else if command_string.has_prefix("graph:") then
+                       return new GraphCommand(command_string)
                end
                return new UnknownCommand(command_string)
        end
@@ -153,3 +153,11 @@ end
 class CodeCommand
        super AbstractDocCommand
 end
+
+# A `DocCommand` that display an graph for a `MEntity`.
+#
+# Syntax:
+# * `graph: MEntity::name`
+class GraphCommand
+       super AbstractDocCommand
+end
index 2e4e1d2..3897c5c 100644 (file)
@@ -103,6 +103,7 @@ class APIRouter
                use("/defs/:id", new APIEntityDefs(model, mainmodule))
                use("/inheritance/:id", new APIEntityInheritance(model, mainmodule))
                use("/graph/", new APIGraphRouter(model, mainmodule))
+               use("/docdown/", new APIDocdown(model, mainmodule, modelbuilder))
        end
 end
 
diff --git a/src/web/api_docdown.nit b/src/web/api_docdown.nit
new file mode 100644 (file)
index 0000000..3510b64
--- /dev/null
@@ -0,0 +1,272 @@
+# 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.
+
+# Nitdoc specific Markdown format handling for Nitweb
+module api_docdown
+
+import api_graph
+intrude import doc_down
+intrude import markdown::wikilinks
+import doc_commands
+
+# Docdown handler accept docdown as POST data and render it as HTML
+class APIDocdown
+       super APIHandler
+
+       # Modelbuilder used by the commands
+       var modelbuilder: ModelBuilder
+
+       # Specific Markdown processor to use within Nitweb
+       var md_processor: MarkdownProcessor is lazy do
+               var proc = new MarkdownProcessor
+               proc.emitter.decorator = new NitwebDecorator(view, modelbuilder)
+               return proc
+       end
+
+       redef fun post(req, res) do
+               res.html md_processor.process(req.body)
+       end
+end
+
+# Specific Markdown decorator for Nitweb
+#
+# We reuse all the implementation of the NitdocDecorator and add the wikilinks handling.
+class NitwebDecorator
+       super NitdocDecorator
+
+       # View used by wikilink commands to find model entities
+       var view: ModelView
+
+       # Modelbuilder used to access code
+       var modelbuilder: ModelBuilder
+
+       redef fun add_wikilink(v, token) do
+               var link = token.link
+               if link == null then return
+               var cmd = new DocCommand(link.write_to_string)
+               cmd.render(v, token, view)
+       end
+end
+
+# Same as `InlineDecorator` but with wikilink commands handling
+class NitwebInlineDecorator
+       super InlineDecorator
+
+       # View used by wikilink commands to find model entities
+       var view: ModelView
+
+       # Modelbuilder used to access code
+       var modelbuilder: ModelBuilder
+
+       redef fun add_wikilink(v, token) do
+               var link = token.link
+               if link == null then return
+               var cmd = new DocCommand(link.write_to_string)
+               cmd.render(v, token, view)
+       end
+end
+
+redef interface DocCommand
+
+       # Emit the HTML related to the execution of this doc command
+       fun render(v: MarkdownEmitter, token: TokenWikiLink, model: ModelView) do
+               write_error(v, "Not yet implemented command `{token.link or else "null"}`")
+       end
+
+       # Find the MEntity ` with `full_name`.
+       fun find_mentity(model: ModelView, full_name: nullable String): nullable MEntity do
+               if full_name == null then return null
+               return model.mentity_by_full_name(full_name.from_percent_encoding)
+       end
+
+       # Write a warning in the output
+       fun write_warning(v: MarkdownEmitter, text: String) do
+               v.emit_text "<p class='text-warning'>Warning: {text}</p>"
+       end
+
+       # Write an error in the output
+       fun write_error(v: MarkdownEmitter, text: String) do
+               v.emit_text "<p class='text-danger'>Error: {text}</p>"
+       end
+
+       # Write a link to a mentity in the output
+       fun write_mentity_link(v: MarkdownEmitter, mentity: MEntity) do
+               var link = mentity.web_url
+               var name = mentity.name
+               var mdoc = mentity.mdoc_or_fallback
+               var comment = null
+               if mdoc != null then comment = mdoc.synopsis
+               v.decorator.add_link(v, link, name, comment)
+       end
+end
+
+redef class UnknownCommand
+       redef fun render(v, token, model) do
+               var link = token.link
+               if link == null then
+                       write_error(v, "Empty command")
+                       return
+               end
+               var full_name = link.write_to_string
+               var mentity = find_mentity(model, full_name)
+               if mentity == null then
+                       write_error(v, "Unknown command `{link}`")
+                       return
+               end
+               write_mentity_link(v, mentity)
+       end
+end
+
+redef class ArticleCommand
+       redef fun render(v, token, model) do
+               if args.is_empty then
+                       write_error(v, "Expected one arg: the MEntity name")
+                       return
+               end
+               var name = args.first
+               var mentity = find_mentity(model, name)
+               if mentity == null then
+                       write_error(v, "No MEntity found for name `{name}`")
+                       return
+               end
+               var mdoc = mentity.mdoc_or_fallback
+               if mdoc == null then
+                       write_warning(v, "No MDoc for mentity `{name}`")
+                       return
+               end
+               v.add "<h3>"
+               write_mentity_link(v, mentity)
+               v.add " - "
+               v.emit_text mdoc.synopsis
+               v.add "</h3>"
+               v.add v.processor.process(mdoc.comment).write_to_string
+       end
+end
+
+redef class CommentCommand
+       redef fun render(v, token, model) do
+               if args.is_empty then
+                       write_error(v, "Expected one arg: the MEntity name")
+                       return
+               end
+               var name = args.first
+               var mentity = find_mentity(model, name)
+               if mentity == null then
+                       write_error(v, "No MEntity found for name `{name}`")
+                       return
+               end
+               var mdoc = mentity.mdoc_or_fallback
+               if mdoc == null then
+                       write_warning(v, "No MDoc for mentity `{name}`")
+                       return
+               end
+               v.add v.processor.process(mdoc.comment).write_to_string
+       end
+end
+
+redef class ListCommand
+       redef fun render(v, token, model) do
+               if args.is_empty then
+                       write_error(v, "Expected one arg: the MEntity name")
+                       return
+               end
+               var name = args.first
+               var mentity = find_mentity(model, name)
+               if mentity isa MPackage then
+                       write_list(v, mentity.mgroups)
+               else if mentity isa MGroup then
+                       var res = new Array[MEntity]
+                       res.add_all mentity.in_nesting.smallers
+                       res.add_all mentity.mmodules
+                       write_list(v, res)
+               else if mentity isa MModule then
+                       write_list(v, mentity.mclassdefs)
+               else if mentity isa MClass then
+                       write_list(v, mentity.collect_intro_mproperties(model))
+               else if mentity isa MClassDef then
+                       write_list(v, mentity.mpropdefs)
+               else if mentity isa MProperty then
+                       write_list(v, mentity.mpropdefs)
+               else
+                       write_error(v, "No list found for name `{name}`")
+               end
+       end
+
+       # Write a mentity list in the output
+       fun write_list(v: MarkdownEmitter, mentities: Collection[MEntity]) do
+               v.add "<ul>"
+               for mentity in mentities do
+                       var mdoc = mentity.mdoc_or_fallback
+                       v.add "<li>"
+                       write_mentity_link(v, mentity)
+                       if mdoc != null then
+                               v.add " - "
+                               v.emit_text mdoc.synopsis
+                       end
+                       v.add "</li>"
+               end
+               v.add "</ul>"
+       end
+end
+
+redef class CodeCommand
+       redef fun render(v, token, model) do
+               if args.is_empty then
+                       write_error(v, "Expected one arg: the MEntity name")
+                       return
+               end
+               var name = args.first
+               var mentity = find_mentity(model, name)
+               if mentity == null then
+                       write_error(v, "No MEntity found for name `{name}`")
+                       return
+               end
+               if mentity isa MClass then mentity = mentity.intro
+               if mentity isa MProperty then mentity = mentity.intro
+               var source = render_source(mentity, v.decorator.as(NitwebDecorator).modelbuilder)
+               if source == null then
+                       write_error(v, "No source for MEntity `{name}`")
+                       return
+               end
+               v.add "<pre>"
+               v.add source
+               v.add "</pre>"
+       end
+
+       # Highlight `mentity` source code.
+       private fun render_source(mentity: MEntity, modelbuilder: ModelBuilder): nullable HTMLTag do
+               var node = modelbuilder.mentity2node(mentity)
+               if node == null then return null
+               var hl = new HighlightVisitor
+               hl.enter_visit node
+               return hl.html
+       end
+end
+
+redef class GraphCommand
+       redef fun render(v, token, model) do
+               if args.is_empty then
+                       write_error(v, "Expected one arg: the MEntity name")
+                       return
+               end
+               var name = args.first
+               var mentity = find_mentity(model, name)
+               if mentity == null then
+                       write_error(v, "No MEntity found for name `{name}`")
+                       return
+               end
+               var g = new InheritanceGraph(mentity, model)
+               v.add g.draw(3, 3).to_svg
+       end
+end
index be5d2d1..e82a0f5 100644 (file)
@@ -18,3 +18,4 @@ module web
 import model_api
 import api_catalog
 import api_graph
+import api_docdown