Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NGenetzky opened this issue Oct 4, 2018 · 0 comments
Closed

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

NGenetzky opened this issue Oct 4, 2018 · 0 comments
Milestone

Comments

@NGenetzky
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants