Issues in the current implementation
The current implementation of ExDoc contains a series of issues, I would like to fix in the long term. The most prominent consequence of those issues is the difficulty in testing, either because the APIs are not well exposed or because the different modules are too coupled. The main issues are:
-
ExDoc as the main driver for logic. Initially we have thought about having ExDoc driving the main logic by simply calling functions on the formatter. This approach limits greatly what the compiler can achieve. My proposal is to change ExDoc to simply call:
formatter.run(data, config)and let the formatter do whatever it pleases. This will make it easy to unit testExDoc, because we can simply create a dummy formatter that returns the given data and config. Data is meant to be the information returned from the retriever and config is the current ExDoc config record. -
The ExDoc.Retriever module was created with ExDoc in mind. Its only API is
get_docswhich expects a bunch of files, which, once again, make them hard to test. Instead I would like the retriever to work against a list of modules names, which is possible because aliases in Elixir can exist without the underlying module existing, likeThisIsUnknown. Again, the direct benefit is that testability becomes, because we can simply pass a module for testing, instead of having to generate actual beam files. My proposed API for this module would be:ExDoc.Retriever.docs_from_dir(dir, config) ExDoc.Retriever.docs_from_files(files, config) ExDoc.Retriever.docs_from_modules(files, config)docs_from_dirshould simply do a wildcard on the given directory for ebin fles, which are then passed to files which then calls modules. I would also like to change theExDoc.Retrieverto return the full list of docs, without nesting or segregation by protocols or records. Nesting and segregation should be done by the underlying formatter on whatever way it pleases. We could provide helpers for such functionality either as extra functions inExDoc.Retriever. -
Finally, since those changes will likely move more logic into the formatter, I would like to split the current formatter in two modules. One that will do the required assembling and another one only with the templates. Today, both templates and assembling are in the same module, which cases confusion. For example, it would be wise to still keep the parts that write to disk decoupled from the template, so we can test the template without a need to read and write to disk.
As the main goal here is provide a well documented and tested project, it is important to document and update tests as we go, getting rid of the old, coupled tests in favor of new ones.
cc @pminten
I've pushed my current refactoring work to jwarwick/ex_doc@ed36946 in the refactor branch.
So far I've moved most of the ExDoc work into HTMLFormatter and cleaned up Retriever. Though I'm not thrilled with how nesting is achieved (the entire list gets rebuilt).
Still need to refactor templates out of HTMLFormatter and clean up the tests. Templates shouldn't be a problem, but figuring out how to do the tests more cleanly will be a challenge.
Feedback certainly welcome.
@jwarwick I have reviewed the code and it looks fantastic, thank you! I am excited to see how the tests will look like after we clean them up. :D
@jwarwick Looks good to me. From a glance the merge conflicts with my typespec code should be easily resolved. Tests are probably going to be biggest conflict, but I haven't written mine yet. I'll finish writing my code, sans tests, and push them to my typespec branch (so you can see what my changes are), then I'll try a merge/rebase locally to see what the actual problems are.
My code is relatively simple, the most "difficult" bit is adding links to the typespecs. But that code is unlikely to be impacted much by your changes.
Maybe it would be useful to replace some of the more simple templates with defs using programmatic construction of XML to reduce the number of template files needed. As a little test of usefulness I've implemented a simple XML/XHTML encoder (not ready for prime time yet): pminten/elixir@3d24084c600c2fbd575662760b7855a7d6a5a813
This allows writing the summary template as:
{:li, [],
[{:span, [class: "summary signature"],
[{:a, [href: "##{node.id}"], node.id}]
}]
}
For the larger templates this might be less readable though, and I'm not sure if it's really a good idea to mix small programmatic templates with larger eex templates. For some parts (encoding links in type declarations) it's definitely less ugly than manual HTML construction though (which is the main reason I wrote that code).
I've done a merge to see where the conflicts would be. The situation is not too bad, some stuff in html_formatter.ex that needs to be moved to html_formatter/templates.ex and various other small changes like adding types to the arguments to module_templates.
A tree with the result of the merge can be found in a throwaway branch: pminten/ex_doc@81e72ece3d0309adf4d3ed797b6887a000de4c07, (it's not a proper merge due to me messing up the git commands but that shouldn't matter).
Given how easy it is to resolve the conflicts I'm comfortable waiting with my commits until the refactoring work hits master and then rebasing my work on top of it.
@pminten The refactor branch is merged now, you can try applying your changes.
Great, thanks.