Repository: mwouts/jupytext

Issue:
Bibliography section becomes code
This is really to ask for advice about a problem I have with jupyter-book files.

For some files, I want to list ciations.  The obvious way to do this, is to include a version of this Markdown cell at the end of the page (RMarkdown format) - see https://jupyterbook.org/en/stable/content/citations.html#local-bibliographies

~~~
```{bibliography}
```
~~~

However, Jupytext interprets this text as a code cell of language `bibliography`, so running `jupytext --to Rmd` on a file with this cell, results in:

~~~
```{bibliography language="bibliography"}

```
~~~

In the notebook interface, this also appears as a code cell.

Do you have any advice for avoiding this behavior?

Thanks as ever.

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/mwouts/jupytext/pull/1175?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`405617f`)](https://app.codecov.io/gh/mwouts/jupytext/commit/405617f43f566de79aecde37863614715884e0e6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) 97.61% compared to head [(`0db460d`)](https://app.codecov.io/gh/mwouts/jupytext/pull/1175?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) 97.61%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1175   +/-   ##
=======================================
  Coverage   97.61%   97.61%           
=======================================
  Files          29       29           
  Lines        4451     4451           
=======================================
  Hits         4345     4345           
  Misses        106      106           
```

| [Flag](https://app.codecov.io/gh/mwouts/jupytext/pull/1175/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) | Coverage Δ | |
|---|---|---|
| [external](https://app.codecov.io/gh/mwouts/jupytext/pull/1175/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) | `75.12% <100.00%> (ø)` | |
| [functional](https://app.codecov.io/gh/mwouts/jupytext/pull/1175/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) | `88.54% <100.00%> (ø)` | |
| [integration](https://app.codecov.io/gh/mwouts/jupytext/pull/1175/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) | `77.37% <100.00%> (ø)` | |
| [unit](https://app.codecov.io/gh/mwouts/jupytext/pull/1175/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts) | `?` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/mwouts/jupytext/pull/1175?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marc+Wouts).


Patch:
diff --git a/src/jupytext/cell_reader.py b/src/jupytext/cell_reader.py
index e4ea21c..738cd24 100644
--- a/src/jupytext/cell_reader.py
+++ b/src/jupytext/cell_reader.py
@@ -443,7 +443,9 @@ class MarkdownCellReader(BaseCellReader):
                         return i - 1, i, False
                     return i, i, False
 
-                if self.start_code_re.match(line):
+                if self.start_code_re.match(line) and not line.startswith(
+                    "```{bibliography}"
+                ):
                     # Cells with a .noeval attribute are markdown cells #347
                     _, metadata = self.options_to_metadata(
                         self.start_code_re.findall(line)[0]
@@ -862,6 +864,7 @@ class DoublePercentScriptCellReader(LightScriptCellReader):
             if self.metadata.get("active") == "":
                 del self.metadata["active"]
             self.cell_type = "raw"
+            self.comment = ""
         else:
             self.cell_type = "code"
 


Test patch:
diff --git a/tests/functional/others/test_active_cells.py b/tests/functional/others/test_active_cells.py
index 2982419..1403caf 100644
--- a/tests/functional/others/test_active_cells.py
+++ b/tests/functional/others/test_active_cells.py
@@ -291,6 +291,60 @@ print('should only be displayed in py file')
     compare(text2, text)
 
 
+def test_comments_work_in_active_cells_from_py_percent_1131(
+    text="""# %% tags=["active-py"]
+# this is a comment
+""",
+):
+    nb = jupytext.reads(text, "py:percent")
+    assert nb.cells[0].cell_type == "raw"
+    assert nb.cells[0].source == "# this is a comment"
+
+    text2 = jupytext.writes(nb, "py:percent")
+    compare(text2, text)
+
+
+def test_comments_work_in_active_cells_from_py_light_1131(
+    text="""# + tags=["active-py"]
+# this is a comment
+""",
+):
+    nb = jupytext.reads(text, "py:light")
+    assert nb.cells[0].cell_type == "raw"
+    assert nb.cells[0].source == "# this is a comment"
+
+    text2 = jupytext.writes(nb, "py:light")
+    compare(text2, text)
+
+
+def test_comments_plus_code_work_in_active_cells_from_py_percent_1131(
+    text="""# %% tags=["active-py"]
+# this is a comment
+1 + 1
+""",
+):
+    nb = jupytext.reads(text, "py:percent")
+    assert nb.cells[0].cell_type == "raw"
+    assert nb.cells[0].source == "# this is a comment\n1 + 1"
+
+    text2 = jupytext.writes(nb, "py:percent")
+    compare(text2, text)
+
+
+def test_comments_plus_code_work_in_active_cells_from_py_light_1131(
+    text="""# + tags=["active-py"]
+# this is a comment
+1 + 1
+""",
+):
+    nb = jupytext.reads(text, "py:light")
+    assert nb.cells[0].cell_type == "raw"
+    assert nb.cells[0].source == "# this is a comment\n1 + 1"
+
+    text2 = jupytext.writes(nb, "py:light")
+    compare(text2, text)
+
+
 def test_active_cells_from_py_light(
     text="""# + active="py"
 print('should only be displayed in py file')
diff --git a/tests/functional/simple_notebooks/test_read_simple_rmd.py b/tests/functional/simple_notebooks/test_read_simple_rmd.py
index ee10f3b..56958a1 100644
--- a/tests/functional/simple_notebooks/test_read_simple_rmd.py
+++ b/tests/functional/simple_notebooks/test_read_simple_rmd.py
@@ -264,3 +264,20 @@ def test_commented_triple_quote_1060(line):
     compare(rmd2, rmd)
     nb2 = jupytext.reads(rmd, fmt="Rmd")
     compare_notebooks(nb2, nb)
+
+
+def test_bibliography_in_rmd(
+    rmd="""Issue #1161
+
+The bibliography section below should not
+become a code cell
+
+```{bibliography}
+```
+""",
+):
+    nb = jupytext.reads(rmd, fmt="Rmd")
+    assert len(nb.cells) == 1, nb.cells
+    assert nb.cells[0].cell_type == "markdown"
+    rmd2 = jupytext.writes(nb, fmt="Rmd")
+    compare(rmd2, rmd)