From 860f8aa764c6d4efcf7af16ea1ea83e7687435e3 Mon Sep 17 00:00:00 2001 From: alawrenc Date: Fri, 24 Feb 2023 14:44:18 -0700 Subject: [PATCH] Adding SAS languague support --- docs/languages.md | 1 + jupytext/languages.py | 10 ++- jupytext/magics.py | 2 +- tests/notebooks/ipynb_sas/sas.ipynb | 81 +++++++++++++++++++ tests/notebooks/mirror/ipynb_to_Rmd/sas.Rmd | 39 +++++++++ .../mirror/ipynb_to_hydrogen/sas.sas | 36 +++++++++ tests/notebooks/mirror/ipynb_to_md/sas.md | 39 +++++++++ tests/notebooks/mirror/ipynb_to_myst/sas.md | 38 +++++++++ .../notebooks/mirror/ipynb_to_percent/sas.sas | 36 +++++++++ .../notebooks/mirror/ipynb_to_script/sas.sas | 32 ++++++++ 10 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 tests/notebooks/ipynb_sas/sas.ipynb create mode 100644 tests/notebooks/mirror/ipynb_to_Rmd/sas.Rmd create mode 100644 tests/notebooks/mirror/ipynb_to_hydrogen/sas.sas create mode 100644 tests/notebooks/mirror/ipynb_to_md/sas.md create mode 100644 tests/notebooks/mirror/ipynb_to_myst/sas.md create mode 100644 tests/notebooks/mirror/ipynb_to_percent/sas.sas create mode 100644 tests/notebooks/mirror/ipynb_to_script/sas.sas diff --git a/docs/languages.md b/docs/languages.md index e73e49942..dc1dfd5fa 100644 --- a/docs/languages.md +++ b/docs/languages.md @@ -24,6 +24,7 @@ Jupytext works with notebooks in any of the following languages: - Robot Framework - Rust/Evxcr - Sage +- SAS - Scala - Scheme - Script of Script diff --git a/jupytext/languages.py b/jupytext/languages.py index 37c0d9d32..c3cebcdb4 100644 --- a/jupytext/languages.py +++ b/jupytext/languages.py @@ -23,6 +23,7 @@ "octave", "idl", "robotframework", + "sas", "spark", "sql", "cython", @@ -84,6 +85,11 @@ }, # Maxima only has block comments ".gp": {"language": "gnuplot", "comment": "#"}, ".do": {"language": "stata", "comment": "//"}, + ".sas": { + "language": "sas", + "comment": "/*", + "comment_suffix": "*/", + }, } _COMMENT_CHARS = [ @@ -121,7 +127,7 @@ def default_language_from_metadata_and_ext(metadata, ext, pop_main_language=Fals ): metadata["jupytext"].pop("main_language") - if language is None or language == "R": + if language is None or language in ["R", "sas"]: return language if language.startswith("C++"): @@ -143,6 +149,8 @@ def usual_language_name(language): return "csharp" if language in ["fs", "f#"]: return "fsharp" + if language == "sas": + return "SAS" return language diff --git a/jupytext/magics.py b/jupytext/magics.py index 69af12b60..babda739d 100644 --- a/jupytext/magics.py +++ b/jupytext/magics.py @@ -69,7 +69,7 @@ def get_comment(ext): def is_magic(line, language, global_escape_flag=True, explicitly_code=False): """Is the current line a (possibly escaped) Jupyter magic, and should it be commented?""" language = usual_language_name(language) - if language in ["octave", "matlab"] or language not in _SCRIPT_LANGUAGES: + if language in ["octave", "matlab", "sas"] or language not in _SCRIPT_LANGUAGES: return False if _MAGIC_FORCE_ESC_RE[language].match(line): return True diff --git a/tests/notebooks/ipynb_sas/sas.ipynb b/tests/notebooks/ipynb_sas/sas.ipynb new file mode 100644 index 000000000..25ed8b11e --- /dev/null +++ b/tests/notebooks/ipynb_sas/sas.ipynb @@ -0,0 +1,81 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "14a71cf0-4339-4ae6-982d-7dda15f3810e", + "metadata": {}, + "source": [ + "# SAS Notebooks with jupytext" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47346888-e79f-4205-82fd-3a5fdfde9b4d", + "metadata": {}, + "outputs": [], + "source": [ + "proc sql;\n", + " select *\n", + " from sashelp.cars (obs=10)\n", + " ;\n", + "quit; " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c96b0faf-26fe-4245-a1d9-78dee30b19c3", + "metadata": {}, + "outputs": [], + "source": [ + "%let name = \"Jupytext\";" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56cafffb-e397-4c3b-97ab-c6970149b6bc", + "metadata": {}, + "outputs": [], + "source": [ + "%put &name;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bbc273c9-8690-450d-bd9b-2d075d512437", + "metadata": {}, + "outputs": [], + "source": [ + "/* Note when defining macros \"%macro\" cannot be the first line of text in the cell */\n", + "%macro test;\n", + " data temp;\n", + " set sashelp.cars;\n", + " name = \"testx\";\n", + " run; \n", + " proc print data = temp (obs=10);\n", + " run; \n", + "%mend test;\n", + "\n", + "%test" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "SAS", + "language": "sas", + "name": "sas" + }, + "language_info": { + "codemirror_mode": "sas", + "file_extension": ".sas", + "mimetype": "text/x-sas", + "name": "sas" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/notebooks/mirror/ipynb_to_Rmd/sas.Rmd b/tests/notebooks/mirror/ipynb_to_Rmd/sas.Rmd new file mode 100644 index 000000000..6ef3b8622 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_Rmd/sas.Rmd @@ -0,0 +1,39 @@ +--- +jupyter: + kernelspec: + display_name: SAS + language: sas + name: sas +--- + +# SAS Notebooks with jupytext + +```{sas} +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; +``` + +```{sas} +%let name = "Jupytext"; +``` + +```{sas} +%put &name; +``` + +```{sas} +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test +``` diff --git a/tests/notebooks/mirror/ipynb_to_hydrogen/sas.sas b/tests/notebooks/mirror/ipynb_to_hydrogen/sas.sas new file mode 100644 index 000000000..c8887d824 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_hydrogen/sas.sas @@ -0,0 +1,36 @@ +/* --- */ +/* jupyter: */ +/* kernelspec: */ +/* display_name: SAS */ +/* language: sas */ +/* name: sas */ +/* --- */ + +/* %% [markdown] */ +/* # SAS Notebooks with jupytext */ + +/* %% */ +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; + +/* %% */ +%let name = "Jupytext"; + +/* %% */ +%put &name; + +/* %% */ +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test diff --git a/tests/notebooks/mirror/ipynb_to_md/sas.md b/tests/notebooks/mirror/ipynb_to_md/sas.md new file mode 100644 index 000000000..adeba014a --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_md/sas.md @@ -0,0 +1,39 @@ +--- +jupyter: + kernelspec: + display_name: SAS + language: sas + name: sas +--- + +# SAS Notebooks with jupytext + +```sas +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; +``` + +```sas +%let name = "Jupytext"; +``` + +```sas +%put &name; +``` + +```sas +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test +``` diff --git a/tests/notebooks/mirror/ipynb_to_myst/sas.md b/tests/notebooks/mirror/ipynb_to_myst/sas.md new file mode 100644 index 000000000..0c9ffdd29 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_myst/sas.md @@ -0,0 +1,38 @@ +--- +kernelspec: + display_name: SAS + language: sas + name: sas +--- + +# SAS Notebooks with jupytext + +```{code-cell} +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; +``` + +```{code-cell} +%let name = "Jupytext"; +``` + +```{code-cell} +%put &name; +``` + +```{code-cell} +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test +``` diff --git a/tests/notebooks/mirror/ipynb_to_percent/sas.sas b/tests/notebooks/mirror/ipynb_to_percent/sas.sas new file mode 100644 index 000000000..c8887d824 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_percent/sas.sas @@ -0,0 +1,36 @@ +/* --- */ +/* jupyter: */ +/* kernelspec: */ +/* display_name: SAS */ +/* language: sas */ +/* name: sas */ +/* --- */ + +/* %% [markdown] */ +/* # SAS Notebooks with jupytext */ + +/* %% */ +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; + +/* %% */ +%let name = "Jupytext"; + +/* %% */ +%put &name; + +/* %% */ +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test diff --git a/tests/notebooks/mirror/ipynb_to_script/sas.sas b/tests/notebooks/mirror/ipynb_to_script/sas.sas new file mode 100644 index 000000000..f350285ec --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_script/sas.sas @@ -0,0 +1,32 @@ +/* --- */ +/* jupyter: */ +/* kernelspec: */ +/* display_name: SAS */ +/* language: sas */ +/* name: sas */ +/* --- */ + +/* # SAS Notebooks with jupytext */ + +proc sql; + select * + from sashelp.cars (obs=10) + ; +quit; + +%let name = "Jupytext"; + +%put &name; + +/* + +/* Note when defining macros "%macro" cannot be the first line of text in the cell */ +%macro test; + data temp; + set sashelp.cars; + name = "testx"; + run; + proc print data = temp (obs=10); + run; +%mend test; + +%test