|
1 | | -.. include:: ../README.rst |
| 1 | +=================== |
| 2 | +sphinxext-rediraffe |
| 3 | +=================== |
| 4 | + |
| 5 | +.. role:: code-py(code) |
| 6 | + :language: Python |
| 7 | + |
| 8 | +This Sphinx extension redirects non-existent pages to working pages. |
| 9 | +Rediraffe can also check that deleted or renamed files in your git repo |
| 10 | +are redirected. |
| 11 | + |
| 12 | +Rediraffe creates a graph of all specified redirects and traverses it |
| 13 | +to point all internal urls to leaf urls. |
| 14 | +This means that chained redirects will be resolved. |
| 15 | +For example, if a config has 6 chained redirects, all 6 links will redirect |
| 16 | +directly to the final link. |
| 17 | +The end user will never experience more than 1 redirection. |
| 18 | + |
| 19 | +Note: Rediraffe supports the html and dirhtml builders. |
| 20 | + |
| 21 | +Installation |
| 22 | +============ |
| 23 | + |
| 24 | +.. code-block:: sh |
| 25 | +
|
| 26 | + python -m pip install sphinxext-rediraffe |
| 27 | +
|
| 28 | +Usage |
| 29 | +===== |
| 30 | + |
| 31 | +Just add ``sphinxext.rediraffe`` to the extensions list in :file:`conf.py`, |
| 32 | + |
| 33 | +.. code-block:: python |
| 34 | +
|
| 35 | + extensions = [ |
| 36 | + 'sphinxext.rediraffe', |
| 37 | + ] |
| 38 | +
|
| 39 | +and set :confval:`rediraffe_redirects` to a dict or file of redirects. |
| 40 | + |
| 41 | +Diff Checker |
| 42 | +------------ |
| 43 | + |
| 44 | +The diff checker ensures that deleted or renamed files in your git repo |
| 45 | +are in your redirects. |
| 46 | + |
| 47 | +To run the diff checker: |
| 48 | + |
| 49 | +1. Set :confval:`rediraffe_branch` and :confval:`rediraffe_redirects` |
| 50 | + in :file:`conf.py`. |
| 51 | +2. Run the ``rediraffecheckdiff`` builder. |
| 52 | + |
| 53 | +Auto Redirect builder |
| 54 | +--------------------- |
| 55 | + |
| 56 | +The auto redirect builder can be used to automatically add renamed files |
| 57 | +to your redirects file. |
| 58 | +Simply run the ``rediraffewritediff`` builder. |
| 59 | + |
| 60 | +To run the auto redirecter: |
| 61 | + |
| 62 | +1. Set :confval:`rediraffe_branch` and :confval:`rediraffe_redirects` |
| 63 | + in :file:`conf.py`. |
| 64 | +2. Run the ``rediraffewritediff`` builder. |
| 65 | + |
| 66 | +Note: The auto redirect builder only works with a configuration file. |
| 67 | + |
| 68 | +Note: Deleted files cannot be added to your redirects file automatically. |
| 69 | + |
| 70 | + |
| 71 | +Options |
| 72 | +======= |
| 73 | + |
| 74 | +These values are placed in the :file:`conf.py` of your Sphinx project. |
| 75 | + |
| 76 | +.. confval:: rediraffe_branch |
| 77 | + :type: :code-py:`str` |
| 78 | + :default: :code-py:`''` |
| 79 | + |
| 80 | + **Required** for the ``rediraffecheckdiff`` & ``rediraffewritediff`` builders. |
| 81 | + The branch or commit to diff against. |
| 82 | + |
| 83 | +.. confval:: rediraffe_redirects |
| 84 | + :type: :code-py:`str | dict[str, str]` |
| 85 | + |
| 86 | + A filename or dict containing redirects. |
| 87 | + |
| 88 | +.. confval:: rediraffe_template |
| 89 | + :type: :code-py:`str` |
| 90 | + :default: :code-py:`None` |
| 91 | + |
| 92 | + A jinja template to use to render the inserted redirecting files. |
| 93 | + If not specified, a default template will be used. |
| 94 | + This template will only be accessed after the html/htmldir builder is finished, |
| 95 | + meaning that this file may be generated as part of your build. |
| 96 | + |
| 97 | + Variables available to :confval:`!rediraffe_template`: |
| 98 | + |
| 99 | + ``from_file`` |
| 100 | + the file being redirected as written in :confval:`rediraffe_redirects`. |
| 101 | + ``to_file`` |
| 102 | + the destination file that from_file is redirected to as written in |
| 103 | + :confval:`rediraffe_redirects`. |
| 104 | + ``from_url`` |
| 105 | + the path to ``from_url``'s html file (built by rediraffe) relative to the |
| 106 | + :confval:`!outdir`. |
| 107 | + ``to_url`` |
| 108 | + the path to ``to_url``'s built html file relative to the :confval:`!outdir`. |
| 109 | + ``rel_url`` |
| 110 | + the relative path from ``from_url`` to ``to_url``. |
| 111 | + |
| 112 | +.. confval:: rediraffe_auto_redirect_perc |
| 113 | + :type: :code-py:`int` |
| 114 | + :default: :code-py:`100` |
| 115 | + |
| 116 | + Only used by the ``rediraffewritediff`` builder. |
| 117 | + The percentage as an integer representing the accuracy required before |
| 118 | + auto redirecting with the ``rediraffewritediff`` builder. |
| 119 | + |
| 120 | +Example Config |
| 121 | +============== |
| 122 | + |
| 123 | +redirects only (file) |
| 124 | +--------------------- |
| 125 | + |
| 126 | +:file:`conf.py`: |
| 127 | + |
| 128 | +.. code-block:: python |
| 129 | +
|
| 130 | + rediraffe_redirects = 'redirects.txt' |
| 131 | +
|
| 132 | +:file:`redirects.txt`: |
| 133 | + |
| 134 | +.. code-block:: text |
| 135 | +
|
| 136 | + # comments start with '#' |
| 137 | + 'another file.rst' index.rst |
| 138 | + another2.rst "another file.rst" |
| 139 | +
|
| 140 | +Note: Filepaths can be wrapped in quotes (single or double). |
| 141 | +This is especially useful for filepaths containing spaces. |
| 142 | + |
| 143 | +redirects only (dict) |
| 144 | +--------------------- |
| 145 | + |
| 146 | +:file:`conf.py`: |
| 147 | + |
| 148 | +.. code-block:: python |
| 149 | +
|
| 150 | + rediraffe_redirects = { |
| 151 | + 'another.rst': 'index.rst', |
| 152 | + 'another2.rst': 'another.rst', |
| 153 | + } |
| 154 | +
|
| 155 | +redirects + diff checker |
| 156 | +------------------------ |
| 157 | + |
| 158 | +:file:`conf.py`: |
| 159 | + |
| 160 | +.. code-block:: python |
| 161 | +
|
| 162 | + rediraffe_redirects = 'redirects.txt' |
| 163 | + rediraffe_branch = 'main~1' |
| 164 | +
|
| 165 | +redirects with jinja template |
| 166 | +----------------------------- |
| 167 | + |
| 168 | +:file:`conf.py`: |
| 169 | + |
| 170 | +.. code-block:: python |
| 171 | +
|
| 172 | + rediraffe_redirects = 'redirects.txt' |
| 173 | + rediraffe_template = 'template.html' |
| 174 | +
|
| 175 | +:file:`template.html`: |
| 176 | + |
| 177 | +.. code-block:: html |
| 178 | + |
| 179 | + <html> |
| 180 | + <body> |
| 181 | + <p>Your destination is {{to_url}}</p> |
| 182 | + </body> |
| 183 | + </html> |
| 184 | + |
| 185 | +A complex example can be found at :file:`tests/roots/ext/`. |
| 186 | + |
| 187 | +Testing |
| 188 | +======= |
| 189 | + |
| 190 | +Rediraffe uses pytest for testing. |
| 191 | +To run tests: |
| 192 | + |
| 193 | +1. Install this package |
| 194 | +2. Install test dependencies |
| 195 | + |
| 196 | + .. code-block:: sh |
| 197 | +
|
| 198 | + python -m pip install --group test |
| 199 | +
|
| 200 | +3. Navigate to the tests directory and run |
| 201 | + |
| 202 | + .. code-block:: sh |
| 203 | +
|
| 204 | + python -m pytest --headless |
| 205 | +
|
| 206 | +The ``--headless`` flag ensures that a browser window does not open |
| 207 | +during browser backed selenium testing. |
0 commit comments