It looks like you're new here. If you want to get involved, click one of these buttons!
postrender.quotes = function(passage, content, task) {
function processTextNodes(node) {
if (node.nodeType == 3) { // text node
node.nodeValue = node.nodeValue
.replace(/\b'/g, '’')
.replace(/'\b/g, '‘')
.replace(/"(?:\s|$)/g, '”')
.replace(/(?:\s|^)"/g, '“');
} else {
for (var i in node.childNodes) {
processTextNodes(node.childNodes[i]);
}
}
}
processTextNodes(passage);
}
I did some simple tests on a story in progress and it worked well there. The regular expressions might need some tweaking to work well everywhere. Please let me know if you encounter texts on which the quotes are replaced incorrectly.
Comments
1) How do you use this? Is postrender a built-in object whose methods are each called every time a passage is rendered?
2) Is this code compatible with SugarCube?
Thanks!
You can include this code in any passage tagged as "script".
I'd call "postrender" a collection of functions rather than methods, as there is no "this" object, but it does work as you describe.
Yes, if you have a recent version of SugarCube (-2835 or later). In fact, I only tested it with SugarCube, but it should work with the vanilla headers as well.