diff --git a/book/syntax_exercises/012.md b/book/syntax_exercises/012.md index 2b60263..59b195d 100644 --- a/book/syntax_exercises/012.md +++ b/book/syntax_exercises/012.md @@ -49,7 +49,7 @@ Important: Save your work frequently. - `danger` -## Custom titles for admonitions +## Custom titles & styles for admonitions 5. Some admonitions allow you to **specify your own title**. Try the `admonition` directive: @@ -59,7 +59,6 @@ You can customise the title of this block using the `admonition` directive. ``` ```` - 6. Similarly, you can customise titles for `dropdown` admonitions: ````md @@ -77,6 +76,7 @@ This content is hidden until the user clicks the title. 3. Preview the changes. ``` ```` + 8. Check your understanding with the summary table below. ```{admonition} Summary: Fixed vs. Custom Title Admonitions @@ -87,13 +87,132 @@ Here’s a quick overview: | `note`, `tip`, `warning`, `attention`, `caution`, `important`, `seealso`, `hint`, `danger`, `error` | `admonition`, `dropdown` | ``` -9. When you are ready, commit your changes to the repository by clicking on the green `Commit changes` button. +9. For these admonitions that allow custom titles (`admonition`, `dropdown`), you can also apply a **custom style** using the `:class:` option. This can be used to change the appearance (e.g. colour or layout) of the block. Try the following examples: -10. Add a commit message. +````md +```{admonition} Pro tip! +:class: tip -11. To see your changes, go to {octicon}`play` `Actions` - The most recent workflow run `overview.md / the commit message of the commit you just made` - Wait for it to finish - In the summary, click on the link of your book shown in the table `Branches deployed` and under `Primary book at root` (getting bored of waiting? There'll be exercising on doing this locally which prevents you from waiting). +You can apply styles using the `:class:` option. +``` +```` -12. Do you see your change? If you don't see it click `CTRL`+`F5`/`Control`+`F5`to refresh the page. +````md +```{dropdown} Save your work +:class: warning + +Don't forget to save your changes regularly to avoid data loss! +``` +```` + +10. You can apply the `:class: dropdown` to any `admonition`, even if it is not originally a dropdown block. This will allow the admonition to be collapsible, making it useful for sections where you'd like to hide additional content until the reader clicks to expand it. Test this out: + +````md +```{admonition} Extra Info +:class: dropdown + +This content will be hidden until the reader clicks to reveal it. +``` +```` + +11. The table below summarizes the classes that can be used together with the `admonition` and `dropdown`admonitions. Try customize admonitions yourself, using different classes from the table. + + +| Class name | Effect | +|------------------|------------------------------------------------------------------------| +| `tip` | Gives the block a green background (styled like a helpful tip) | +| `warning` | Orange background with a warning-style border | +| `caution` | Yellow caution-style background | +| `error` / `danger` | Red background indicating errors or important warnings | +| `dropdown` | Makes the block collapsible (content is hidden until clicked) | +| `seealso` | Blue background, often used for related info or references | +| `important` | Dark blue or purple background for emphasis | +| `note` | Subtle grey note block | + +12. You can also use **multiple classes** at once by separating them with spaces. For example, `:class: important dropdown` will give you a collapsible, important-looking block. Try mixing and matching these, using the example below as a base: + + +````md +```{admonition} Important: Check this out! +:class: important dropdown + +This content is both highlighted as important and collapsible. +``` +```` + + +## Custom colours for admonitions + +13. It is also possible to use specific colours to customize your admonitions, using built-in Sphinx colour classes. To do so, it is necessary to follow the installation process outlined below: + +**Step A: Install the Package** + +Install the `sphinx-named-colors` package using `pip`: + +``` +pip install sphinx-named-colors +``` + +**Step B: Add to `requirements.txt`** + +Ensure that the package is included in your project's `requirements.txt` to track the dependency: + +``` +sphinx-named-colors +``` + +**Step C: Enable in `_config.yml`** + +In your `_config.yml` file, add the extension to the list of Sphinx extra extensions: + +``` +sphinx: + extra_extensions: + . + . + . + - sphinx_named_colors + . + . + . +``` + +14. Once the installation is done, we can experiment with custom-coloured admonitions! Go ahead and look at the available [Sphinx Named Colours](https://teachbooks.io/manual/external/Sphinx-Named-Colors/README.html#admonitions), and pick a desired colour. + +15. Once you have chosen a colour, coloured admonitions can be generated in two ways, depending on how you'd like to structure your content. The first method is by adding the name-colour via `:class:` to an existing `admonition` or `dropdown` type block, similar to what was done above. This can be done as follows: + +````md +```{admonition} Custom Colour Example +:class: darkred + +This is a custom coloured admonition using `:class: darkred`. +``` +```` + +16. When using the above method, a new class is introduced: `no-title`. This suppresses printing of the title bar, even if the title is given. Try this out by adding this class to the above code. + +17. The second method of adding coloured admonitions is by directly creating an admonition based on a colour. This method can be used if you'd rather not use the generic type and want a minimal syntax. In this case, the title is optional, and if omitted, the title bar will not appear. Try this out with the code below: + + +````md +```{darkgreen} Optional Title + +This is a green-styled custom admonition. +``` +```` + +18. For the second method using named colour admonitions, a new class is also introduced: `show-bar`. This allows titleless admonitions, which forces printing of the title bar. Try this out using the code above with an omitted title, and this added class. + +19. For both methods, you can also combine classes here, for example, `:class: darkblue dropdown` to make it collapsible and coloured. Test this out, building on the above examples. + + +20. When you are ready, commit your changes to the repository by clicking on the green `Commit changes` button. + +21. Add a commit message. + +22. To see your changes, go to {octicon}`play` `Actions` - The most recent workflow run `overview.md / the commit message of the commit you just made` - Wait for it to finish - In the summary, click on the link of your book shown in the table `Branches deployed` and under `Primary book at root` (getting bored of waiting? There'll be exercising on doing this locally which prevents you from waiting). + +23. Do you see your change? If you don't see it click `CTRL`+`F5`/`Control`+`F5`to refresh the page. ```{admonition} Check your understanding :class: note @@ -102,6 +221,7 @@ Before moving on, make sure you understand: - Which admonitions allow custom titles, and which don’t? - What is the purpose of a dropdown admonition? - When might you use a `note` vs. an `admonition`? +- In which ways can you further customize `admonition` blocks? ```