import yaml

from .text_item import AbstractTextItem
from .tree_item import AbstractTreeItem
from .. import register


@register
class YamlItem(AbstractTextItem, AbstractTreeItem):
    extensions = ("yaml", "yml")

    mime_type = "text/x-yaml"

    new_item_order_preference = 1.6

    type_description = lambda: "YAML"

    def get_yaml(self):
        return yaml.load(self.text, Loader=yaml.SafeLoader)

    def _repr_html_(self):
        p = self.get_yaml()
        return self._tree_to_html(p)