Skip to content

frontmatter.detect_format() use handler.detect() #48

@NGenetzky

Description

@NGenetzky

The following function doesn't properly ask the handlers if they can "detect" and instead utilizes the key of the dictionary which is assigned to handler.FM_BOUNDARY.

# global handlers
handlers = {
    Handler.FM_BOUNDARY: Handler() 
    for Handler in [YAMLHandler, JSONHandler, TOMLHandler]
    if Handler is not None
}


def detect_format(text, handlers):
    """
    Figure out which handler to use, based on metadata.
    Returns a handler instance or None.

    ``text`` should be unicode text about to be parsed.

    ``handlers`` is a dictionary where keys are opening delimiters 
    and values are handler instances.
    """
    for pattern, handler in handlers.items():
        if pattern.match(text):
            return handler

    # nothing matched, give nothing back
    return None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions