super-powered code blocks with title & links
LeonB opened this issue · comments
I really like the fenced code block thingy of Octopress:
``` ruby Example code
do_somethin()
```
Which generates a figure & figcaption element. The three backticks functionality of redcarpet (for example) doesn't use the extra parameters.
What would be a nice way to implement this in middleman(-syntax)?
I think the best would be to have an option that turns it on, and then you can generate the extra markup.
What I think it should do: first pass it through the code-block parser and then pass it on to the markdown parser. Is there a way in middleman to achieve this?
Check out the source for middleman-syntax
- it plugs into redcarpet
to control how it produces markup for code blocks, so you can just produce something different.
I've already looked at block_code() but this won't get triggered when you put something after the language of you code block. Like this:
``` python test
for i in stuff:
i.do()
```
I've tried using the redcarpet codespan()
and that worked for this:
``` python raaarrrrr
for i in stuff:
i.do()
do_something_else()
```
But not for this (the extra break):
``` python raaarrrrr
for i in stuff:
i.do()
do_something_else()
```
I think I've exhausted my options 😃 So the only option I see left is something of a pre-parser before redcarpet/markdown (that's the way they do it in octopress).
Sounds good.
OK, great 😄 But I have no idea how to even start beginning to implement this in middleman. Do you have any pointers?
Not really... just poke through the source. I don't think we currently have any hook to pre-process files, so you might have to add one.
Is there a way to call a template helper from the code? I have access to the app variable and I want to call the code()
method from middleman-syntax.
I've got it ready! How should this be integrated? Or should I keep it as a separate extension?
I have some minor tweaks I want to make to how your before_content and after_content hooks work, but after that, if you can write form it into a pull request where it's integrated with middleman-syntax, I'd be happy to ship it with the main syntax gem. I'd love to see some tests too, but since I don't have them for the gem itself yet...
OK, I've changed the callbacks - they now look like this:
before_render(template_source, path, locals, template_class)
And the callback should return the modified source (or nil
if it doesn't want to modify anything).
I tried out your extension locally w/ some tweaks, and one thing I think you forgot is to mix your render_code_block
method into the Middleman RedCarpet renderer - I never got the <figure>
element in my output.
I'll have a look at it. What is an easy way to use the version from git? I've always used the gems.
You can just put this in your Gemfile:
gem "middleman", :git => "http://github.com/middleman/middleman"
I've updated the backtick code block. It works for me with the new syntax.
I converted the render_code_block
method from a static one and mixed it in. I don't really understand why but did it nonetheless.
If I have a little time this weekend I'l look into writing tests.
Ping? I've written tests for this repo now, if you need an example.
Please open a PR for this feature when you've gotten it finished.