question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiline strings in metadata

See original GitHub issue

I am attempting to add a multiline string to a document, but I’m getting syntax that breaks my pandoc processing. Is there a way to save the multi line strings using yaml block syntax?


Test case

import frontmatter

doc = frontmatter.loads(
    '\n'.join([
        '---',
        'title: multiline test',
        '---',
        '# multiline test',
    ])
)

doc['multiline'] = '\n'.join([
    'this is a',
    'multiline',
    'string',
])

print(frontmatter.dumps(doc))

Expected (desired) output

---
multiline: |
  this is a
  multiline
  string
title: multiline test
---

# multiline test

Actual output

---
multiline: 'this is a

  multiline

  string'
title: multiline test
---

# multiline test

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JonElliscommented, Mar 26, 2021

Yes, it is indeed the SafeDumper. Excellent suggestion. Makes sense as I’m now wrapping a string in an object. It seems that if I add the new representer to the SafeDumper itself, then I can continue to dump this document safely.

So this is the working version:

import frontmatter
import yaml

class literal_str(str): pass

def literal_str_representer(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')

yaml.SafeDumper.add_representer(literal_str, literal_str_representer)


doc = frontmatter.loads(
    '\n'.join([
        '---',
        'title: multiline test',
        '---',
        '# multiline test',
    ])
)

doc['multiline'] = literal_str('\n'.join([
    'this is a',
    'multiline',
    'string',
]))

print(frontmatter.dumps(doc))

Which outputs:

---
multiline: |-
  this is a
  multiline
  string
title: multiline test
---

# multiline test
1reaction
eyeseastcommented, Mar 26, 2021

Any **kwargs you pass when you run frontmatter.dumps will get passed through to yaml.dump, so you might try that. Here’s the relevant code: https://github.com/eyeseast/python-frontmatter/blob/master/frontmatter/default_handlers.py#L240-L249

It uses PyYaml’s SafeDumper class by default, which might be the issue. But if you can make it work with yaml.dump, you should be able to get it working with frontmatter.dumps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define multi-line @description metadata?
You don't. The general idea was that the description was supposed to be concise "Microcontent" and the size can't be too large without ......
Read more >
Metadata Properties
If you need a string that spans several lines, use a multiline string literal - a sequence of characters surrounded by double (...
Read more >
Metadata format: metadata is not a plain mapping of strings
Almost all build backends assume metadata to be a plain mapping of strings (distutils, setuptools, flit, poetry, etc.)
Read more >
Ease creating multiline documentation or metadata values.
Creating multiline documentation is currently somewhat annoying as you need to include \n in the test data yourself: *** Settings *** Documentation First...
Read more >
appendix B. TOML and JSON for metadata - Hugo in Action
key1= """ This is a multiline string where newline characters are valid. Multi line strings end by three quote(") symbols """ key12= '''...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found