-
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: move python public API to djc-core create #13
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
JuroOravec
merged 3 commits into
main
from
jo-refactor-move-rust-python-api-to-djc-core
Oct 23, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,76 @@ | ||
| use djc_html_transformer::set_html_attributes; | ||
| use djc_html_transformer::{ | ||
| set_html_attributes as set_html_attributes_rust, HtmlTransformerConfig, | ||
| }; | ||
| use pyo3::exceptions::{PyValueError}; | ||
| use pyo3::prelude::*; | ||
| use pyo3::types::{PyDict, PyTuple}; | ||
|
|
||
| /// A Python module implemented in Rust for high-performance transformations. | ||
| /// Singular Python API that brings togther all the other Rust crates. | ||
| #[pymodule] | ||
| fn djc_core(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
| // HTML transformer | ||
| m.add_function(wrap_pyfunction!(set_html_attributes, m)?)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Transform HTML by adding attributes to the elements. | ||
| /// | ||
| /// Args: | ||
| /// html (str): The HTML string to transform. Can be a fragment or full document. | ||
| /// root_attributes (List[str]): List of attribute names to add to root elements only. | ||
| /// all_attributes (List[str]): List of attribute names to add to all elements. | ||
| /// check_end_names (bool, optional): Whether to validate matching of end tags. Defaults to false. | ||
| /// watch_on_attribute (str, optional): If set, captures which attributes were added to elements with this attribute. | ||
| /// | ||
| /// Returns: | ||
| /// Tuple[str, Dict[str, List[str]]]: A tuple containing: | ||
| /// - The transformed HTML string | ||
| /// - A dictionary mapping captured attribute values to lists of attributes that were added | ||
| /// to those elements. Only returned if watch_on_attribute is set, otherwise empty dict. | ||
| /// | ||
| /// Example: | ||
| /// >>> html = '<div data-id="123"><p>Hello</p></div>' | ||
| /// >>> html, captured = set_html_attributes(html, ['data-root-id'], ['data-v-123'], watch_on_attribute='data-id') | ||
| /// >>> print(captured) | ||
| /// {'123': ['data-root-id', 'data-v-123']} | ||
| /// | ||
| /// Raises: | ||
| /// ValueError: If the HTML is malformed or cannot be parsed. | ||
| #[pyfunction] | ||
| #[pyo3(signature = (html, root_attributes, all_attributes, check_end_names=None, watch_on_attribute=None))] | ||
| #[pyo3( | ||
| text_signature = "(html, root_attributes, all_attributes, *, check_end_names=False, watch_on_attribute=None)" | ||
| )] | ||
| pub fn set_html_attributes( | ||
| py: Python, | ||
| html: &str, | ||
| root_attributes: Vec<String>, | ||
| all_attributes: Vec<String>, | ||
| check_end_names: Option<bool>, | ||
| watch_on_attribute: Option<String>, | ||
| ) -> PyResult<Py<PyAny>> { | ||
| let config = HtmlTransformerConfig::new( | ||
| root_attributes, | ||
| all_attributes, | ||
| check_end_names.unwrap_or(false), | ||
| watch_on_attribute, | ||
| ); | ||
|
|
||
| match set_html_attributes_rust(html, &config) { | ||
| Ok((html, captured)) => { | ||
| // Convert captured attributes to a Python dictionary | ||
| let captured_dict = PyDict::new(py); | ||
| for (id, attrs) in captured { | ||
| captured_dict.set_item(id, attrs)?; | ||
| } | ||
|
|
||
| // Convert items to Bound<PyAny> for the tuple | ||
| use pyo3::types::PyString; | ||
| let html_obj = PyString::new(py, &html).as_any().clone(); | ||
| let dict_obj = captured_dict.as_any().clone(); | ||
| let result = PyTuple::new(py, vec![html_obj, dict_obj])?; | ||
| Ok(result.into_any().unbind()) | ||
| } | ||
| Err(e) => Err(PyValueError::new_err(e.to_string())), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| [package] | ||
| name = "djc-html-transformer" | ||
| description = "Apply attributes to HTML in a single pass" | ||
| version = "1.0.3" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| pyo3 = { workspace = true } | ||
| quick-xml = { workspace = true } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here is the Python definition of
set_html_attributes()function.To actually expose it to Python, this Python function is added to the module's scope on line 12: