"Hybrid" MarkDown for code

I would like the code snippets in the book I have written to be exported with full syntax highlighting.

I have a book already written that I am moving to Scrivener. The book is written in a plain text format.

The book has lots of source code examples (that are currently also in plaintext).

After researching Pandoc, MMD, and Chapter 21 of the Scrivener_Man-Mac, I am still a bit unsure how to move forward.

Ideal Situation (that I do not believe exists…maybe?):

I apply the “code block” style to my code snippets, In the compile settings, I am able to specify the desired code syntax, and magically, it gets rendered in C++ on output.

Other Option, that is nearly ideal (and I believe is possible):
I use the “Hybrid” option mentioned in the Scrivener Manual (section 21.5 Markdown and Scrivener).

I don’t apply any style to my code snippets, but surround them in:

//My un-styled code here

When I compile the document, Scrivener magically identifies the markdown I have used, and applies the proper code syntax highlight and structure format.

Am I anywhere on the right track? Thanks for any help!

With neither option will you get syntax highlighting out of the box. There may be a way of doing that with Pandoc or MultiMarkdown, but Scrivener doesn’t really have much to do with actual formatting of the files, it just combines the text together into a file, optionally produces a little Markdown syntax where it is programmed to, and then passes the result along to one of these engines. It is then their job to come up with such things as code blocks and so forth, let alone the formatting of the text that goes into the code block itself.

Now as to how the text is written in Scrivener, that is up to taste. If you prefer having the code fences in your writings then by all means type them in. If you want to use the style called “Code Block” then use that (and you’ll find with all of our default Markdown-based formats, such text will automatically be marked as code in the Markdown file. If you want to make your own style so that it marks the code block as “c”, then you can do that as well.

Thank you AmberV

I would go with the Code block style way to do this as it is more flexible and looks cleaner in the editor.

For Pandoc at least, you can change the syntax highlighting colours if you wish too, see here:

pandoc.org/MANUAL.html#syntax-highlighting

This is great NONTROPPO , thank you!

Anyway, what seems to be working is this:

I use code block style, and then just surround it in

[code]```c
//Code


I then export from scrivener using Pandoc -> ePub

Then I open the ePub in Kindle Preview 3, and export to .mobi format.

I think this will do the job for kindle, though for some reason it appears line breaks get inserted between each line of code in the kindle app. So I guess I'll have to play around with that some more.


I was hoping I wouldn't have to fence the code, but it looks like I'll have to if i want syntax highlighting.


Thanks for your help.  This forum is great.