Created
June 16, 2019 05:20
-
-
Save chadaustin/dd8717c19075263e03f689cbc15326c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import yaml | |
# Try to preserve order in the front matter. | |
def dict_representer(dumper, data): | |
return dumper.represent_dict(data.items()) | |
def dict_constructor(loader, node): | |
return collections.OrderedDict(loader.construct_pairs(node)) | |
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG | |
yaml.Dumper.add_representer(collections.OrderedDict, dict_representer) | |
yaml.Loader.add_constructor(_mapping_tag, dict_constructor) | |
yaml.FullLoader.add_constructor(_mapping_tag, dict_constructor) | |
# Represent null as blank. | |
def none_representer(dumper, _): | |
return dumper.represent_scalar('tag:yaml.org,2002:null', '') | |
yaml.Dumper.add_representer(type(None), none_representer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment