gma / nesta

File Based CMS and Static Site Generator
http://nestacms.com
MIT License
902 stars 122 forks source link

rack-codehighlighter and nesta #1

Closed chikamichi closed 14 years ago

chikamichi commented 14 years ago

Hi! I strive to plug rack-codehighlighter into nesta, but I did not manage to.

http://gist.github.com/264691 is an example of rack-codehighlighter used within a Sinatra app, without any haml layer. It works as expected (ruby test.rb, localhost:4567 -> the pre has been processed by ultraviolet). I tried to do the same in nesta, but nothing gets processed. I tried to add the haml processing step to my simple test.rb, did not succeed :) So I'll keep working on it, but if you have any hint on this…

gma commented 14 years ago

Hi. Sorry for the delay, I was a little busy there...

I'm having a bit of trouble getting ultraviolet installed on my Mac (though Coderay works fine). I don't think that will matter too much though, as the problem seems to be that new lines inside <pre/> tags are converted to the equivalent XHTML escape code (which is &#x000A;). I think these escape codes will be confusing rack-codehighlighter (and if it's actually Coderay that is getting confused – I've not dug into the source yet, there's a good chance that ultraviolet is too).

I'm not best sure how to sort this out yet. I'm going to go and get a coffee and collect my thoughts, but wanted to let you know that I'm on the case in the meantime.

Graham

gma commented 14 years ago

Okay, I think I've found a nice solution. The &#x000A; characters were getting inserted by HAML. From the HAML FAQ:

Because Haml automatically indents the HTML source code, the contents of whitespace-sensitive tags like +pre+ and +textarea+ can get screwed up. The solution is to replace the newlines inside these tags with HTML newline entities (&#x000A;), ...

I did some tests and verified that the &#x000A; codes were confusing both rack-codehighlighter and Coderay. It was fairly easy to patch rack-codehighlighter so that it treated &#x000A; as a new line character, and I've commited a patch to my rack-codehighlighter fork on GitHub:

We'll have to see what Wlodek makes of it; he may have a better solution up his sleeve, or feel that the fix doesn't belong in rack-codehighlighter.

Anyway, I suspect that anybody else who wants to use rack-codehighlighter with HTML rendered by HAML will run into similar problems. They certainly will if they use HAML's ~ command to insert HTML into a template with whitespace preserved, which is what Nesta is doing in the page.haml file.

Note that my patch to rack-codehighlighter changes the default regex, so you will either need to update your :pattern => /.../ code, or remove it so that the default is used.

This was all I needed to get Coderay working:

require "coderay"
require "rack/codehighlighter"

use Rack::Codehighlighter, :coderay, :element => "pre>code", :markdown => true

I created a local directory in my top level Nesta directory and added put that code in local/app.rb. It's loaded automatically if it exists, which means that you won't have to worry about merge conflicts in future if I add some similar code. Alternatively config.ru would be a good place to put it.

Also bear in mind that because Nesta's file caching writes files to public/ that your web server may bypass Rack/Sinatra entirely and just serve the file off disk. I've got Nginx configured to behave like this on my sites. If this is the case the codehighlighter middleware won't get a chance to modify the HTML. HTTP caching (i.e. Rack::Cache) would be a good way to fix this; it's on the todo list...

Hope this has helped. Sorry it took me a few days to get to it.

Cheers, Graham

chikamichi commented 14 years ago

Hi. I'm glad you fixed the issue so quickly and were kind enough to write this very clear explanation! I've been quite busy lately so I did not tried to get it working on my side, but I'll sure be able to hack on it within the next few days.

gma commented 14 years ago

Wlodek has sent me a message to say that the patch has been pulled into rack-codehighlighter, and that he's pushed the latest gem (0.4.1) to gemcutter. I suspect that a quick gem update should get it all working...