tmbb

tmbb

Doktor: extract docs from external files

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?

Popular in Announcing Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14410 106
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement