Сохранение относительного порядка ключей слияния и явных ключей с помощью ruamel

Это потому, что вы забыли пройти в event в функцию click:

$('.menuOption').on('click', function (e) { // <-- the "e" for event

    e.preventDefault(); // now it'll work

    var categories = $(this).attr('rel');
    $('.pages').hide();
    $(categories).fadeIn();
});

На боковой ноте e чаще используется в отличие от слова event ], поскольку Event является глобальной переменной в большинстве браузеров.

1
задан Anthon 15 January 2019 в 20:42
поделиться

1 ответ

С помощью настройки двух строк в представителе для отображений это можно исправить, информация о местоположении для ключа слияния была там, она просто не использовалась. К сожалению, это довольно большая функция, которая требует нескольких импортов:

import sys
import ruamel.yaml

if ruamel.yaml.version_info < (0, 15, 86):
    from ruamel.yaml.nodes import MappingNode, ScalarNode
    from ruamel.yaml.comments import comment_attrib, merge_attrib

    def represent_mapping(self, tag, mapping, flow_style=None):
        value = []
        try:
            flow_style = mapping.fa.flow_style(flow_style)
        except AttributeError:
            flow_style = flow_style
        try:
            anchor = mapping.yaml_anchor()
        except AttributeError:
            anchor = None
        node = MappingNode(tag, value, flow_style=flow_style, anchor=anchor)
        if self.alias_key is not None:
            self.represented_objects[self.alias_key] = node
        best_style = True
        # no sorting! !!
        try:
            comment = getattr(mapping, comment_attrib)
            node.comment = comment.comment
            if node.comment and node.comment[1]:
                for ct in node.comment[1]:
                    ct.reset()
            item_comments = comment.items
            for v in item_comments.values():
                if v and v[1]:
                    for ct in v[1]:
                        ct.reset()
            try:
                node.comment.append(comment.end)
            except AttributeError:
                pass
        except AttributeError:
            item_comments = {}
        merge_list = [m[1] for m in getattr(mapping, merge_attrib, [])]
        merge_pos = getattr(mapping, merge_attrib, [[0]])[0][0]          # <<<<<<<< line added
        item_count = 0
        if bool(merge_list):
            items = mapping.non_merged_items()
        else:
            items = mapping.items()
        for item_key, item_value in items:
            item_count += 1
            node_key = self.represent_key(item_key)
            node_value = self.represent_data(item_value)
            item_comment = item_comments.get(item_key)
            if item_comment:
                assert getattr(node_key, 'comment', None) is None
                node_key.comment = item_comment[:2]
                nvc = getattr(node_value, 'comment', None)
                if nvc is not None:  # end comment already there
                    nvc[0] = item_comment[2]
                    nvc[1] = item_comment[3]
                else:
                    node_value.comment = item_comment[2:]
            if not (isinstance(node_key, ScalarNode) and not node_key.style):
                best_style = False
            if not (isinstance(node_value, ScalarNode) and not node_value.style):
                best_style = False
            value.append((node_key, node_value))
        if flow_style is None:
            if ((item_count != 0) or bool(merge_list)) and self.default_flow_style is not None:
                node.flow_style = self.default_flow_style
            else:
                node.flow_style = best_style
        if bool(merge_list):
            # because of the call to represent_data here, the anchors
            # are marked as being used and thereby created
            if len(merge_list) == 1:
                arg = self.represent_data(merge_list[0])
            else:
                arg = self.represent_data(merge_list)
                arg.flow_style = True
            value.insert(merge_pos, (ScalarNode(u'tag:yaml.org,2002:merge', '<<'), arg))   # <<<<< line changed
        return node

    ruamel.yaml.representer.RoundTripRepresenter.represent_mapping = represent_mapping



yaml_str = """\
foo: &foo
  color: red

bar:
  name: qux
  <<: *foo
"""

yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)

, что дает:

foo: &foo
  color: red

bar:
  name: qux
  <<: *foo

Вышеуказанное пытается сохранить абсолютную позицию, не занимая удаление или вставку ключа -значение пар.

Вышеуказанное ничего не исправит при использовании следующей версии ruamel.yaml, которая будет включать эти изменения.

0
ответ дан Anthon 15 January 2019 в 20:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: