Skip to content

Refactor fenced code attributes #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
tox-env: [py35, py36, py37, py38, pypy3]
tox-env: [py35, py36, py37, py38, pypy3, pygments]
include:
- tox-env: py35
python-version: 3.5
Expand All @@ -32,6 +32,8 @@ jobs:
python-version: 3.8
- tox-env: pypy3
python-version: pypy3
- tox-env: pygments
python-version: 3.7

env:
TOXENV: ${{ matrix.tox-env }}
Expand Down
4 changes: 3 additions & 1 deletion .spell-dict
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ElementTree
encodings
extendMarkdown
Fauske
Formatter
formatter
Fortin
GitHub
globals
Expand Down Expand Up @@ -77,6 +77,8 @@ PHP
Posix
postprocessor
postprocessors
prepend
prepended
preprocessor
preprocessors
Pygments
Expand Down
6 changes: 2 additions & 4 deletions docs/change_log/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ title: Change Log
Python-Markdown Change Log
=========================

version 3.3.3 (under development)

* Document limitations of `attr_list` extension (#965).
Under development: version 3.3 ([Notes](release-3.3.md)).

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

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

Expand Down
46 changes: 46 additions & 0 deletions docs/change_log/release-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,56 @@ PyPy3.

## Backwards-incompatible changes

### The prefix `language-` is now prepended to all language classes by default on code blocks.

The [HTML5 spec][spec] recommends that the class defining the language of a code block be prefixed with `language-`.
Therefore, by default, both the [fenced_code] and [codehilite] extensions now prepend the prefix when code
highlighting is disabled.

If you have previously been including the prefix manually in your fenced code blocks, then you will not want a second
instance of the prefix. Similarly, if you are using a third party syntax highlighting tool which does not recognize
the prefix, or requires a different prefix, then you will want to redefine the prefix globally using the `lang_prefix`
configuration option of either the `fenced_code` or `codehilite` extensions.

For example, to configure `fenced_code` to not apply any prefix (the previous behavior), set the option to an empty string:

```python
from markdown.extensions.fenced_code import FencedCodeExtension

markdown.markdown(src, extensions=[FencedCodeExtension(lang_prefix='')])
```

!!! note
When code highlighting is [enabled], the output from Pygments is used unaltered. Currently, Pygments does not
provide an option to include the language class in the output, let alone prefix it. Therefore, any language prefix
is only applied when syntax highlighting is disabled.

## New features

The following new features have been included in the 3.3 release:

* All Pygments' options are now available for syntax highlighting (#816).
- The [Codehilite](../extensions/code_hilite.md) extension now accepts any options
which Pygments supports as global configuration settings on the extension.
- [Fenced Code Blocks](../extensions/fenced_code_blocks.md) will accept any of the
same options on individual code blocks.
- Any of the previously supported aliases to Pygments' options continue to be
supported at this time. However, it is recommended that the Pygments option names
be used directly to ensure continued compatibility in the future.

* [Fenced Code Blocks](../extensions/fenced_code_blocks.md) now work with
[Attribute Lists](../extensions/attr_list.md) when syntax highlighting is disabled.
Any random HTML attribute can be defined and set on the `<code>` tag of fenced code
blocks when the `attr_list` extension is enabled (#816).

## Bug fixes

The following bug fixes are included in the 3.3 release:

* Fix issues with complex emphasis (#979).
* Limitations of `attr_list` extension are Documented (#965).

[spec]: https://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
[fenced_code]: ../extensions/fenced_code_blocks.md
[codehilite]: ../extensions/code_hilite.md
[enabled]: ../extensions/fenced_code_blocks.md#enabling-syntax-highlighting
43 changes: 28 additions & 15 deletions docs/extensions/code_hilite.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,54 +185,67 @@ configuring extensions.

The following options are provided to configure the output:

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

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

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

* **`guess_lang`**:
* **`guess_lang`**{ #guess_lang }:
Automatic language detection. Defaults to `True`.

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

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

* **`pygments_style`**:
* **`pygments_style`**{ #pygments_style }:
Pygments HTML Formatter Style (`ColorScheme`). Defaults to `default`.

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

* **`noclasses`**:
* **`noclasses`**{ #noclasses }:
Use inline styles instead of CSS classes. Defaults to `False`.

* **`use_pygments`**:
* **`use_pygments`**{ #use_pygments }:
Specifies the use of Pygments in generating the output.

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

Otherwise, Pygments will not be used. If a language is defined for a code
block, it will be assigned to the `<code>` tag as a class in the manner
suggested by the [HTML5 spec][spec] (alternate output will not be
entertained) and may be used by a JavaScript library in the browser to
highlight the code block.

Otherwise, Pygments will not be used. If a language is defined for a code block, it will be assigned to the
`<code>` tag as a class in the manner suggested by the [HTML5 spec][spec] and may be used by a JavaScript library
in the browser to highlight the code block. See the [`lang_prefix`](#lang_prefix) option to customize the prefix.

* **`lang_prefix`**{ #lang_prefix }:
The prefix prepended to the language class assigned to the HTML `<code>` tag. Default: `language-`.

This option only applies when `use_pygments` is `False` as Pygments does not provide an option to include a
language prefix.


* Any other Pygments' options:

All other options are accepted and passed on to Pygments' lexer and formatter. Therefore,
valid options include any options which are accepted by the [html formatter] or
whichever [lexer] the code's language uses. Invalid options are ignored without error.

A trivial example:

```python
markdown.markdown(some_text, extensions=['codehilite'])
```

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