|
6 | 6 |
|
7 | 7 | ## Introduction |
8 | 8 |
|
9 | | -A custom superfence for pymdown-extensions that can filters lines and plays nice with MkDocs. |
| 9 | +A custom superfence for pymdown-extensions that can filters lines and plays |
| 10 | +nice with MkDocs. |
10 | 11 |
|
11 | | -## Supported Platforms |
| 12 | +This is particularly useful when you want to hide some comments or some |
| 13 | +boilerplate code from the documentation for any reason. |
12 | 14 |
|
13 | | -The following platforms are officially supported (tested): |
| 15 | +A typical use case is when you are testing your examples in the documentation, |
| 16 | +so you might need to add some initialization code, or importing some dependencies |
| 17 | +that are not relevant for the point you are trying to make in the |
| 18 | +documentation, but you still need the example code to work to make sure they are |
| 19 | +not inadvertedly broken. |
14 | 20 |
|
15 | | -- **Python:** 3.11 |
16 | | -- **Operating System:** Ubuntu Linux 20.04 |
17 | | -- **Architectures:** amd64, arm64 |
| 21 | +## Quick Example |
| 22 | + |
| 23 | +When writing some documentation, and you want to show some code block, but |
| 24 | +you want to show only some lines, you can use this superfence as follows: |
| 25 | + |
| 26 | +~~~markdown |
| 27 | +```text show_lines=":2,4,7,10:12,15:" |
| 28 | +This is line 1 |
| 29 | +This is line 2 |
| 30 | +This is line 3 |
| 31 | +This is line 4 |
| 32 | +This is line 5 |
| 33 | +This is line 6 |
| 34 | +This is line 7 |
| 35 | +This is line 8 |
| 36 | +This is line 9 |
| 37 | +This is line 10 |
| 38 | +This is line 11 |
| 39 | +This is line 12 |
| 40 | +This is line 13 |
| 41 | +This is line 14 |
| 42 | +This is line 15 |
| 43 | +This is line 16 |
| 44 | +This is line 17 |
| 45 | +``` |
| 46 | +~~~ |
| 47 | + |
| 48 | +This will show the following block of code in the rendered output: |
| 49 | + |
| 50 | +```text |
| 51 | +This is line 1 |
| 52 | +This is line 2 |
| 53 | +This is line 4 |
| 54 | +This is line 7 |
| 55 | +This is line 10 |
| 56 | +This is line 11 |
| 57 | +This is line 12 |
| 58 | +This is line 15 |
| 59 | +This is line 16 |
| 60 | +This is line 17 |
| 61 | +``` |
| 62 | + |
| 63 | +See [Usage](#usage) for a more detailed explanation of the available options. |
| 64 | + |
| 65 | +## Configuration |
| 66 | + |
| 67 | +### MkDocs |
| 68 | + |
| 69 | +To use this superfence with [MkDocs](https://www.mkdocs.org/), you can use |
| 70 | +something like this: |
| 71 | + |
| 72 | +```yaml |
| 73 | +markdown_extensions: |
| 74 | + - pymdownx.superfences: |
| 75 | + custom_fences: |
| 76 | + - name: "*" |
| 77 | + class: "highlight" |
| 78 | + format: !!python/name:frequenz.pymdownx.superfences.filter_lines.do_format |
| 79 | + validator: !!python/name:frequenz.pymdownx.superfences.filter_lines.do_validate |
| 80 | +``` |
| 81 | +
|
| 82 | +### Standalone |
| 83 | +
|
| 84 | +To use this superfence standalone, you can use something like this: |
| 85 | +
|
| 86 | +```python |
| 87 | +import markdown |
| 88 | +from frequenz.pymdownx.superfences.filter_lines import do_format, do_validate |
| 89 | + |
| 90 | +html = markdown.markdown( |
| 91 | + markdown_source, |
| 92 | + extensions=['pymdownx.superfences'], |
| 93 | + extension_configs={ |
| 94 | + "pymdownx.superfences": { |
| 95 | + "custom_fences": [ |
| 96 | + { |
| 97 | + 'name': '*', |
| 98 | + 'class': 'highlight', |
| 99 | + 'validator': do_validate, |
| 100 | + 'format': do_format |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + } |
| 105 | +) |
| 106 | + |
| 107 | +print(html) |
| 108 | +``` |
| 109 | + |
| 110 | +## Usage |
| 111 | + |
| 112 | +See [Quick Example](#quick-example) for an example. |
| 113 | + |
| 114 | +The superfence supports the following options: |
| 115 | + |
| 116 | +### `show_lines` |
| 117 | + |
| 118 | +A comma separated list of line numbers or ranges of line numbers to show. |
| 119 | + |
| 120 | +The line numbers are 1-based. If any line number is zero or negative, a warning |
| 121 | +is logged and the line or range are ignored. |
| 122 | + |
| 123 | +If `show_lines` is omitted, it defaults to showing all lines. |
| 124 | + |
| 125 | +#### Ranges |
| 126 | + |
| 127 | +Ranges are inclusive and are defined as follows: |
| 128 | + |
| 129 | +* The ranges are specified as `start:end`, where `start` and `end` are the line |
| 130 | + numbers of the first and last lines to show, respectively. |
| 131 | + |
| 132 | +* If `start` is omitted, it defaults to the first line. If `end` is omitted, it |
| 133 | + defaults to the last line. |
| 134 | + |
| 135 | +* If `start` is greater than `end`, a warning is logged and the range is |
| 136 | + ignored. |
| 137 | + |
| 138 | +* If `start` is greater than the number of lines in the code block, the range |
| 139 | + is ignored. |
| 140 | + |
| 141 | +* If `end` is greater than the number of lines in the code block, it is set to |
| 142 | + the number of lines in the code block. |
18 | 143 |
|
19 | 144 | ## Contributing |
20 | 145 |
|
|
0 commit comments