Get front matter from string.
(text)
| 39 | |
| 40 | |
| 41 | def get_frontmatter(text): |
| 42 | """Get front matter from string.""" |
| 43 | |
| 44 | frontmatter = OrderedDict() |
| 45 | |
| 46 | if text.startswith("---"): |
| 47 | m = re.search(r'^(-{3}\r?\n(?!\r?\n)(.*?)(?<=\n)(?:-{3}|\.{3})\r?\n)', text, re.DOTALL) |
| 48 | if m: |
| 49 | yaml_okay = True |
| 50 | try: |
| 51 | frontmatter = yaml_load(m.group(2)) |
| 52 | if frontmatter is None: |
| 53 | frontmatter = OrderedDict() |
| 54 | # If we didn't get a dictionary, we don't want this as it isn't front matter. |
| 55 | assert isinstance(frontmatter, (dict, OrderedDict)), TypeError |
| 56 | except Exception: |
| 57 | # We had a parsing error. This is not the YAML we are looking for. |
| 58 | yaml_okay = False |
| 59 | frontmatter = OrderedDict() |
| 60 | |
| 61 | if yaml_okay: |
| 62 | text = text[m.end(1):] |
| 63 | |
| 64 | return frontmatter, text |
| 65 | |
| 66 | |
| 67 | def md_render(src="", language="", class_name=None, options=None, md="", **kwargs): |