The code in these Mix projects was extracted from a much larger system, in order to have smaller and more cohesive versions for experimentation. In the original system, the code adds a table of contents (TOC) to the HTML output page from a Phoenix-based server.
Both versions do this task successfully, but the old version
causes an error for mix dialyzer
(see dial_out
for details).
The current version (see take_3
, below) passes Dialyzer.
This code uses several functions from the Floki library:
find/2
- search the input tree for atoc
elementparse_document/1
- generate a parse tree from the input HTMLparse_fragment/1
- generate a parse tree from the TOC HTMLraw_html/1
- regenerate HTML from the parse treetext/1
- extract text from header elementstraverse_and_update/2
- replace thetoc
element with the TOCtraverse_and_update/3
- add the TOC to the page
It also uses some of Floki's type definitions, in its @spec
entries:
Floki.html_tag
Floki.html_tree
Note:
Floki's traverse_and_update/3
function is very new
and its @spec
entry has a small error.
So, for the moment,
I'm using a modified copy of the library.
This Git repo contains two Mix projects.
Each of these passes mix test
, but fails mix dialyzer
.
(See dial_out_t1
and dial_out_t2
for details.)
The projects differ as discussed below.
The smalltoc.ex
file was copied
from the old router.toc.ex
in the larger system.
The Phoenix-related code was then removed
and a test file (smalltoc_test.exs
) was created.
This version of Smalltoc.add/1
:
- wraps each HTML header in a
<a name=...
element - replaces the
<toc />
element with a TOC list
The smalltoc.ex
file was edited
to remove the TOC generation and replacement code;
the test file was modified accordingly.
The smalltoc.ex
file was copied
from the new router.toc.ex
in the larger system.
The Phoenix-related code was then removed
and a test file (smalltoc_test.exs
) was created.
This version of Smalltoc.add/1
uses a combination of Floki calls
and regular expressions.
The resulting code is shorter, simpler, and passes Dialyzer.