mirror of
https://github.com/mwilliamson/mammoth.js.git
synced 2024-12-08 15:14:29 +00:00
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
exports.readStylesXml = readStylesXml;
|
|
exports.Styles = Styles;
|
|
exports.defaultStyles = new Styles({}, {});
|
|
|
|
function Styles(paragraphStyles, characterStyles) {
|
|
return {
|
|
findParagraphStyleById: function(styleId) {
|
|
return paragraphStyles[styleId];
|
|
},
|
|
findCharacterStyleById: function(styleId) {
|
|
return characterStyles[styleId];
|
|
}
|
|
};
|
|
}
|
|
|
|
function readStylesXml(xml) {
|
|
var paragraphStyles = {};
|
|
var characterStyles = {};
|
|
|
|
var styles = {
|
|
"paragraph": paragraphStyles,
|
|
"character": characterStyles
|
|
};
|
|
|
|
xml.root.getElementsByTagName("w:style").forEach(function(styleElement) {
|
|
var style = readStyleElement(styleElement);
|
|
var styleSet = styles[style.type];
|
|
if (styleSet) {
|
|
styleSet[style.styleId] = style;
|
|
}
|
|
});
|
|
|
|
return new Styles(paragraphStyles, characterStyles);
|
|
}
|
|
|
|
function readStyleElement(styleElement) {
|
|
var type = styleElement.attributes["w:type"];
|
|
var styleId = styleElement.attributes["w:styleId"];
|
|
var name = styleName(styleElement);
|
|
return {type: type, styleId: styleId, name: name};
|
|
}
|
|
|
|
function styleName(styleElement) {
|
|
var nameElement = styleElement.first("w:name");
|
|
return nameElement ? nameElement.attributes["w:val"] : null;
|
|
}
|