12 Domain specific languages
Q: The escaping rules for
<style>tags are different: you don’t want to escape angle brackets or ampersands, but you do want to escape
</style>. Adapt the code above to follow these rules.
Q: The use of
...for all functions has some big downsides. There’s no input validation and there will be little information in the documentation or autocomplete about how they are used in the function. Create a new function that, when given a named list of tags and their
attribute names (like below), creates functions which address this problem.
list( a = c("href"), img = c("src", "width", "height") )
All tags should get
Q: Currently the HTML doesn’t look terribly pretty, and it’s hard to see the structure. How could you adapt
tag()to do indenting and formatting?
Q: Add escaping. The special symbols that should be escaped by adding a backslash in front of them are
%. Just as with HTML, you’ll need to make sure you don’t end up double-escaping. So you’ll need to create a small S3 class and then use that in function operators. That will also allow you to embed arbitrary LaTeX if needed.
Q: Complete the DSL to support all the functions that
Q: There’s a repeating pattern in
latex_env(): we take a character vector, do something to each piece, convert it to a list, and then convert the list to an environment. Write a function that automates this task, and then rewrite
Q: Study the source code for
dplyr. An important part of its structure is