tmbb

tmbb

Sometimes we have more markdown than Elixir in our elixir files. I thought it would be fun to have a tool that could extract the docs from specially prepared markdown files. I’ve came up with Doktor.

First, you write your docs, say, in dok/Dummy.md:

<!-- @moduledoc Dummy -->
A dummy module for testing purposes.

<!-- @doc hey/1 -->
Says "hey" to a person.

<!-- @doc hey/2 -->
Says "hey" to a pair of persons.

Then, you use Doktor on your module:

defmodule Dummy do
  use Doktor, file: "doks/Dummy.md"

  @moduledok "Dummy"

  @dok "hey/1"
  def hey(name) do
    "Hey #{name}!"
  end

  @dok "hey/2"
  def hey(name1, name2) do
    "Hey #{name1} & #{name2}"
  end
end

You can now do this:

iex(26)> h Dummy
* Dummy

A dummy module for testing purposes.

iex(27)> h Dummy.hey
* def hey(name)

Says "hey" to a person.

* def hey(name1, name2)

Says "hey" to a pair of persons.

It works by overwriting the @ macro so that it treats the dok, typedok and moduledok calls in a different way. Fortunately this is the only “magic” I need. At compile time, the use Doktor, file: filename parses the sections from the filename and stores it in an agent, which is disposed of when the module is compiled. While the module is compiled, @dok and friends retrieve the function/module/etc. from the agent and add it to the AST of the module. It’s quite simple.

The beauty of that markdown file format is that it’s perfectly compatible with ordinary markdown, which means it highlights correctly and can be used to extract parts of markdown files. The format in the special comments is pretty self-explanatory. I don’t know if I’ll ever end up using this in practice, but it’s pretty cool.

This is restricted to one file per module for usability reasons, but it seems easy to generalize it so that it can retrieve docs from several files.

Link to github here: GitHub - tmbb/doktor: Document elixir modules using external files · GitHub

This library hasn’t been published on Hex, and won’t be unless there is some demand for it :slight_smile: (we should avoid namespace pollution there)

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ausimian
Castle is a package I put together to assist with building Elixir releases capable of being live-upgraded via the ‘official’ release hand...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
gushonorato
Hey everyone! :waving_hand: I’m excited to share a new library with you all: Magic Auth! It’s an authentication library for Phoenix proj...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews