Yuvipanda has submitted this change and it was merged.

Change subject: Initial commit
......................................................................


Initial commit

Implements:

 - A NotebookContent that represents a jupyter notebook. This
   uses a TextContent even though notebooks are JSON, since
   the PHP side can not really meaningfully do anything about
   the JSON, and so parsing it into JSON just wastes effort.
 - A rendering pipeline for it that involves (for now!) just
   shelling out to a simple python script (adopted from script
   from @rgbkrk)
 - Uses symfony/process for shelling out
 - Strips all custom JS and CSS from notebooks before rendering
   them

Bug: T120697
Change-Id: Iaaff2dd22d780fe89a82dd50d88bbb6c1e1c81cb
---
A NotebookContent.php
A NotebookContentHandler.php
A composer.json
A convertor.py
A extension.json
5 files changed, 135 insertions(+), 0 deletions(-)

Approvals:
  Prtksxna: Verified; Looks good to me, approved



diff --git a/NotebookContent.php b/NotebookContent.php
new file mode 100644
index 0000000..83852cd
--- /dev/null
+++ b/NotebookContent.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Notebook content model
+ *
+ * @file
+ * @ingroup Extensions
+ * @ingroup NotebookViewer
+ *
+ * @author Ori Livneh <[email protected]>
+ * @author Yuvi Panda <[email protected]>
+ */
+
+use Symfony\Component\Process\Process;
+
+/**
+ * Represents the configuration of a Jupyter Notebook
+ */
+class NotebookContent extends TextContent {
+
+       function __construct( $text ) {
+               parent::__construct( $text, 'Notebook' );
+       }
+
+       protected function renderNotebook( $content ) {
+               $retval = null;
+               $process = new Process( __DIR__ . "/convertor.py" );
+               $process->setInput( $content );
+
+               $process->run();
+               if ( $process->isSuccessful() ) {
+                       return $process->getOutput();
+               } else {
+                       return "Parsing error";
+               }
+       }
+
+       protected function fillParserOutput( Title $title, $revId,
+               ParserOptions $options, $generateHtml, ParserOutput &$output
+       ) {
+               // FIXME: WikiPage::doEditContent generates parser output 
before validation.
+               // As such, native data may be invalid (though output is 
discarded later in that case).
+               if ( $generateHtml && $this->isValid() ) {
+                       $output->setText( $this->renderNotebook( 
$this->getNativeData() ) );
+               } else {
+                       $output->setText( 'error' );
+               }
+       }
+}
diff --git a/NotebookContentHandler.php b/NotebookContentHandler.php
new file mode 100644
index 0000000..d6fab60
--- /dev/null
+++ b/NotebookContentHandler.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Notebook Content Handler
+ *
+ * @file
+ * @ingroup Extensions
+ * @ingroup NotebookHandler
+ *
+ * @author Ori Livneh <[email protected]>
+ * @author Yuvi Panda <[email protected]>
+ */
+
+class NotebookContentHandler extends TextContentHandler {
+
+       public function __construct( $modelId = 'Notebook' ) {
+               parent::__construct( $modelId );
+       }
+
+       public function canBeUsedOn( Title $title ) {
+               return $title->inNamespace( NS_NOTEBOOK );
+       }
+
+       protected function getContentClass() {
+               return 'NotebookContent';
+       }
+}
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..47ae9c2
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,5 @@
+{
+       "require": {
+               "symfony/process": "~2.5"
+       }
+}
diff --git a/convertor.py b/convertor.py
new file mode 100755
index 0000000..b15be4e
--- /dev/null
+++ b/convertor.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+import sys
+reload(sys)
+sys.setdefaultencoding("utf-8")
+
+from nbconvert.exporters import HTMLExporter
+from traitlets.config import Config
+
+config = Config({
+    "HTMLExporter": {"template_file": "basic"},
+    'NbConvertBase': {
+        'display_data_priority': [
+            'text/html',
+            'text/markdown',
+            'application/pdf',
+            'image/svg+xml',
+            'text/latex',
+            'image/png',
+            'image/jpeg',
+            'text/plain',
+        ]
+    }
+})
+ex = HTMLExporter(config=config)
+
+html, extra = ex.from_file(sys.stdin)
+sys.stdout.write(html)
diff --git a/extension.json b/extension.json
new file mode 100644
index 0000000..cc8d66c
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,27 @@
+{
+       "name": "NotebookViewer",
+       "version": "0.0.1",
+       "author": [
+               "Yuvi Panda"
+       ],
+       "url": "https://www.mediawiki.org/wiki/Extension:NotebookViewer";,
+       "descriptionmsg": "notebookviewer-desc",
+       "license-name": "MIT",
+       "type": "other",
+       "AutoloadClasses": {
+               "NotebookContent": "NotebookContent.php",
+               "NotebookContentHandler": "NotebookContentHandler.php"
+       },
+       "namespaces": [
+               {
+                       "id": 486,
+                       "constant": "NS_NOTEBOOK",
+                       "name": "Notebook",
+                       "defaultcontentmodel": "Notebook"
+               }
+       ],
+       "ContentHandlers": {
+               "Notebook": "NotebookContentHandler"
+       },
+       "manifest_version": 1
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/257553
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iaaff2dd22d780fe89a82dd50d88bbb6c1e1c81cb
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/NotebookViewer
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Milimetric <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Prtksxna <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to