vmg / sundown

Standards compliant, fast, secure markdown processing library in C
2k stars 385 forks source link

Add support for shebang style code blocks. #142

Open jnicklas opened 11 years ago

jnicklas commented 11 years ago

If the MKDEXT_SHEBANG_CODE flag is enabled, a block like this:

#!ruby
puts "hello world"

Is stripped of the shebang line, and the language is treated the same as with a fenced code block.

This was born out of a discussion on Twitter, where I was whining about the fact that syntax highlighting on @github requires the use of fenced code blocks. I hate fenced code blocks, so I wanted an equivalent syntax for indented code blocks. After a brief discussion with @ryanb, @searls, @tpope and a few others, we all felt that this syntax was the nicest.

Does this project have any tests? I couldn't find any. If so, I'd be happy to add tests for this functionality. I used this file as test input. Attached at the link is also output with the flag enabled and disabled.

I tried to be as conservative as possible in what is allowed as a shebang line. I'm total C newb though, so I have no idea if I've missed anything important.

gampleman commented 11 years ago

It would be nice if this allowed real hashbangs:

    #!/usr/bin/env ruby 
    puts ARGF[1]
jnicklas commented 11 years ago

@gampleman I considered that, but then it gets pretty messy, imagine if ruby had taken an argument:

#!/usr/bin/env ruby -w

Suddenly this is pretty hard to figure out.

tpope commented 11 years ago

@jnicklas, for the record, that doesn't even work. Shebang lines are limited to a single argument.

gampleman commented 11 years ago

I think that a simple heuristic for figuring this out is this:

  1. If 1 word, then split on / and take last token.
  2. If 2 words:
    • If second word starts with -, use first word and do 1.
    • Otherwise use second word.
  3. If more words, fail.

Where words are tokens separated by whitespace.

gampleman commented 11 years ago

Also to address more the whole issue rather than this specific point, I would be in favor of this proposal if the hashbang line was rendered as well; that is, it would simply trigger the syntax highlighter but still it would be also passed to the final document.

Why? While I'm not a huge fan of code fences myself, I'm even less for yet another fracturing in the way Markdown is rendered and this proposal AFAIK isn't implemented in any other implementation. While adding tweaks like syntax highlighting enhances the output, having hashbangs render in some implementations and not render in others seems like a major pain.

jnicklas commented 11 years ago

@gampleman: that would obviously be easy to change, but it would leave the status quo that code fences provide a feature that can't be achieved through indentation. My intention is to find a solution which looks good both as plain text, and as rendered text. If this looks worse than fenced blocks when rendered (due to the added shebang) then it isn't worthwhile.

I don't want to fracture Markdown either, but does that mean innovation is impossible, since there is no body which has responsibility for the format?

gampleman commented 11 years ago

Kind of. But then you still have the problem of code fences having the one up, what if you actually want a shebang at the top of the code? (Quite common, imho). An alternative solution is simply to turn on syntax highlighting for all code blocks and use language auto detection (for which shebangs certainly help).

jnicklas commented 11 years ago

@gampleman in UNIX, shebangs must be absolute paths, that is, they must start with a slash. In other words, this syntax ignores actual shebangs. So this would only be relevant if someone starts a code block with a shebang for another reason than it being part of a script, which I can't imagine is too common.

jnicklas commented 10 years ago

Hello guys, I know there is some grand plan to transition off of sundown at @github, but it is currently still in use right? @vmg, any chance that @github could support this syntax? I write a lot of README files and this would clean them up quite nicely.

FSX commented 10 years ago

@jnicklas Maybe you can ask here: https://github.com/hoedown/hoedown