From 1943dfcba66250d7add5d751fb81fca1fdc49878 Mon Sep 17 00:00:00 2001 From: Helge Date: Sun, 22 Dec 2024 10:28:19 +0100 Subject: [PATCH] Add two additional examples for the documentation This should help with issues: - https://github.com/messense/nh3/issues/36 - https://github.com/messense/nh3/issues/70 --- src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 17eba74..56048cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,38 @@ use pyo3::types::{PyString, PyTuple}; /// 'hi' /// >>> nh3.clean("XSS?") /// 'XSS?' +/// +/// Example of using ``attribute_filter``: +/// +/// .. code-block:: pycon +/// +/// >>> from copy import deepcopy +/// >>> attributes = deepcopy(nh3.ALLOWED_ATTRIBUTES) +/// >>> attributes["a"].add("class") +/// >>> def attribute_filter(tag, attr, value): +/// ... if tag == "a" and attr == "class": +/// ... if "mention" in value.split(" "): +/// ... return "mention" +/// ... return None +/// ... return value +/// >>> nh3.clean("@foo", +/// ... attributes=attributes, +/// ... attribute_filter=attribute_filter) +/// '@foo' +/// +/// Example of maintaining the ``rel`` attribute: +/// +/// .. code-block:: pycon +/// +/// >>> from copy import deepcopy +/// >>> attributes = deepcopy(nh3.ALLOWED_ATTRIBUTES) +/// >>> attributes["a"].add("rel") +/// >>> nh3.clean("", +/// ... link_rel=None, attributes=attributes) +/// '' + + + #[pyfunction(signature = ( html, tags = None,