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

Function parse_frontmatter

src/licensedcode/frontmatter.py:100–126  ·  view source on GitHub ↗

Parse text with frontmatter, return `content` and `metadata`. Pass in optional metadata defaults as keyword args. If frontmatter is not found, returns an empty metadata dictionary (or defaults) and original text content.

(text, encoding="utf-8", handler=SaneYAMLHandler(), **defaults)

Source from the content-addressed store, hash-verified

98
99
100def parse_frontmatter(text, encoding="utf-8", handler=SaneYAMLHandler(), **defaults):
101 """
102 Parse text with frontmatter, return `content` and `metadata`.
103 Pass in optional metadata defaults as keyword args.
104
105 If frontmatter is not found, returns an empty metadata dictionary
106 (or defaults) and original text content.
107 """
108 # ensure unicode first
109 text = return_unicode(text, encoding)
110
111 # metadata starts with defaults
112 metadata = defaults.copy()
113
114 # split on the delimiters
115 try:
116 fm, content = handler.split(text)
117 except ValueError:
118 # if we can't split, bail
119 return metadata, text
120
121 # parse, now that we have frontmatter
122 fm = handler.load(fm)
123 if isinstance(fm, dict):
124 metadata.update(fm)
125
126 return content, metadata
127
128
129def load_frontmatter(fd, encoding="utf-8", **defaults):

Callers 1

load_frontmatterFunction · 0.85

Calls 6

SaneYAMLHandlerClass · 0.85
return_unicodeFunction · 0.85
copyMethod · 0.45
splitMethod · 0.45
loadMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected