Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Forked from thom4parisot/convert-helpers.js
Created April 11, 2014 08:42
Show Gist options
  • Save nfroidure/10450649 to your computer and use it in GitHub Desktop.
Save nfroidure/10450649 to your computer and use it in GitHub Desktop.
'use strict';
var STYLE_ATTRIBUTE = 'TEXT-STYLE-NAME';
var styleMatches = function (item, styleName) {
if (!item || item.attributes === undefined || item.attributes[STYLE_ATTRIBUTE] === undefined) {
return false;
}
return item.attributes[STYLE_ATTRIBUTE].match(styleName);
};
module.exports = {
replaceStyle: function (oldStyleName, newStyleName){
return function(item){
if (styleMatches(item, oldStyleName)) {
item.attributes[STYLE_ATTRIBUTE] = newStyleName;
}
return item;
}
}
};
#!/usr/bin/env node
var sax = require('sax').createStream();
var fileStream = require('fs').createReadStream('data.xml');
var helpers = require('./convert-helpers.js');
var replaceStyle = helpers.replaceStyle;
sax.on('opentag', replaceStyle('Text_20_body', 'BodyText'));
sax.on('opentag', replaceStyle(/Heading_\d+_1/, 'HeadingLevel1'));
sax.on('opentag', replaceStyle(/P\d+/, 'ListItem'));
fileStream.pipe(sax).pipe(process.stdout);
<root>
<section>
<text:h text-style-name="Heading_20_1">Title 1</title>
<text:p text-style-name="Text_20_body">Some text</text:p>
<text:list text-style-name="L20">
<text:list-item>
<text:p text-style-name="P12">Bullet point 1</text:p>
</text:list-item>
<text:list-item>
<text:p text-style-name="P12">Bullet point 2</text:p>
</text:list-item>
</text:list>
</section>
</root>
{
"name": "node-streams",
"version": "0.0.0",
"description": "",
"main": "convert.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/10409366.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/10409366"
},
"homepage": "https://gist.github.com/10409366",
"dependencies": {
"sax": "^0.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment