<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="281104" data-post-id="281104">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="yolo007wizard" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  yolo007wizard
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is v1 prototype to get a json spec from module docs. I also learned about running <code>iex&gt; c("MathOps.ex", ".")</code> to compile the file.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MathOps do
  @moduledoc """
  This module provides math operations
  """
  @after_compile {__MODULE__, :_build_json_def}
  def _build_json_def(_env, bytecode) do
    {:docs_v1, _, _, _, module_doc, _, docs} = Code.fetch_docs(__MODULE__)
    {:ok, specs} = Code.Typespec.fetch_specs(bytecode)
    defs =
      for {{_, doc_name, _}, _, [sig], doc, _} &lt;- docs,
        {{spec_name, _}, [{:type, _, :fun, [{:type, _, :product, args}, {_, _, result, _}]}]} &lt;- specs,
        doc != :hidden,
        doc_name == spec_name do
          params =
            for arg &lt;- args,
            {:type, _, type, _} = arg do
              %{"type" =&gt; Atom.to_string(type)}
            end
          %{"name"=&gt; Atom.to_string(doc_name), "doc"=&gt; doc, "signature"=&gt; sig, "params"=&gt; params, "return"=&gt; result}
      end
    document = %{"module"=&gt; Atom.to_string(__MODULE__), "doc"=&gt; module_doc["en"], "defs"=&gt; defs}
    IO.inspect(document)
  end

  @doc """
  This function adds
  """
  @spec add(integer, integer) :: map()
  def add(a, b) do
    %{"result" =&gt; Integer.to_string(a + b)}
  end

end

</code></pre>
<p>Looks kinda bad in my opinion but it works <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"> . On compile it spits out clean looking map. Couple minor things yet and then json dump to file.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{
  "module" =&gt; "Elixir.MathOps",
  "doc" =&gt; "This module provides math operations\n",
  "defs" =&gt; [
    %{
      "doc" =&gt; %{"en" =&gt; "This function adds\n"},
      "name" =&gt; "add",
      "params" =&gt; [%{"type" =&gt; "integer"}, %{"type" =&gt; "integer"}],
      "return" =&gt; "map",
      "signature" =&gt; "add(a, b)"
    }
  ]
}
</code></pre>
<p>Thinking later on this could be a mix compile task but haven’t learned about it yet.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281104" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/turn-module-docs-and-specs-into-a-json-definition/54037/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-281104" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281104"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>