Repository: mwouts/jupytext
Pull request #253 (2019-06-12 08:36:03)

Adding scala support to jupytext extension
This PR adds the scala syntax support to the jupytext extension

codecov[bot]: # [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=h1) Report
> Merging [#253](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=desc) into [master](https://codecov.io/gh/mwouts/jupytext/commit/4afe603a200a3329fe262cf474c7943f205bee9d?src=pr&el=desc) will **decrease** coverage by `10.81%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/mwouts/jupytext/pull/253/graphs/tree.svg?width=650&token=a1pNjsiILi&height=150&src=pr)](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           master     #253       +/-   ##
===========================================
- Coverage   99.16%   88.34%   -10.82%     
===========================================
  Files          66       66               
  Lines        6485     6325      -160     
===========================================
- Hits         6431     5588      -843     
- Misses         54      737      +683
```


| [Impacted Files](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [jupytext/languages.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-anVweXRleHQvbGFuZ3VhZ2VzLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [tests/test\_black.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9ibGFjay5weQ==) | `29.47% <0%> (-70.53%)` | :arrow_down: |
| [tests/test\_mirror.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9taXJyb3IucHk=) | `40.66% <0%> (-59.34%)` | :arrow_down: |
| [tests/test\_read\_simple\_pandoc.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9wYW5kb2MucHk=) | `44.82% <0%> (-55.18%)` | :arrow_down: |
| [tests/test\_knitr\_spin.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9rbml0cl9zcGluLnB5) | `45.45% <0%> (-54.55%)` | :arrow_down: |
| [jupytext/pandoc.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-anVweXRleHQvcGFuZG9jLnB5) | `43.75% <0%> (-52.09%)` | :arrow_down: |
| [tests/test\_active\_cells.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9hY3RpdmVfY2VsbHMucHk=) | `48.52% <0%> (-51.48%)` | :arrow_down: |
| [tests/test\_read\_simple\_rmd.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9ybWQucHk=) | `50% <0%> (-50%)` | :arrow_down: |
| [tests/test\_contentsmanager.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9jb250ZW50c21hbmFnZXIucHk=) | `65.46% <0%> (-34.06%)` | :arrow_down: |
| [tests/test\_rmd\_to\_ipynb.py](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9ybWRfdG9faXB5bmIucHk=) | `72.72% <0%> (-27.28%)` | :arrow_down: |
| ... and [27 more](https://codecov.io/gh/mwouts/jupytext/pull/253/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=footer). Last update [4afe603...6b9915a](https://codecov.io/gh/mwouts/jupytext/pull/253?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

mwouts: Nice, thanks @Freshwood . Would you be open to contributing a sample scala notebook as well? This way we can test your contribution. It's documented [here](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md#propose-enhancements), and in principle it should be easy.
mwouts: Excellent! Thanks for the notebook. I will integrate this soon, hopefully later on today.

Patch:
diff --git a/jupytext/languages.py b/jupytext/languages.py
index 14937976..7467eaf9 100644
--- a/jupytext/languages.py
+++ b/jupytext/languages.py
@@ -3,7 +3,7 @@
 # Languages that may appear as magic instructions in Python notebooks
 _JUPYTER_LANGUAGES = ['R', 'bash', 'sh', 'python', 'python2', 'python3', 'javascript', 'js', 'perl',
                       'html', 'latex', 'markdown', 'pypy', 'ruby', 'script', 'svg', 'writefile',
-                      'matlab', 'octave', 'idl']
+                      'matlab', 'octave', 'idl', 'scala']
 
 # Supported file extensions (and languages)
 # Please add more languages here (and add a few tests) - see CONTRIBUTING.md
@@ -20,7 +20,8 @@ _SCRIPT_EXTENSIONS = {'.py': {'language': 'python', 'comment': '#'},
                       '.m': {'language': 'matlab', 'comment': '%'},
                       '.pro': {'language': 'idl', 'comment': ';'},
                       '.js': {'language': 'javascript', 'comment': '//'},
-                      '.ts': {'language': 'typescript', 'comment': '//'}}
+                      '.ts': {'language': 'typescript', 'comment': '//'},
+                      '.scala': {'language': 'scala', 'comment': '//'}}
 
 _COMMENT_CHARS = [_SCRIPT_EXTENSIONS[ext]['comment'] for ext in _SCRIPT_EXTENSIONS if
                   _SCRIPT_EXTENSIONS[ext]['comment'] != '#']


Test patch:
diff --git a/tests/notebooks/ipynb_scala/simple_scala_notebook.ipynb b/tests/notebooks/ipynb_scala/simple_scala_notebook.ipynb
new file mode 100644
index 00000000..f8d6a3b1
--- /dev/null
+++ b/tests/notebooks/ipynb_scala/simple_scala_notebook.ipynb
@@ -0,0 +1,75 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "defined object SampleObject\n",
+       "result = 3\n"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data",
+     "source": "user"
+    },
+    {
+     "data": {
+      "text/plain": [
+       "3"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "// This is just a simple scala notebook\n",
+    "\n",
+    "object SampleObject {\n",
+    "    def calculation(x: Int, y: Int): Int = x + y\n",
+    "}\n",
+    "\n",
+    "val result = SampleObject.calculation(1, 2)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "30\n"
+     ]
+    }
+   ],
+   "source": [
+    "println(result * 10)"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Apache Toree - Scala",
+   "language": "scala",
+   "name": "apache_toree_scala"
+  },
+  "language_info": {
+   "codemirror_mode": "text/x-scala",
+   "file_extension": ".scala",
+   "mimetype": "text/x-scala",
+   "name": "scala",
+   "pygments_lexer": "scala",
+   "version": "2.11.12"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/tests/notebooks/mirror/ipynb_to_percent/simple_scala_notebook.scala b/tests/notebooks/mirror/ipynb_to_percent/simple_scala_notebook.scala
new file mode 100644
index 00000000..bf025876
--- /dev/null
+++ b/tests/notebooks/mirror/ipynb_to_percent/simple_scala_notebook.scala
@@ -0,0 +1,19 @@
+// ---
+// jupyter:
+//   kernelspec:
+//     display_name: Apache Toree - Scala
+//     language: scala
+//     name: apache_toree_scala
+// ---
+
+// %%
+// This is just a simple scala notebook
+
+object SampleObject {
+    def calculation(x: Int, y: Int): Int = x + y
+}
+
+val result = SampleObject.calculation(1, 2)
+
+// %%
+println(result * 10)
diff --git a/tests/notebooks/mirror/ipynb_to_script/simple_scala_notebook.scala b/tests/notebooks/mirror/ipynb_to_script/simple_scala_notebook.scala
new file mode 100644
index 00000000..631ac90e
--- /dev/null
+++ b/tests/notebooks/mirror/ipynb_to_script/simple_scala_notebook.scala
@@ -0,0 +1,19 @@
+// ---
+// jupyter:
+//   kernelspec:
+//     display_name: Apache Toree - Scala
+//     language: scala
+//     name: apache_toree_scala
+// ---
+
+// +
+// This is just a simple scala notebook
+
+object SampleObject {
+    def calculation(x: Int, y: Int): Int = x + y
+}
+
+val result = SampleObject.calculation(1, 2)
+// -
+
+println(result * 10)
diff --git a/tests/test_mirror.py b/tests/test_mirror.py
index a1b87cc8..5e26bf1d 100644
--- a/tests/test_mirror.py
+++ b/tests/test_mirror.py
@@ -4,10 +4,12 @@ change on new releases.
 """
 
 import os
+
 import mock
 import pytest
 from nbformat.v4.nbbase import new_notebook
 from testfixtures import compare
+
 import jupytext
 from jupytext.compare import compare_notebooks, combine_inputs_with_outputs
 from jupytext.formats import long_form_one_format
@@ -315,3 +317,13 @@ def test_ipynb_to_ts(nb_file):
 @pytest.mark.parametrize('nb_file', list_notebooks('ipynb_ts'))
 def test_ipynb_to_ts_percent(nb_file):
     assert_conversion_same_as_mirror(nb_file, 'ts:percent', 'ipynb_to_percent')
+
+
+@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_scala'))
+def test_ipynb_to_scala(nb_file):
+    assert_conversion_same_as_mirror(nb_file, 'scala', 'ipynb_to_script')
+
+
+@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_scala'))
+def test_ipynb_to_scala_percent(nb_file):
+    assert_conversion_same_as_mirror(nb_file, 'scala:percent', 'ipynb_to_percent')