Emit a YAML scalar for str/None/bool.
(s)
| 96 | # --------------------------------------------------------------------------- |
| 97 | |
| 98 | def yaml_str(s): |
| 99 | """Emit a YAML scalar for str/None/bool.""" |
| 100 | if s is None: |
| 101 | return "null" |
| 102 | if isinstance(s, bool): |
| 103 | return "true" if s else "false" |
| 104 | s = str(s) |
| 105 | if s == "": |
| 106 | return '""' |
| 107 | # quote if contains any YAML special / leading-whitespace / starts-with-digit |
| 108 | if (re.search(r'[:#\n"\'{}\[\],&*?|<>=!%@`\\]', s) |
| 109 | or s != s.strip() |
| 110 | or s.lower() in ("null", "true", "false", "yes", "no", "on", "off", "~") |
| 111 | or re.match(r'^[\-+\d]', s)): |
| 112 | return json.dumps(s, ensure_ascii=False) |
| 113 | return s |
| 114 | |
| 115 | |
| 116 | def slugify(text: str, max_len: int = 60) -> str: |
no outgoing calls
no test coverage detected