middleman / middleman-syntax

Code syntax highlighting plugin via Rouge for Middleman
https://middlemanapp.com
MIT License
179 stars 57 forks source link

super-powered code blocks with title & links #8

Closed LeonB closed 11 years ago

LeonB commented 11 years ago

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)?

[Original bugreport](https://github.com/middleman/middleman/issues/708)
bhollis commented 11 years ago

I think the best would be to have an option that turns it on, and then you can generate the extra markup.

LeonB commented 11 years ago

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?

bhollis commented 11 years ago

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.

LeonB commented 11 years ago

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()
LeonB commented 11 years ago

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):
          for i in stuff:
              i.do()

          do_something_else()


I think I've exhausted my options :smiley: 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).
bhollis commented 11 years ago

Sounds good.

LeonB commented 11 years ago

OK, great :smile: But I have no idea how to even start beginning to implement this in middleman. Do you have any pointers?

bhollis commented 11 years ago

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.

LeonB commented 11 years ago

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.

LeonB commented 11 years ago

I've got it ready! How should this be integrated? Or should I keep it as a separate extension?

bhollis commented 11 years ago

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...

bhollis commented 11 years ago

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.

LeonB commented 11 years ago

I'll have a look at it. What is an easy way to use the version from git? I've always used the gems.

bhollis commented 11 years ago

You can just put this in your Gemfile:

gem "middleman", :git => "http://github.com/middleman/middleman"
LeonB commented 11 years ago

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.

bhollis commented 11 years ago

Ping? I've written tests for this repo now, if you need an example.

bhollis commented 11 years ago

Please open a PR for this feature when you've gotten it finished.