MyST-NB icon indicating copy to clipboard operation
MyST-NB copied to clipboard

Rendering issue with C++ notebooks

Open bluescarni opened this issue 4 years ago • 4 comments

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.

bluescarni avatar Apr 07 '21 12:04 bluescarni

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:

welcome[bot] avatar Apr 07 '21 12:04 welcome[bot]

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"
    }
  }
}

chrisjsewell avatar Apr 07 '21 13:04 chrisjsewell

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)

bluescarni avatar Apr 07 '21 13:04 bluescarni

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 😄

chrisjsewell avatar Apr 07 '21 13:04 chrisjsewell