forked from orsinium-labs/svg.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify tests, use reflection over MDN docs
- Loading branch information
Showing
13 changed files
with
130 additions
and
1,661 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from ._mdn_attr import MDNAttr | ||
from ._lib_element import LibElement | ||
|
||
__all__ = ['LibElement', 'MDNAttr'] |
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,32 @@ | ||
from dataclasses import dataclass, fields | ||
from typing import List, Set | ||
import svg | ||
from functools import cached_property | ||
|
||
|
||
@dataclass | ||
class LibElement: | ||
title: str | ||
fields: List[str] | ||
|
||
@classmethod | ||
def parse_all(cls) -> List['LibElement']: | ||
result = [] | ||
for el in svg.elements.Element.__subclasses__(): | ||
result.append(cls( | ||
title=el.element_name, | ||
fields=[f.name for f in fields(el)] | ||
)) | ||
return result | ||
|
||
@cached_property | ||
def attrs(self) -> Set[str]: | ||
result = set() | ||
for field in self.fields: | ||
if field == 'elements': | ||
continue | ||
field = field.rstrip('_') | ||
field = field.replace('__', ':') | ||
field = field.replace('_', '-') | ||
result.add(field) | ||
return result |
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,75 @@ | ||
from dataclasses import dataclass | ||
from typing import List, Optional, Set | ||
from pathlib import Path | ||
import re | ||
import yaml | ||
from functools import cached_property | ||
|
||
|
||
REX_EL = re.compile(re.escape('li>{{SVGElement("') + '([a-zA-Z-]+)' + re.escape('")}}</')) | ||
DEPRECATED = [ | ||
# attributes of <font-face> which is deprecated | ||
'underline-thickness', | ||
'underline-position', | ||
'overline-thickness', | ||
'overline-position', | ||
'strikethrough-thickness', | ||
'strikethrough-position', | ||
|
||
# not supported by any browser | ||
'crossorigin', | ||
'systemLanguage', | ||
] | ||
|
||
|
||
@dataclass | ||
class MDNAttr: | ||
title: str | ||
slug: str | ||
tags: List[str] | ||
content: str | ||
|
||
@classmethod | ||
def parse(cls, path: Path) -> Optional['MDNAttr']: | ||
path /= 'index.html' | ||
if not path.is_file(): | ||
return None | ||
raw = path.read_text() | ||
first, _, second = raw.partition('\n---\n') | ||
fields: dict = next(yaml.load_all(first, Loader=yaml.SafeLoader)) | ||
fields.pop('browser-compat', None) | ||
return cls(**fields, content=second) | ||
|
||
@classmethod | ||
def parse_all(cls, path: Path) -> List['MDNAttr']: | ||
result = [] | ||
for subpath in (path / 'attribute').iterdir(): | ||
attr = cls.parse(subpath) | ||
if attr is not None: | ||
result.append(attr) | ||
return result | ||
|
||
@cached_property | ||
def is_deprecated(self) -> bool: | ||
if self.title in DEPRECATED: | ||
return True | ||
if 'Deprecated' in self.tags: | ||
return True | ||
if '<div>{{SVGRef}}{{Deprecated_Header}}</div>' in self.content: | ||
return True | ||
if '<div>{{SVGRef}}{{deprecated_header}}</div>' in self.content: | ||
return True | ||
if '<div>{{deprecated_header}}</div>' in self.content: | ||
return True | ||
return False | ||
|
||
@cached_property | ||
def elements(self) -> Set[str]: | ||
sep = 'You can use this attribute with the following SVG elements:' | ||
_, _, txt = self.content.partition(sep) | ||
txt, _, _ = self.content.partition('</ul>') | ||
txt, _, _ = self.content.partition('See also') | ||
result = set() | ||
for match in REX_EL.finditer(txt): | ||
result.add(match.group(1)) | ||
return result |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.