jgm / cheapskate

Experimental markdown processor in Haskell
BSD 3-Clause "New" or "Revised" License
107 stars 18 forks source link

Rudimentary Math Support #3

Open 2piix opened 10 years ago

2piix commented 10 years ago

This is purely a feature request/request for some quick help. I'm trying to add rudimentary support for MathJax, and I've done most of the work, but I'm having a little bit of trouble with my debugging.

The support, as I envision it, simply passes the contents of $foo$ and

$$
foo
$$

through, without change. This way, the onus to import the mathjax libraries (or any other library) is on the user -- presumably by embedding the Cheapskate's Html in their product. However, I am having some problems getting the $$ parsers to work. I defined a new Container called 'DisplayMath'. I am unable to "close" the DisplayMath container. The closest I have gotten is with this case in continueContainer:

DisplayMath -> nfb $ string "\n$$"

This results in:

  DisplayMath
    BlankLine ""
    "\\sin \\pi = 0"
  DisplayMath
    BlankLine ""
    BlankLine ""
    "The quick brown $\\pi$ jumps over the lazy dog."
    BlankLine "",fromList [])

on the input

$$
\sin \pi = 0
$$

The quick brown $\pi$ jumps over the lazy dog.

Which makes it look like the second $$ is closing the environment but is not getting consumed, so it is triggering a new DisplayMath container. What is the proper place to consume the $$'s?

Sorry, I hope this request for help isn't too tangential to your goals. I really would like to make some nice, high quality code for your library.

jgm commented 10 years ago

Hint: your construct is like a fenced code block (opening line delimiter, contents, closing line delimiter). So you should be able to mimic the treatment of FencedCode.

2piix commented 10 years ago

Thanks for the hint. It's working great now.

I'm not sure if you're interested in my code, but if you are, I've committed a working version. One thing I did that might be off-putting is treat the math displays with an alternative version of parseInlines -- I'm calling it processMathLines (obviously, I'd be happy to do some clean up to fit your tree if you're interested). The point is that I do want the math environments to be richer than just plain Text, but I don't want the full power of Markdown in them. I plan on adding "Hide" (for "spoiler" tags) Inlines and Blocks next. Unless you're interested in that stuff too, I will be maintaining my own fork, either way.

https://github.com/2piix/cheapskate