Include images in blog posts with inline diagrams code
#2Literate Haskell is not visible in .dia blocks
I am probably missing something but definitions written inside Bird tracks are not visible inside .dia blocks and it seems one has to repeat them.
Here's an example
We define the usual factorial function:
> fact 0 = 1 > fact n = n * fact (n-1)
NOTE: Square brackets below should be replaced by curly braces. Haddock chokes on curly braces.
Here is a green square:
dia = gSq
And another
dia = gSq
Green squares like gSq
and blue circles like circle 1 # fc blue
are extremely important.
[dia-def]
fact 0 = 1
fact n = n * fact (n-1)
gSq = square (1 / fact 1) # fc green # opacity 0.5 <>
square (1 / fact 2) # fc blue # opacity 0.5
Hmm, you're absolutely right. At the moment, BlogLiteratelyD makes no attempt to have the
.lhs
file itself in scope for diagram definitions.Part of what makes this tricky is that you might not always want the
.lhs
file contents in scope. For example, sometimes people just use.lhs
to format/syntax-highlight Haskell code without worrying about having it be a valid Haskell file. And sometimes there may be no Haskell code at all (which confuses the interpreter).Anyway, I will try to give it more thought soon and see if I can come up with something that works well.
Note that in the meantime you can simply import your module itself into any diagrams that want to reference stuff in scope. For example, you could have a file called
Foo.hs
which looks like this:> module Foo where Here is some text, and some code. > foo = 5 Here is a diagram: ```{.dia width='100'} import Foo dia = hcat (replicate foo (circle 1)) ```
Argh, the formatting is horrible. But hopefully you can tell what I mean.
I can and it works for some cases. But consider the case where I want to explain the factorial function in my blog post so I define it inside some bird tracks but I also want to use it to generate a diagram. I can put the factorial function inside another module that I can then import both to generate a value and to generate a diagram but I can't show the code the to the reader. Does that make sense?
No, that's the point, you don't have to put the factorial function inside another module. You make a file called
MyBlogPost.lhs
. Inside that file you havemodule MyBlogPost where
in some bird tracks and also the definition of factorial in some bird tracks, to show the code to the reader. Then later you have a diagram defined likeimport MyBlogPost dia = ... factorial ...
Does that make sense? So inside
MyBlogPost.lhs
you can have diagrams which importMyBlogPost
itself.- status set to closed
Apologies. It did make sense.