Skip to content

Commit 10058fa

Browse files
authored
Refactor fenced_code & codehilite options (#816)
* Add `language-` prefix to output when syntax highlighting is disabled for both codehilite and fenced_code extensions. * Add `lang_prefix` config option to customize the prefix. * Add a 'pygments' env to tox which runs the tests with Pygments installed. Pygments is locked to a specific version in the env. * Updated codehilite to accept any Pygments options. * Refactor fenced code attributes. - ID attr is defined on `pre` tag. - Add support for attr_list extension, which allows setting arbitrary attributes. - When syntax highlighting is enabled, any pygments options can be defined per block in the attr list. - For backward compatibility, continue to support `hi_lines` outside of an attr_list. That is the only attr other than lang which is allowed without the brackets (`{}`) of an attr list. Note that if the brackets exist, then everything, including lang and hl_lines, must be within them. * Resolves #775. Resolves #334. Addresses #652.
1 parent dbb9b3d commit 10058fa

File tree

18 files changed

+1799
-633
lines changed

18 files changed

+1799
-633
lines changed

.github/workflows/tox.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
fail-fast: false
2121
max-parallel: 4
2222
matrix:
23-
tox-env: [py35, py36, py37, py38, pypy3]
23+
tox-env: [py35, py36, py37, py38, pypy3, pygments]
2424
include:
2525
- tox-env: py35
2626
python-version: 3.5
@@ -32,6 +32,8 @@ jobs:
3232
python-version: 3.8
3333
- tox-env: pypy3
3434
python-version: pypy3
35+
- tox-env: pygments
36+
python-version: 3.7
3537

3638
env:
3739
TOXENV: ${{ matrix.tox-env }}

.spell-dict

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ElementTree
3333
encodings
3434
extendMarkdown
3535
Fauske
36-
Formatter
36+
formatter
3737
Fortin
3838
GitHub
3939
globals
@@ -77,6 +77,8 @@ PHP
7777
Posix
7878
postprocessor
7979
postprocessors
80+
prepend
81+
prepended
8082
preprocessor
8183
preprocessors
8284
Pygments

docs/change_log/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ title: Change Log
33
Python-Markdown Change Log
44
=========================
55

6-
version 3.3.3 (under development)
7-
8-
* Document limitations of `attr_list` extension (#965).
6+
Under development: version 3.3 ([Notes](release-3.3.md)).
97

108
May 8, 2020: version 3.2.2 (a bug-fix release).
119

@@ -15,7 +13,7 @@ May 8, 2020: version 3.2.2 (a bug-fix release).
1513
* Do not double escape entities in TOC.
1614
* Correctly report if an extension raises a `TypeError` (#939).
1715
* Raise a `KeyError` when attempting to delete a nonexistent key from the
18-
extension registry (#939).
16+
extension registry (#939).
1917
* Remove import of `packaging` (or `pkg_resources` fallback) entirely.
2018
* Remove `setuptools` as a run-time dependency (`install_required`).
2119

docs/change_log/release-3.3.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,56 @@ PyPy3.
77

88
## Backwards-incompatible changes
99

10+
### The prefix `language-` is now prepended to all language classes by default on code blocks.
11+
12+
The [HTML5 spec][spec] recommends that the class defining the language of a code block be prefixed with `language-`.
13+
Therefore, by default, both the [fenced_code] and [codehilite] extensions now prepend the prefix when code
14+
highlighting is disabled.
15+
16+
If you have previously been including the prefix manually in your fenced code blocks, then you will not want a second
17+
instance of the prefix. Similarly, if you are using a third party syntax highlighting tool which does not recognize
18+
the prefix, or requires a different prefix, then you will want to redefine the prefix globally using the `lang_prefix`
19+
configuration option of either the `fenced_code` or `codehilite` extensions.
20+
21+
For example, to configure `fenced_code` to not apply any prefix (the previous behavior), set the option to an empty string:
22+
23+
```python
24+
from markdown.extensions.fenced_code import FencedCodeExtension
25+
26+
markdown.markdown(src, extensions=[FencedCodeExtension(lang_prefix='')])
27+
```
28+
29+
!!! note
30+
When code highlighting is [enabled], the output from Pygments is used unaltered. Currently, Pygments does not
31+
provide an option to include the language class in the output, let alone prefix it. Therefore, any language prefix
32+
is only applied when syntax highlighting is disabled.
33+
1034
## New features
1135

36+
The following new features have been included in the 3.3 release:
37+
38+
* All Pygments' options are now available for syntax highlighting (#816).
39+
- The [Codehilite](../extensions/code_hilite.md) extension now accepts any options
40+
which Pygments supports as global configuration settings on the extension.
41+
- [Fenced Code Blocks](../extensions/fenced_code_blocks.md) will accept any of the
42+
same options on individual code blocks.
43+
- Any of the previously supported aliases to Pygments' options continue to be
44+
supported at this time. However, it is recommended that the Pygments option names
45+
be used directly to ensure continued compatibility in the future.
46+
47+
* [Fenced Code Blocks](../extensions/fenced_code_blocks.md) now work with
48+
[Attribute Lists](../extensions/attr_list.md) when syntax highlighting is disabled.
49+
Any random HTML attribute can be defined and set on the `<code>` tag of fenced code
50+
blocks when the `attr_list` extension is enabled (#816).
51+
1252
## Bug fixes
1353

1454
The following bug fixes are included in the 3.3 release:
1555

1656
* Fix issues with complex emphasis (#979).
57+
* Limitations of `attr_list` extension are Documented (#965).
58+
59+
[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
60+
[fenced_code]: ../extensions/fenced_code_blocks.md
61+
[codehilite]: ../extensions/code_hilite.md
62+
[enabled]: ../extensions/fenced_code_blocks.md#enabling-syntax-highlighting

docs/extensions/code_hilite.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,54 +185,67 @@ configuring extensions.
185185

186186
The following options are provided to configure the output:
187187

188-
* **`linenums`**:
189-
Use line numbers. Possible values are `True` for yes, `False` for no and
190-
`None` for auto. Defaults to `None`.
188+
* **`linenums`**{ #linenums }:
189+
An alias to Pygments' `linenos` formatter option. Possible values are `True` for yes, `False` for no and `None`
190+
for auto. Defaults to `None`.
191191

192192
Using `True` will force every code block to have line numbers, even when
193193
using colons (`:::`) for language identification.
194194

195195
Using `False` will turn off all line numbers, even when using shebangs
196196
(`#!`) for language identification.
197197

198-
* **`guess_lang`**:
198+
* **`guess_lang`**{ #guess_lang }:
199199
Automatic language detection. Defaults to `True`.
200200

201201
Using `False` will prevent Pygments from guessing the language, and thus
202202
highlighting blocks only when you explicitly set the language.
203203

204-
* **`css_class`**:
205-
Set CSS class name for the wrapper `<div>` tag. Defaults to
204+
* **`css_class`**{ #css_class }:
205+
An alias to Pygments `cssclass` formatter option. Set CSS class name for the wrapper `<div>` tag. Defaults to
206206
`codehilite`.
207207

208-
* **`pygments_style`**:
208+
* **`pygments_style`**{ #pygments_style }:
209209
Pygments HTML Formatter Style (`ColorScheme`). Defaults to `default`.
210210

211211
!!! Note
212212
This is useful only when `noclasses` is set to `True`, otherwise the
213213
CSS styles must be provided by the end user.
214214

215-
* **`noclasses`**:
215+
* **`noclasses`**{ #noclasses }:
216216
Use inline styles instead of CSS classes. Defaults to `False`.
217217

218-
* **`use_pygments`**:
218+
* **`use_pygments`**{ #use_pygments }:
219219
Specifies the use of Pygments in generating the output.
220220

221221
If `True` (the default) and Pygments is available, CodeHilite will use
222222
Pygments to analyze and format the output. Additionally, if using Pygments
223223
&gt;= 2.4, the output will be wrapped in `<code>` tags, whereas earlier
224224
versions will not.
225225

226-
Otherwise, Pygments will not be used. If a language is defined for a code
227-
block, it will be assigned to the `<code>` tag as a class in the manner
228-
suggested by the [HTML5 spec][spec] (alternate output will not be
229-
entertained) and may be used by a JavaScript library in the browser to
230-
highlight the code block.
231-
226+
Otherwise, Pygments will not be used. If a language is defined for a code block, it will be assigned to the
227+
`<code>` tag as a class in the manner suggested by the [HTML5 spec][spec] and may be used by a JavaScript library
228+
in the browser to highlight the code block. See the [`lang_prefix`](#lang_prefix) option to customize the prefix.
229+
230+
* **`lang_prefix`**{ #lang_prefix }:
231+
The prefix prepended to the language class assigned to the HTML `<code>` tag. Default: `language-`.
232+
233+
This option only applies when `use_pygments` is `False` as Pygments does not provide an option to include a
234+
language prefix.
235+
236+
237+
* Any other Pygments' options:
238+
239+
All other options are accepted and passed on to Pygments' lexer and formatter. Therefore,
240+
valid options include any options which are accepted by the [html formatter] or
241+
whichever [lexer] the code's language uses. Invalid options are ignored without error.
242+
232243
A trivial example:
233244

234245
```python
235246
markdown.markdown(some_text, extensions=['codehilite'])
236247
```
237248

249+
[html formatter]: https://pygments.org/docs/formatters/#HtmlFormatter
250+
[lexer]: https://pygments.org/docs/lexers/
238251
[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element

0 commit comments

Comments
 (0)