import re
import yaml


def split_doc(content):
    """Split a document contents into the YAML metadata and the content
    itself.

    Args:
        contents (str): A string starting with a YAML block (delimited
        by '---' and '...')

    Returns:
        dict, str: A tuple consisting of the metadata (as a dictionary)
        and the body of the content.

    """

    # NOTE: python-frontmatter insists on ending metadata with ---
    match = re.match(r'(^---.*?\.\.\.)?(.*)', content, flags=re.DOTALL)

    if match:
        # check for "plain text" file, i.e. no header
        match = match.groups()

        if match[0] is None:
            return None, match[1]
        else:
            return yaml.safe_load(match[0]), match[1]
    else:
        return None, None