-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from weblyzard/fix/bug-81-custom-html-handling2
Fix/bug 81 custom html handling2
- Loading branch information
Showing
40 changed files
with
652 additions
and
261 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Custom HTML tag handling example. | ||
Add a custom HTML handler for the bold <b> tag which encloses | ||
bold text with "**". | ||
Example: | ||
"Welcome to <b>Chur</b>" is rendered as "Welcome to **Chur**". | ||
""" | ||
from typing import Dict | ||
|
||
from inscriptis import ParserConfig | ||
from inscriptis.html_engine import Inscriptis | ||
from inscriptis.model.html_document_state import HtmlDocumentState | ||
from inscriptis.model.tag import CustomHtmlTagHandlerMapping | ||
from lxml.html import fromstring | ||
|
||
|
||
def my_handle_start_b(state: HtmlDocumentState, _: Dict) -> None: | ||
"""Handle the opening <b> tag.""" | ||
state.tags[-1].write("**") | ||
|
||
|
||
def my_handle_end_b(state: HtmlDocumentState) -> None: | ||
"""Handle the closing </b> tag.""" | ||
state.tags[-1].write("**") | ||
|
||
|
||
MY_MAPPING = CustomHtmlTagHandlerMapping( | ||
start_tag_mapping={"b": my_handle_start_b}, | ||
end_tag_mapping={"b": my_handle_end_b}, | ||
) | ||
|
||
|
||
HTML = "Welcome to <b>Chur</b>" | ||
|
||
html_tree = fromstring(HTML) | ||
inscriptis = Inscriptis( | ||
html_tree, ParserConfig(custom_html_tag_handler_mapping=MY_MAPPING) | ||
) | ||
print(inscriptis.get_text()) |
This file contains 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,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "inscriptis" | ||
version = "2.4.0.1" | ||
version = "2.5.0" | ||
authors = ["Albert Weichselbraun <[email protected]>", "Fabian Odoni <[email protected]>"] | ||
description = "inscriptis - HTML to text converter." | ||
keywords = ["HTML", "converter", "text"] | ||
|
@@ -59,5 +59,5 @@ line-length = 88 | |
target-version = ["py38", "py39", "py310", "py311", "py312"] | ||
extend-exclude = '\.html$|\.json$|\.txt$|/a$|/b$' | ||
include = ''' | ||
^/src/|^/tests/|^/benchmarking/ | ||
^/src/|^/tests/|^/benchmarking/|^/examples/ | ||
''' |
This file contains 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 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 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 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 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
Oops, something went wrong.