-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Design a system for embedding diagrams code within Haddock documentation #2
Comments
To clarify, the goal of this isn't just to get something that works for diagrams' own documentation, but a usable tool for anyone to enhance their Haddock documentation with diagrams. This fits into a larger strategy of coming up with ways to embed diagrams in various sorts of documents, which I think could be a "killer feature" for diagrams. See also this blog post (about embedding in blog posts and LaTeX documents). |
I started looking into this a bit today. Technically I think there is nothing too hard lurking here. We can use haskell-src-exts to parse code including comments, modify the comments, and exact-print the file back out. The hardest, part, I think, is in designing what it should actually do! The requirements are something like
It's not obvious (to me at least) what the best way is to satisfy all these requirements, or if it's even possible. Requiring idempotency hurts a lot, because of how restrictive Haddock is as a markup format. |
For 3 and 4: It seems that data urls “support” # parameters, i.e. they are ignored. So the URLs could be marked up as
and now this url is recognizable by the tool as where the final diagram should go to, and putting the diagram in there is idempotent, as the For 2: If the code will be found in non-haddock-comments as well, then this is easy. And for 1, the most simple solution might be that if there is a data url referencing If you don’t like putting those that should not show up in the haddock documentation in real comments: I believe you can have haddock comments that are not shown by naming them, e.g. {-$foo > ... -} but not referencing this named chunk from the exports list of the module. |
Ah, interesting idea! I hadn't really been thinking in terms of data URLs before, but that does nicely solve 3. The only downside (not insignificant!) is that source files will now contain large blocks of base64-encoded gunk which one has to wade through when editing the code. Ah, but it looks like I agree re: 2. I think the only difficulty with 1 is when one does want to include the code in the Haddock output; presumably we don't want the "markers" identifying and naming the diagrams code to also be output. But perhaps there's no way around that and it's not really that bad to have something like (say)
where |
But, why do you need markers? Instead of -- > -- [diagram: foo] -- > dia = square 1 just write -- > foo = square 1 The presence of |
Aha! Indeed, I think that will work quite nicely. |
This would be very cool! (btw, congrats on getting the release out!! Good stuff! Hoping to get time You might want to take a look at the source code of -Michael On Sun, Dec 16, 2012 at 6:11 PM, Brent Yorgey notifications@github.comwrote:
|
Also, I wonder if there is not something even more general to aim for: TH slices in haddock documentation? After all, I might not only want to generate a diagram but also show the textual output of some calculation in the documentation, or even some marked up calculation. So if haddock would allow TH splices (as much as I don’t like TH) in comments, maybe of type "Q String" or "Q HaddockSyntaxAST" the issue at hand becomes a simple library function to use, with no additional preprocessor needed. But then, better find a solution that works for diagrams first and then think about the silver bullet :-) |
A couple of relevant links:
import Distribution.Simple
import Distribution.Simple.Setup ( haddockDistPref, Flag(..))
import Distribution.Verbosity ( normal )
import Distribution.Simple.Utils ( copyFiles )
import Distribution.Text ( display )
import System.FilePath ((</>))
import System.Directory
-- Ugly hack, logic copied from Distribution.Simple.Haddock
haddockOutputDir flags pkg = destDir
where
baseDir = case haddockDistPref flags of
NoFlag -> "."
Flag x -> x
destDir = baseDir </> "doc" </> "html" </> display (packageName pkg)
main = defaultMainWithHooks simpleUserHooks {
postHaddock = \args flags pkg lbi -> do
copyFiles normal (haddockOutputDir flags pkg) [("doc","split.png")]
postHaddock simpleUserHooks args flags pkg lbi
} |
I've begun working on something putting all the above discussion together; it's in the repository here: http://github.com/diagrams/diagrams-haddock At this point it seems to really work -- it just needs a nicer API, more documentation, and so on. So I'm going to go ahead and close this ticket. |
The idea is to be able to write (specially marked) diagrams code within Haddock documentation, then run some sort of preprocessor over it (using diagrams-builder) which will compile the code and insert URLs referencing the images (Haddock supports
<<URL>>
syntax for embedded images). The images can be hosted onprojects.haskell.org
. The final result will be Haddock documentation with embedded images next to the code used to generate them -- these can serve as simple examples as well as illustrations of whatever is being explained in the documentation.The tricky part is that this preprocessor needs to be idempotent. It's no good to replace the code with image URLs, because we don't want to be forced to maintain two separate versions of everything (one with diagrams code and the other with image URLs).
One idea is to just have the preprocessor look for comment sections of the form
that is, if you want the preprocessor to render some diagrams code, you must prefix it with a
<<URL>>
link (presumably the link could be garbage or empty initially). The preprocessor will then update the URL if necessary --diagrams-builder
already has a facility for computing a hash of the code to see whether it needs to be rebuilt.The text was updated successfully, but these errors were encountered: