Repository: mwouts/jupytext

Issue:
%magic can be after indent or tab
I have a code that I expect to work both in colab and jupyter, also in .py

```python

FROM_GOOGLE_COLAB = True
PROJECT_FOLDER = '/content/drive/MyDrive/SOME_REPO_NAME'
if FROM_GOOGLE_COLAB:
    !pip install somepackages

    from google.colab import drive
    drive.mount('/content/drive')
    %cd $PROJECT_FOLDER

```

But, jupytext didn't translate `%cd` as magic if it's indented or some white space exists before it.

```python
%cd
!pip install somepackages 

# become:
# %cd
# !pip install somepackages 


if True:
    %cd
    !pip install somepackages
    pass

# become:
if True:
    %cd # not works
    # !pip install somepackages 
    pass
```

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/717?src=pr&el=h1) Report
> Merging [#717](https://codecov.io/gh/mwouts/jupytext/pull/717?src=pr&el=desc) (ba37bac) into [master](https://codecov.io/gh/mwouts/jupytext/commit/12fb00e4e00460f889478b0c54dedd157f567174?el=desc) (12fb00e) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

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

```diff
@@           Coverage Diff           @@
##           master     #717   +/-   ##
=======================================
  Coverage   99.12%   99.12%           
=======================================
  Files          95       95           
  Lines        9356     9359    +3     
=======================================
+ Hits         9274     9277    +3     
  Misses         82       82           
```


| [Impacted Files](https://codecov.io/gh/mwouts/jupytext/pull/717?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [jupytext/magics.py](https://codecov.io/gh/mwouts/jupytext/pull/717/diff?src=pr&el=tree#diff-anVweXRleHQvbWFnaWNzLnB5) | `100.00% <ø> (ø)` | |
| [tests/test\_escape\_magics.py](https://codecov.io/gh/mwouts/jupytext/pull/717/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9lc2NhcGVfbWFnaWNzLnB5) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mwouts/jupytext/pull/717?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/717?src=pr&el=footer). Last update [12fb00e...ba37bac](https://codecov.io/gh/mwouts/jupytext/pull/717?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index abe4b7a..b86ff3d 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -7,6 +7,9 @@ Jupytext ChangeLog
 **Changed**
 - Jupytext does not work properly with the new cell ids of the version 4.5 of `nbformat>=5.1.0` yet, so we added the requirement `nbformat<=5.0.8` (#715)
 
+**Fixed**
+- Indented magic commands are supported (#694)
+
 
 1.9.1 (2021-01-06)
 ------------------
diff --git a/jupytext/magics.py b/jupytext/magics.py
index 4de898a..9b7489d 100644
--- a/jupytext/magics.py
+++ b/jupytext/magics.py
@@ -7,13 +7,13 @@ from .languages import _SCRIPT_EXTENSIONS, _COMMENT, usual_language_name
 # A magic expression is a line or cell or metakernel magic (#94, #61) escaped zero, or multiple times
 _MAGIC_RE = {
     _SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
-        r"^({0} |{0})*(%|%%|%%%)[a-zA-Z]".format(_SCRIPT_EXTENSIONS[ext]["comment"])
+        r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z]".format(_SCRIPT_EXTENSIONS[ext]["comment"])
     )
     for ext in _SCRIPT_EXTENSIONS
 }
 _MAGIC_FORCE_ESC_RE = {
     _SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
-        r"^({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*escape".format(
+        r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*escape".format(
             _SCRIPT_EXTENSIONS[ext]["comment"]
         )
     )
@@ -21,7 +21,7 @@ _MAGIC_FORCE_ESC_RE = {
 }
 _MAGIC_NOT_ESC_RE = {
     _SCRIPT_EXTENSIONS[ext]["language"]: re.compile(
-        r"^({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*noescape".format(
+        r"^\s*({0} |{0})*(%|%%|%%%)[a-zA-Z](.*){0}\s*noescape".format(
             _SCRIPT_EXTENSIONS[ext]["comment"]
         )
     )


Test patch:
diff --git a/tests/test_escape_magics.py b/tests/test_escape_magics.py
index b483128..e25c9ac 100644
--- a/tests/test_escape_magics.py
+++ b/tests/test_escape_magics.py
@@ -270,5 +270,8 @@ def test_configure_magic(no_jupytext_version_number):
 def test_indented_magic():
     assert is_magic("    !rm file", "python")
     assert is_magic("    # !rm file", "python")
+    assert is_magic("    %cd", "python")
     assert comment_magic(["    !rm file"]) == ["    # !rm file"]
     assert uncomment_magic(["    # !rm file"]) == ["    !rm file"]
+    assert comment_magic(["    %cd"]) == ["    # %cd"]
+    assert uncomment_magic(["    # %cd"]) == ["    %cd"]