executablebooks / MyST-NB

Parse and execute ipynb files in Sphinx
https://myst-nb.readthedocs.io
BSD 3-Clause "New" or "Revised" License
214 stars 84 forks source link

Rendering issue with C++ notebooks #312

Open bluescarni opened 3 years ago

bluescarni commented 3 years ago

Describe the bug

I am using myst-nb to include in sphinx C++ notebooks written on top of cling.

The issue is that C++ pragmas beginning with # seem to be interpreted as Python comments when they are rendered in sphinx by myst-nb.

E.g., compare the original notebook in jupyter:

Screenshot_20210407_143651

vs the html produced by sphinx + myst-nb:

Screenshot_20210407_143754

As you can see, the second #pragma line is formatted as a comment.

Environment

sphinx and myst-nb have been installed from conda-forge. The versions are 3.5.3 and 0.12.0 respectively.

welcome[bot] commented 3 years ago

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

chrisjsewell commented 3 years ago

thanks for the report, I guees this may be a problem with the pygments lexer: https://pygments.org/docs/lexers/#pygments.lexers.c_cpp.CppLexer

Can you provide an example notebook, i.e. I want to see what is set as the pygments lexer in the metadata:

{
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.1"
    }
  }
}
bluescarni commented 3 years ago

Hi @chrisjsewell , thanks for the quick reply.

Here is a sample notebook:

https://github.com/bluescarni/mppp/blob/master/doc/notebooks/integer_basics.ipynb

This is how it is rendered in sphinx:

https://bluescarni.github.io/mppp/notebooks/integer_basics.html

(you can launch it in binder from the rocket icon if you want)

chrisjsewell commented 3 years ago

Ok ta, there is no actual pygments lexer set:

 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },

so it will default to language_info/name, i.e. c++: https://github.com/executablebooks/MyST-NB/blob/a54dc18cb90c8845123f29f261132145ef2349b2/myst_nb/parser.py#L138-L139

So my next course of action would be to create a minimal example case, directly passing some text to pygments and see if this replicates the issue. If so, then open an issue on https://github.com/pygments/pygments

I can try this when I have some time, or obviously feel free to try yourself 😄