Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/257553

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

Initial commit

Implements:

 - A NotebookContent that represents a jupyter notebook
 - 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

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


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/NotebookViewer 
refs/changes/53/257553/1

diff --git a/NotebookContent.php b/NotebookContent.php
new file mode 100644
index 0000000..751e126
--- /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..4fae126
--- /dev/null
+++ b/convertor.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+import sys
+
+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..9a26427
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,38 @@
+{
+       "name": "NotebookViewer",
+       "version": "0.0.1",
+       "author": [
+               "Yuvi Panda"
+       ],
+       "url": "https://www.mediawiki.org/wiki/Extension:NotebookViwer";,
+       "descriptionmsg": "notebookviewer-desc",
+       "license-name": "MIT",
+       "type": "other",
+       "AutoloadClasses": {
+               "NotebookContent": "NotebookContent.php",
+        "NotebookContentHandler": "NotebookContentHandler.php"
+       },
+    "namespaces": [
+        {
+            "id": 22222,
+            "constant": "NS_NOTEBOOK",
+            "name": "Notebook",
+            "defaultcontentmodel": "Notebook"
+        }
+    ],
+    "ContentHandlers": {
+        "Notebook": "NotebookContentHandler"
+    },
+       "config": {
+               "NotebookStripJS": true
+       },
+       "ExtensionMessagesFiles": {
+               "BoilerPlateAlias": "BoilerPlate.i18n.alias.php"
+       },
+       "MessagesDirs": {
+               "BoilerPlate": [
+                       "i18n"
+               ]
+       },
+       "manifest_version": 1
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaaff2dd22d780fe89a82dd50d88bbb6c1e1c81cb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/NotebookViewer
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>

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

Reply via email to