django / djangoproject.com

Source code to djangoproject.com
https://www.djangoproject.com/
BSD 3-Clause "New" or "Revised" License
1.86k stars 938 forks source link

Fixed #1453 -- Fixed code block indentantion on the Getting started page. #1452

Closed jackoconnordev closed 6 months ago

jackoconnordev commented 6 months ago

Description

This fixes code blocks on www.djangoproject.com/start page not displaying with correct Python indentation (and includes my best guess at correct PEP8 styling).

Fixes https://github.com/django/djangoproject.com/issues/1453

Fix

The DjHTML code indenter was automatically changing the indentation for the code blocks which were colorised by {% pygment 'python' %}. This led to incorrectly formatted Python code which would neither pass a Python interpreter's indentation checks or conform to PEP8.

The DjHTML indenter can be temporarily turned off for certain lines by making use of {# fmt:off #} and {# fmt:on #}. This is the method I think works best in this specific use-case. See https://github.com/rtts/djhtml#fmtoff-and-fmton.

\
 vs fmt:off

The DjHTML indentation can also be stopped from being applied by using <pre> tags. However, the <pre> tags cause lots of extra vertical whitespace on the website so I feel just disabling the formatter is the better way to go.

If pre tags are included within the pygment block then they get treated as plain text instead of markup. If pre tags wrap the pygment block then the newlines at the end of the open and close pygment lines lead to the increased vertical white space since whitespace is preserved

Screenshots

I have the current djangoproject.com/start/ page vs this PR's djangoproject/start/ page visualised side-by-side. Old on the left, new on the right.

Admin section

admin

Forms and Authentications sections

forms-and-authentication

Internationalisation section

internationalisation

 Object Relational Mapper section

object-relational-mapper

URLs and Views section

urls-and-views
jackoconnordev commented 6 months ago

This is an example of a code-block from a page on djangoproject.com which was generated by sphinx. The code starts from the left-most side of the code block.

image
jackoconnordev commented 6 months ago

Added a bit more elaboration on why I went with turning off the indentor vs using pre tags to the original PR description.

felixxm commented 6 months ago

@jackoconnordev Thanks :+1: Welcome aboard :boat:

felixxm commented 6 months ago

Merged in 69e8325f92d402f3ccb91c207d2ae2d7daaca006.

jackoconnordev commented 6 months ago

Woohoo 🥳. Thanks @felixxm