MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / SaneYAMLHandler

Class SaneYAMLHandler

src/licensedcode/frontmatter.py:25–84  ·  view source on GitHub ↗

Load and export YAML metadata. . This is similar to the original frontmatter.default_handlers.YAMLHandler but is using nexB/saneyaml instead of pyyaml.

Source from the content-addressed store, hash-verified

23
24
25class SaneYAMLHandler:
26 """
27 Load and export YAML metadata. .
28
29 This is similar to the original frontmatter.default_handlers.YAMLHandler
30 but is using nexB/saneyaml instead of pyyaml.
31 """
32 FM_BOUNDARY = re.compile(r"^-{3,}\s*$", re.MULTILINE)
33 START_DELIMITER = END_DELIMITER = "---"
34
35 def __init__(self):
36 self.FM_BOUNDARY = self.FM_BOUNDARY
37 self.START_DELIMITER = self.START_DELIMITER
38 self.END_DELIMITER = self.END_DELIMITER
39
40 def detect(self, text):
41 """
42 Decide whether this handler can parse the given ``text``,
43 and return True or False.
44 """
45 if self.FM_BOUNDARY.match(text):
46 return True
47 return False
48
49 def split(self, text):
50 """
51 Split text into frontmatter and content.
52 """
53 _, fm, content = self.FM_BOUNDARY.split(text, 2)
54 return fm, content
55
56 def format(self, content, metadata, template=DEFAULT_POST_TEMPLATE, **kwargs):
57 """
58 Return string with `content` and `metadata` as YAML frontmatter,
59 used in ``frontmatter.dumps``.
60 """
61 start_delimiter = kwargs.pop("start_delimiter", self.START_DELIMITER)
62 end_delimiter = kwargs.pop("end_delimiter", self.END_DELIMITER)
63
64 metadata = self.export(metadata, **kwargs)
65
66 return template.format(
67 metadata=metadata,
68 content=content,
69 start_delimiter=start_delimiter,
70 end_delimiter=end_delimiter,
71 ).strip()
72
73 def load(self, fm, **kwargs):
74 """
75 Parse YAML front matter.
76 """
77 return saneyaml.load(fm, allow_duplicate_keys=False, **kwargs)
78
79 def export(self, metadata, **kwargs):
80 """
81 Export metadata as YAML.
82 """

Callers 2

parse_frontmatterFunction · 0.85
dumps_frontmatterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected