#1066 accepted
Tom Scogland

Syntax Highlighting for code regions

Reported by Tom Scogland | March 3rd, 2015 @ 05:17 PM

I know this has come up before, but I've recently run into some options that might make this more possible/palatable. Given that mailmate uses upskirt/sundown for markdown processing, and it already embeds the language identifier, what would you think of a patch to integrate the GNU Syntax Highlight library (https://www.gnu.org/software/src-highlite/) for code highlighting in the HTML generator?

I'm currently swapping out the patched sundown for a ruby-based daemon that uses the github markdown pipeline to get a similar effect. Of course, that's vastly more fragile, and not really a long-term solution. I might be sufficiently motivated to actually put in a pull request to get highlighting in the patched sundown if there's a chance of it getting taken up.

Comments and changes to this ticket

  • benny

    benny March 3rd, 2015 @ 08:29 PM

    • State changed from “new” to “accepted”

    I've actually already played with syntax highlighting some time ago, but I didn't finish it. It's non-trivial to make it work well, but I think I was on the right track. If you want to try it out then this is how to enable it:

    sudo easy_install pygments
    defaults write com.freron.MailMate MmCodeStylingEnabled -bool YES

    It uses the pygmentize command line tool with inlining of the styling. It is currently hardcoded to use style=colorful.

    Note: I'm pretty sure it's buggy and I don't have time to work on it right now. You are welcome to report issues (or tell me it doesn't work at all), but I cannot promise to fix it soon :-)

  • Tom Scogland

    Tom Scogland March 3rd, 2015 @ 08:50 PM

    I happen to have pygments installed, for minted as one does, and with that defaults command written it works quite nicely. As far as I can tell from a quick test anyway.

    Somehow I was under the impression you would prefer a fully native option integrated into the markdown system rather than another command line tool, hence the thought on GNU Highlight which recently added on a C++ library interface. Doing it with pygments is certainly good with me though. In fact, pulling in a python-based markdown pipeline would be pretty nice for extensibility (probably dreaming here, but still...).

    I'm guessing when you say hardcoded, you mean hardcoded somewhere we outsiders can't really modify. Any chance of a hidden option to switch the style out, or maybe a pointer to how pygments/pygmentize is being invoked so I can interpose something?

  • Tom Scogland

    Tom Scogland March 3rd, 2015 @ 09:34 PM

    Overall this is working great, but there does appear to be a bug in the formatting where the $ character is used. Since lighthouse eats the syntax for breakfast, and doesn't seem to have working verbatim support for backticks, the block bounds are removed below, but imagine the triple-backtick syntax before the language name.

    In a bash block like this, the $ and the name after it simply disappear:

    echo $meh

    Adding a backslash lets it through, but highlights it oddly. Making it a language-free block restores the verbatim semantics to it. Switching it to a non-bash language makes life yet more interesting.

    echo $meh
    echo "$meh"

    Putting it in a C language context, as obviously incorrect as the syntax is for C, prints this:

    echo $meh
    echo ""

    So... somewhere there is an odd escaping problem between shell variable expansions and quotes.

  • benny

    benny March 4th, 2015 @ 10:35 AM

    Thanks, that was a stupid bug.

    I fixed it and it should work in the next update.

    Now, I wonder what Lighthouse does with a code segment sent by email:

    echo $meh
    And yes, the style is currently really hardcoded. I&#39;m not sure how it is 
    best made configurable. I guess it&#39;s yet another hidden preference. I&#39;ll 
    think about it ;-)</code>
  • benny

    benny March 4th, 2015 @ 10:50 AM

    Hmm, didn't go to well with Lighthouse, but I cannot see that MailMate is to blame.

  • Tom Scogland

    Tom Scogland March 4th, 2015 @ 04:39 PM

    Thanks Benny!

    I apologize if I gave the impression that MailMate was to blame for the Lighthouse issue I mentioned, it isn't. The issue I was running into was that even using the triple-at-sign syntax for verbatim blocks doesn't escape backticks in lighthouse's markup for some reason. As a result, I couldn't put in the exact syntax that was causing issues without spurious and rather odd piles of partially escaped HTML tags, rather like you ended up with. This was actually putting it in through the web form, where a properly escaped version of the bash block I had earlier comes out like this:

    echo $meh
    Thanks again for the quick replies, and fixing up the quoting bug for that matter.
  • benny

    benny March 4th, 2015 @ 07:50 PM

    Ah, that looks a bit like what happened to my email reply. Maybe it's related.

  • benny

    benny March 4th, 2015 @ 07:52 PM

    Testing again:

    echo $meh

    Maybe it works better with ~~~.

  • benny

    benny March 4th, 2015 @ 07:56 PM

    Ok, seems we should use ~~~ and the code styling done by MailMate is definitely lost.

  • Dmitry

    Dmitry May 9th, 2015 @ 09:40 AM

    I've executed

    sudo easy_install pygments
    defaults write com.freron.MailMate MmCodeStylingEnabled -bool YES
    Quit Mailmate, opened it again.
    Tried to paste some Scala code into ``` block, it did not work. Neither did it work with java.
    Are some additional actions required to make it work?


  • benny

    benny May 9th, 2015 @ 01:36 PM

    @dmitry: Try providing the programming language explicitly:

  • Felix S

    Felix S September 30th, 2015 @ 03:45 PM

    Since rev 5141 this secret feature seems broken for me ...

  • benny

    benny October 1st, 2015 @ 12:38 PM

    • State changed from “accepted” to “fixcommitted”

    The (still experimental) code styling feature is now handled in a different way. There is a bit about it here.

    I've also changed some things with respect to how it works such that installing pygments is no longer necessary. It's also worth noting that alternative styles can be installed. For example, this should enable github-style styling:

    sudo easy_install pygments-style-github
    defaults write com.freron.MailMate MmDefaultPygmentsStyleName github

    Hopefully I'll soon add GUI settings for enabling code styling since it's almost a complete feature.

  • benny

    benny November 26th, 2015 @ 02:33 PM

    • State changed from “fixcommitted” to “fixreleased”
  • Tom Scogland

    Tom Scogland December 1st, 2015 @ 11:40 PM

    The new bundle for this does not appear to work. I have it installed, and the filter programs it installs work if invoked on the command line with appropriate environment variables, but mailmate never seems to invoke them, or install a command to let me invoke them. Is there a trick to making it work, or debug it?

  • benny

    benny December 2nd, 2015 @ 09:01 AM

    @Tom: Sorry, this functionality is broken until I've finished implementing various features related to handling HTML (including syntax highlighting). I'll have some kind of official beta release when that is complete.

  • Tom Scogland

    Tom Scogland April 27th, 2016 @ 12:29 AM

    @benny, any news here?

  • benny

    benny April 27th, 2016 @ 09:31 AM

    @Tom: If you write me an email then I can point you to a private beta. I'm not quite ready to release it as a public beta/test version. Use “Help ▸ Send Feedback”.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Mac OS X email client.

Shared Ticket Bins