-
-
Save nfroidure/10450649 to your computer and use it in GitHub Desktop.
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
'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; | |
} | |
} | |
}; |
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
#!/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); | |
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
<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> |
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
{ | |
"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