|
3 | 3 | # Utilities.
|
4 | 4 | #
|
5 | 5 |
|
| 6 | +# |
| 7 | +# Expression Capture. |
| 8 | +# |
| 9 | + |
| 10 | +""" |
| 11 | + interpolation(object::T, captured::Expr) -> new_object |
| 12 | +
|
| 13 | +Interface method for hooking into interpolation within docstrings to change |
| 14 | +the behaviour of the interpolation. `object` is the interpolated object within a |
| 15 | +docstring and `captured` is the raw expression that is documented by the docstring |
| 16 | +in which the interpolated `object` has been included. |
| 17 | +
|
| 18 | +To define custom behaviour for your own `object` types implement a method of |
| 19 | +`interpolation(::T, captured)` for type `T` and return a `new_object` to |
| 20 | +be interpolated into the final docstring. Note that you must own the definition |
| 21 | +of type `T`. `new_object` does not need to be of type `T`. |
| 22 | +""" |
| 23 | +interpolation(@nospecialize(object), @nospecialize(_)) = object |
| 24 | + |
| 25 | +# During macro expansion process the interpolated string and replace all interpolation |
| 26 | +# syntax with calls to `interpolation` that pass through the documented expression along |
| 27 | +# with the resolved object that was interpolated. |
| 28 | +function _capture_expression(docstr::Expr, expr::Expr) |
| 29 | + if Meta.isexpr(docstr, :string) |
| 30 | + quoted = QuoteNode(expr) |
| 31 | + new_docstring = Expr(:string) |
| 32 | + append!(new_docstring.args, [_process_interpolation(each, quoted) for each in docstr.args]) |
| 33 | + return new_docstring |
| 34 | + end |
| 35 | + return docstr |
| 36 | +end |
| 37 | +_capture_expression(@nospecialize(other), ::Expr) = other |
| 38 | + |
| 39 | +_process_interpolation(str::AbstractString, ::QuoteNode) = str |
| 40 | +_process_interpolation(@nospecialize(expr), quoted::QuoteNode) = Expr(:call, interpolation, expr, quoted) |
| 41 | + |
6 | 42 | #
|
7 | 43 | # Method grouping.
|
8 | 44 | #
|
|
0 commit comments