PureXML/Sources/Emitting/Options.swift
Mihaela Mihaljevic cd2f30a262 feat(emitting): attribute quote style, line endings, xml:space, writer namespaces
Broaden the serializer and writer options from the audit:

- attributeQuote selects single or double quotes; the active quote is
  escaped in values (apostrophe as ' in single-quote mode).
- lineEnding controls the inserted newline (set to CRLF for \r\n output).
- xml:space=preserve suppresses pretty-print indentation for an element
  and its subtree; xml:space=default re-enables it.
- Writer gains writeStartElementNS(prefix:localName:namespaceURI:) and
  writeNamespace(prefix:uri:), so callers no longer hand-format prefixes
  and xmlns attributes.

CDATA-vs-escape and entity re-escaping strategies are left as a
follow-up; the current escaping is the safe default.

Closes #91
2026-06-08 15:24:49 +02:00

79 lines
3.2 KiB
Swift

public extension PureXML.Emitting {
/// Explicit emitter options. The default is pretty-printed output with
/// two-space indentation and a self-closing tag for empty elements, and no
/// XML declaration.
/// Which quote character delimits attribute values in the output.
enum QuoteStyle: Equatable, Hashable, Sendable {
case double
case single
var character: Character {
self == .double ? "\"" : "'"
}
}
struct Options: Equatable, Hashable, Sendable {
/// Whether to indent nested elements onto their own lines.
public var prettyPrint: Bool
/// The indentation unit used when ``prettyPrint`` is enabled.
public var indent: String
/// The quote character that delimits attribute values.
public var attributeQuote: QuoteStyle
/// The line terminator inserted by pretty-printing and after the
/// declaration. A line feed by default; set to `\r\n` for CRLF output.
public var lineEnding: String
/// Whether childless elements collapse to `<name/>`.
public var selfCloseEmptyElements: Bool
/// Whether to emit an `<?xml ...?>` declaration (libxml2 emits one by
/// default when saving a document; PureXML defaults to off for fragments).
public var includeXMLDeclaration: Bool
/// The version written in the declaration.
public var xmlVersion: String
/// The encoding name written in the declaration, or nil to omit it.
public var encodingName: String?
/// The `standalone` flag written in the declaration, or nil to omit it.
public var standalone: Bool?
public init(
prettyPrint: Bool = true,
indent: String = " ",
attributeQuote: QuoteStyle = .double,
lineEnding: String = "\n",
selfCloseEmptyElements: Bool = true,
includeXMLDeclaration: Bool = false,
xmlVersion: String = "1.0",
encodingName: String? = "UTF-8",
standalone: Bool? = nil,
) {
self.prettyPrint = prettyPrint
self.indent = indent
self.attributeQuote = attributeQuote
self.lineEnding = lineEnding
self.selfCloseEmptyElements = selfCloseEmptyElements
self.includeXMLDeclaration = includeXMLDeclaration
self.xmlVersion = xmlVersion
self.encodingName = encodingName
self.standalone = standalone
}
/// Pretty-printed output with two-space indentation.
public static let `default` = Options()
/// Single-line output with no inserted whitespace.
public static let compact = Options(prettyPrint: false)
/// The `<?xml ...?>` declaration string, or nil when not requested.
var xmlDeclaration: String? {
guard includeXMLDeclaration else { return nil }
var declaration = "<?xml version=\"\(xmlVersion)\""
if let encodingName {
declaration += " encoding=\"\(encodingName)\""
}
if let standalone {
declaration += " standalone=\"\(standalone ? "yes" : "no")\""
}
return declaration + "?>"
}
}
}