Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for conditional blocks in README.rst #6901

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mhucka
Copy link
Contributor

@mhucka mhucka commented Dec 31, 2024

Due to differences in reStructuredText support between GitHub and PyPI, some basic things like centering images are impossible to do purely in RST in such a way that it works on both GitHub and PyPI. (This is not the case with Markdown; both PyPI and GitHub support some limited HTML inside Markdown, and that makes it possible to have the same Markdown README file work on both PyPI and GitHub.)

Since we are sticking with RST format for the top-level module README files, I wanted to have a way to create an attractive project README file for both GitHub and PyPI without having to keep two separate files. However, that meant solving the following problem.

PyPI respects RST's :align: attributes for images, but GitHub ignores it. PyPI also supports RST's .. class:: directive, but GitHub ignores it. Consequently, the only way to center text or images in RST for GitHub is to use raw HTML via the RST .. raw:: directive, but here's the catch: it turns out that PyPI not only doesn't support raw – it actually throws an error and refuses your upload if your README file contains .. raw:: constructs.

What to do? Well, we can use .. raw:: html as long as we remove those constructs in the version of the file we save in the distribution for PyPI. Further, since GitHub simply ignores or strips out RST directives like .. class::, we can leave those constructs in the file that is sent to PYPI, so that at least they do what we want them there. So, in summary:

  1. Make the unmodified README.rst file work for GitHub, because that's what people will see when they visit the Cirq repo on GitHub.

  2. Process the README.rst file when producing distributions for PyPI, to remove content delimited by specific start and end markers that indicate parts that are valid on GitHub but not PyPI.

  3. Use normal RST constructs that work on PyPI but not GitHub (that is, leave them in the file), to accomplish on PyPI what we can't do for GitHub without using raw HTML.

That's the approach is used in the new updated top-level README.rst file for Cirq, and it is enabled by this small modification to the setup.py file.

Due to differences in reStructuredText support between GitHub and
PyPI, some basic things like centering images are impossible to do
purely in RST in such a way that it works on both GitHub and
PyPI.:(This is not the case with Markdown; both PyPI and GitHub
support some limited HTML inside Markdown, and that makes it possible
to have the same Markdown README file work on both PyPI and GitHub.)

Since we are sticking with RST format for the top-level module README
files, I wanted to have a way to create an attractive project README
file for both GitHub and PyPI without having to keep two separate
files. However, that meant solving the following problem.

PyPI supports RST's `.. class::` directive, but GitHub does not. PyPI
also respects `:align:` attributes for images, but GitHub does not.
Consequently, the only way to center text or images in RST for GitHub
is to use raw HTML via the RST `.. raw::` directive, but it turns out
that PyPI not only _doesn't support `raw`_ – it actually _throws an
error and refuses your upload_ if your README file contains `.. raw::`
constructs.

What to do? Well, we _can_ use `.. raw:: html` as long as we remove
those constructs in the version of the file we save in the
distribution for PyPI. Further, since GitHub simply ignores or strips
out RST directives like `.. class::`, we can leave _those_ constructs
in the file that is sent to PYPI, and make use of them to do what we
can't do in GitHub purely in RST. So, in summary:

1. Make the unmodified README.rst file work for GitHub, because that's
   what people will see when they visit the repo on GitHub.

2. Filter the README.rst file when producing distributions for PyPI,
   to remove content delimited by specific start and end markers that
   indicate parts that are valid on GitHub but not PyPI.

3. Use normal RST constructs that work on PyPI but not GitHub to
   accomplish what we can't do for GitHub without using raw HTML.

That's the approach is used in the new updated top-level README.rst
file for Cirq, and it is enabled by this small modification to the
`setup.py` file.
@CirqBot CirqBot added the size: M 50< lines changed <250 label Dec 31, 2024
Copy link

codecov bot commented Dec 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.86%. Comparing base (06f12b9) to head (e7f02b9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6901   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files        1084     1086    +2     
  Lines       94225    94284   +59     
=======================================
+ Hits        92215    92273   +58     
- Misses       2010     2011    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Need to provide `tempfile.NamedTemporaryFile` with the argument
`encoding='utf-8'` or else on Windows, we get an exception when
working with delimiters that contain uncommon Unicode characters:

```
def encode(self, input, final=False):
>   return codecs.charmap_encode(input,self.errors,encoding_table)[0]
E   UnicodeEncodeError: 'charmap' codec can't encode characters in
    position 19-23: character maps to <undefined>
```
It turns out that on Windows, you can't open a temp file while inside
a the context handler "with tempfile.NamedTemporaryFile...". See
https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
@mhucka mhucka marked this pull request as ready for review December 31, 2024 18:07
@mhucka mhucka requested review from vtomole and a team as code owners December 31, 2024 18:07
@mhucka mhucka self-assigned this Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants