I need to access doc comments to autogenerate API docs for a web framework. In macros, I can access CommentStmt that are in procs , but not in type section.
I dont know if i missed something. Does anyone know a way to extract doc comments from object declaration ?
import macros
type
Foo = object
## This is an object
afoo: string ## A foo field
proc fooProc*() =
## This is a procedure
discard
macro desc(i: typed): untyped =
result = newLit(i.getImpl.treeRepr)
echo desc Foo
echo "========================"
echo desc fooProc
Prints
TypeDef
Sym "Foo"
Empty
ObjectTy
Empty
Empty
RecList
IdentDefs
Ident "afoo"
Sym "string"
Empty
========================
ProcDef
Sym "fooProc"
Empty
Empty
FormalParams
Empty
Empty
Empty
StmtList
CommentStmt "This is a procedure"
DiscardStmt
Empty
If you want to extract documentation from sections, etc. you have to use the compiler's module ast. The documentation is stored in the comment field: https://nim-lang.org/docs/compiler/ast.html#comment%2CPNode
Generally, detecting the documentation in Nim code is a bit of fun. :) A small example: https://github.com/thindil/nimalyzer/blob/trunk/src/rules/hasdoc.nim#L209