|
1 | | -export @esc, isexpr, isline, iscall, rmlines, unblock, block, inexpr, namify, isdef, |
| 1 | +export @esc, isexpr, isline, iscall, rmlines, rmdocs, unblock, block, inexpr, namify, isdef, |
2 | 2 | longdef, shortdef, @expand, makeif, prettify, combinedef, splitdef, splitarg, combinearg |
3 | 3 |
|
4 | 4 | macro public(ex) |
|
122 | 122 |
|
123 | 123 | striplines(ex) = prewalk(rmlines, ex) |
124 | 124 |
|
| 125 | +""" |
| 126 | + rmdocs(x) |
| 127 | +
|
| 128 | +Remove the documentation macros from the expression. |
| 129 | +
|
| 130 | +### Examples |
| 131 | +
|
| 132 | +To work with nested blocks: |
| 133 | +
|
| 134 | +```julia |
| 135 | +prewalk(rmdocs, ex) |
| 136 | +``` |
| 137 | +
|
| 138 | +See also: [`rmlines`](@ref) |
| 139 | +""" |
| 140 | +rmdocs(x) = x |
| 141 | +function rmdocs(ex::Expr) |
| 142 | + if ex.head == :macrocall |
| 143 | + m = ex.args[1] |
| 144 | + if isa(m, GlobalRef) && m.mod == Core && m.name == Symbol("@doc") |
| 145 | + for i ∈ 2:length(ex.args) |
| 146 | + arg = ex.args[i] |
| 147 | + if !isline(arg) && !isnothing(arg) |
| 148 | + doc = arg |
| 149 | + if i < length(ex.args) |
| 150 | + return ex.args[i + 1] |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + return nothing |
| 155 | + end |
| 156 | + end |
| 157 | + return ex |
| 158 | +end |
| 159 | + |
| 160 | +stripdocs(ex) = prewalk(rmdocs, ex) |
| 161 | + |
125 | 162 | """ |
126 | 163 | unblock(expr) |
127 | 164 |
|
|
592 | 629 | prettify(ex) |
593 | 630 |
|
594 | 631 | Makes generated code generaly nicer to look at. |
| 632 | +
|
| 633 | +# Keywords |
| 634 | +- `lines::Bool=false`: whether to preserve line number nodes |
| 635 | +- `alias::Bool=true`: whether to replace gensyms with animal names |
| 636 | +- `docs::Bool=false`: whether to preserve docstrings |
595 | 637 | """ |
596 | | -prettify(ex; lines = false, alias = true) = |
597 | | - ex |> (lines ? identity : striplines) |> flatten |> unresolve |> resyntax |> (alias ? alias_gensyms : identity) |
| 638 | +prettify(ex; lines=false, alias=true, docs=false) = |
| 639 | + ex |> (lines ? identity : striplines) |> (docs ? identity : stripdocs) |>flatten |> |
| 640 | + unresolve |> resyntax |> (alias ? alias_gensyms : identity) |
0 commit comments