-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* DocX can now create Word documents with styles Introduced a new DocXConfiguration class that allows clients to control certain aspects of docx creation: o) The styles.xml file to be included in the docx o) Whether the declared font should always be output Added two new NSAttributedString keys so that clients can indicate that text should use Word paragraph and character styles: o) NSAttributedString.Key.paragraphStyleId o) NSAttributedString.Key.characterStyleId The value for these attributes should be a style id that is present in the Word file. * Allow clients to force indentation to 0 When translating NSParagraphStyle values to docx, a value of 0 is often used to indicate that no attributes should be output. This is true for paragraph spacing, line height, etc. And, for many of those, a value of 0 doesn’t really make sense. For indentation, though, setting values to zero can be valid. We don't want to break existing clients (which might rely on this behavior), so we've introduced a constant, `zeroIndent`, which can be used to force indentation to zero, when desired. * Handle baseline offset NSAttributedString.Key We assume that a baseline offset of less than zero indicates subscript, and a baseline offset of more than zero indicates superscript. * Revert "Handle baseline offset NSAttributedString.Key" This reverts commit c790d92. Pulling this out of issue17 so that I can file a separate PR for it. * Revert "Allow clients to force indentation to 0" This reverts commit f983b7a. Pulling this out of issue17 so that I can file a separate PR for it. * DocXConfiguration -> DocXStyleConfiguration In addition, DocXOptions has a new property, styleConfiguration, which holds an optional DocXStyleConfiguration. * Add new initializers for DocXStylesConfiguration The designated initializer now takes an AEXMLDocument. And there are now two new convenience initializers, one that takes a String? and one that takes a URL?. * additional tests for styles Co-authored-by: Brad Andalman <[email protected]> Co-authored-by: Morten Bertz <[email protected]>
- Loading branch information
1 parent
dec3ab6
commit 9999b2e
Showing
16 changed files
with
439 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// DocXStyleConfiguration.swift | ||
// | ||
// | ||
// Created by Brad Andalman on 2023/1/2. | ||
// | ||
|
||
import Foundation | ||
import AEXML | ||
|
||
/// Configuration parameters that control docx styling. | ||
public struct DocXStyleConfiguration { | ||
/// The styles XML document to include | ||
public let stylesXMLDocument: AEXMLDocument? | ||
|
||
/// Should the font family be specified explicitly? | ||
/// This can come in handy when a client prefers that a font specified | ||
/// by a Word style (including the default "Normal" style) should be used. | ||
public let outputFontFamily: Bool | ||
|
||
/// Designated initializer that takes an AEXMLDocument for the `styles.xml` file | ||
public init(stylesXMLDocument: AEXMLDocument?, outputFontFamily: Bool = true) { | ||
self.stylesXMLDocument = stylesXMLDocument | ||
self.outputFontFamily = outputFontFamily | ||
} | ||
|
||
/// Convenience initializer that takes a URL to the `styles.xml` file | ||
public init(stylesXMLURL: URL? = nil, outputFontFamily: Bool = true) throws { | ||
let xmlDocument: AEXMLDocument? | ||
if let xmlURL = stylesXMLURL { | ||
let xmlData = try Data(contentsOf: xmlURL) | ||
xmlDocument = try AEXMLDocument(xml: xmlData, | ||
options: DocXStyleConfiguration.xmlOptions) | ||
} else { | ||
xmlDocument = nil | ||
} | ||
|
||
self.init(stylesXMLDocument: xmlDocument, outputFontFamily: outputFontFamily) | ||
} | ||
|
||
/// Convenience initializer that takes a string for the `styles.xml` file | ||
public init(stylesXMLString: String? = nil, outputFontFamily: Bool = true) throws { | ||
let xmlDocument: AEXMLDocument? | ||
if let xmlString = stylesXMLString { | ||
xmlDocument = try AEXMLDocument(xml: xmlString, | ||
options: DocXStyleConfiguration.xmlOptions) | ||
} else { | ||
xmlDocument = nil | ||
} | ||
|
||
self.init(stylesXMLDocument: xmlDocument, outputFontFamily: outputFontFamily) | ||
} | ||
|
||
/// The paragraph styles that were found in the `styles.xml` file during initialization. Use with `NSAttributedString.Key.paragraphId`. | ||
public var availableParagraphStyles:[String]?{ | ||
return self.stylesXMLDocument?.root.children.filter({element in | ||
element.name == "w:style" && element.attributes["w:type"] == "paragraph" | ||
}).compactMap({$0.attributes["w:styleId"]}) | ||
} | ||
|
||
/// The character styles that were found in the `styles.xml` file during initialization. Use with `NSAttributedString.Key.characterId`. | ||
public var availableCharacterStyles:[String]?{ | ||
return self.stylesXMLDocument?.root.children.filter({element in | ||
element.name == "w:style" && element.attributes["w:type"] == "character" | ||
}).compactMap({$0.attributes["w:styleId"]}) | ||
} | ||
|
||
/// Returns the AEXML options used to create an AEXMLDocument | ||
private static var xmlOptions: AEXMLOptions = { | ||
var options = AEXMLOptions() | ||
options.parserSettings.shouldTrimWhitespace=false | ||
options.documentHeader.standalone="yes" | ||
|
||
// Enable escaping so that reserved characters, like < & >, don't | ||
// result in an invalid docx file | ||
// See: https://github.com/shinjukunian/DocX/issues/24 | ||
options.escape = true | ||
|
||
return options | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.