pantoniou / libfyaml

Fully feature complete YAML parser and emitter, supporting the latest YAML spec and passing the full YAML testsuite.
MIT License
241 stars 74 forks source link

sequence of documents output style #58

Closed cruisercoder closed 10 months ago

cruisercoder commented 1 year ago

I'm trying to get the emitter to output an array of documents in full block style. This is what the library outputs in block mode:

key: [
    a: 1,
    b: 2]

and this is what I'm trying to emit as output:

key:
  - a: 1
  - b: 2

Is this possible?

cruisercoder commented 1 year ago

it has something do internally with an empty save ctx in fy_emit_sequence_prolog.

The program below shows the issue. This is the output:

key: [
    a: 1]
struct fy_emitter*, enum fy_emitter_write_type, const char* str, int len, void*) {
  write(1, str, len);
  return len;
}

int main(int argc, char* argv[]) {
  struct fy_emitter_cfg cfg;
  struct fy_emitter* emit;
  cfg.flags = FYECF_MODE_BLOCK;
  cfg.output = handler;
  emit = fy_emitter_create(&cfg);
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_STREAM_START));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_DOCUMENT_START, true, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit,
                fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "key", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SEQUENCE_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_START, FYNS_BLOCK, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "a", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SCALAR, FYSS_PLAIN, "1", FY_NT, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_SEQUENCE_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_MAPPING_END));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_DOCUMENT_END, true, NULL, NULL));
  fy_emit_event(emit, fy_emit_event_create(emit, FYET_STREAM_END));
  fy_emitter_destroy(emit);
}
pantoniou commented 1 year ago

I am currently testing a fix - the problem is somewhat tricky because you're using the event interface directly and the style hints were not respected in all of the cases.

pantoniou commented 1 year ago

01953f31f7c219061d17beba59c631620a967ad3 should fix this.

You will need to use the manual emitting mode to make sure the hints are respected. If this fixes your issue, please close.

cruisercoder commented 1 year ago

This seems to fix the issue for me. Is there a way, however, to allow indentation of the dash? There seems to be agreement that this is easier to read. The output now, which is valid YAML, is this:

key:
- a: 1
- b: 2

This is the output I would like to see, which is also valid but more readable, is this:

key:
  - a: 1
  - b: 2
pantoniou commented 10 months ago

The original issue is fixed; Reopen this for styling options