Skip to content

Commit

Permalink
#16 Fix te014
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoly-scherbakov committed Feb 3, 2024
1 parent 30e4ba4 commit 6e7a491
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions yaml_ld/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class LoadingDocumentFailed(YAMLLDError):
code: str = 'loading document failed'


@dataclass
class InvalidScriptElement(YAMLLDError):
"""HTML <script> element content is not valid YAML."""

code: str = 'invalid script element'


@dataclass
class NoYAMLWithinHTML(YAMLLDError):
"""No YAML-LD fragments found in an HTML document."""
Expand Down
11 changes: 9 additions & 2 deletions yaml_ld/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DocumentIsScalar,
LoadingDocumentFailed,
UndefinedAliasFound, MappingKeyError, InvalidEncoding, NoYAMLWithinHTML,
InvalidScriptElement,
)
from yaml_ld.loader import YAMLLDLoader
from yaml_ld.models import Document, DocumentType, ExtractAllScripts
Expand Down Expand Up @@ -55,15 +56,21 @@ def _parse_html(
return [parse(script) for script in html_yaml_scripts]

first_script, *_other_scripts = html_yaml_scripts
return parse(first_script)
try:
return parse(
first_script,
document_type=DocumentType.YAML,
)
except LoadingDocumentFailed as err:
raise InvalidScriptElement from err


def parse( # noqa: WPS238, WPS231, C901
raw_document: str | bytes | Path | URL,
extract_all_scripts: ExtractAllScripts = False,
document_type: DocumentType | None = None,
) -> Document:
"""Parse a YAML-LD document."""
document_type = None
fragment = None

if isinstance(raw_document, URL):
Expand Down

0 comments on commit 6e7a491

Please sign in to comment.