# Capture docstring using Macrotools

**URL:** <https://discourse.julialang.org/t/capture-docstring-using-macrotools/118670>\
**Category:** General Usage\
**Created:** [August 27, 2024, 2:57pm UTC](https://discourse.julialang.org/t/capture-docstring-using-macrotools/118670 "2024-08-27T14:57:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aramend](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@aramend](https://discourse.julialang.org/u/aramend)\
**Post date:** [August 27, 2024, 2:57pm UTC](https://discourse.julialang.org/t/capture-docstring-using-macrotools/118670/1 "2024-08-27T14:57:01Z")

</div>

Hoping this is trivial, but let’s see. The task is to extract documentation from a parsed struct definition using MacroTools. I also checked that `splitstructdef` fails in this case.

```julia
using MacroTools

sexpr = Meta.parse("""
    "You can't extract me na-na-na-na!"
    struct Foo
        x
    end
""")

function getdocs(ex::Expr)
    if @capture(ex, @doc str_ struct name_
        body__
    end)
        return str
    end
end

@show getdocs(sexpr) # getdocs(sexpr) = nothing

```

If I look at `dump(sexpr)` I can see that the structure changes depending on how I annotate the struct. The above leaves a `GlobalRef` to `Core.@doc`, which I don’t know how to pattern match. If I annotate using `@doc` as in the pattern, I can get the docstring out successfully.

Any ideas? My end goal is to parse a bunch of strings that may contain structs and docstrings, and group them by identical struct definitions (fields + name).
