Module smartquotes

Module smartquotes 

Source
Expand description

Replaces " and ' quotes with “nicer” ones like , , , , or with for words like “isn’t”.

This currently only supports single character quotes, which is a limitation of the Rust implementation due to the use of const generics.

§Implementation notes

The main obstacle to implementing this was the fact that the document is necessarily represented as a tree of nodes. Each node is thus necessarily referenced by its parents, which means that an any given moment we cannot hold a mutable reference to a node if any other part of the code holds a reference to the document. At least that’s my understanding of the problem. The smartquotes algorithm from the JS library makes heavy use of iteration backwards and forwards through a flat list of tokens. This isn’t really possible in the Rust implementation. Building a flat representation of all Node objects is easy, but holding that list precludes us from executing a root.walk_mut call at the same time. On top of that, while the smartquotes algorithm iterates linearly over all nodes/tokens, looking at a specific token with index j can trigger replacements in any of the tokens with 0 to j - 1.

The solution proposed here is to first compute all the replacement operations on a read-only flat view of the document, and then to perform all replacements in a single call to root.walk_mut.

Structs§

SmartQuotesRule

Functions§

add
Add smartquotes with the “classic” quote set of , , , and .
add_with