Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Post-render function for curly quotes

I think curly quotes are prettier, but typing straight qoutes is a lot easier. Therefore I wrote the following post-render function that converts straight quotes in passage contents into curly quotes:

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

  • Cool, I was just wondering about the ease of incorporating something like this. A couple of questions:

    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!
  • Erik wrote:

    1) How do you use this? Is postrender a built-in object whose methods are each called every time a passage is rendered?


    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.

    Erik wrote:

    2) Is this code compatible with SugarCube?


    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.
  • Great to know! I'll give it a try in the near future and let you know how it goes.
  • I fixed a bug in the earlier code: when there is a double quoted string surrounded by white space, the white space was discarded. This problem is fixed in the new version:

    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, '”$1')
    .replace(/(\s|^)"/g, '$1“');
    } else {
    for (var i in node.childNodes) {
    processTextNodes(node.childNodes[i]);
    }
    }
    }
    processTextNodes(passage);
    }
  • In recent SugarCube releases the replacement with HTML entities no longer works, so I made a new version which uses JavaScript Unicode escape sequences instead:

    postrender.quotes = function(passage, content, task) {
    function processTextNodes(node) {
    if (node.nodeType == 3) { // text node
    node.nodeValue = node.nodeValue
    .replace(/\b'/g, '\u2019')
    .replace(/'\b/g, '\u2018')
    .replace(/"(\s|$)/g, '\u201D$1')
    .replace(/(\s|^)"/g, '$1\u201C');
    } else {
    for (var i in node.childNodes) {
    processTextNodes(node.childNodes[i]);
    }
    }
    }
    processTextNodes(passage);
    }
Sign In or Register to comment.