Epoch's posterous

It's only a model.

Net::HTTPBadResponse: wrong status line: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"

If you ever see: Net::HTTPBadResponse: wrong status line "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"

Then you probably forgot to use ssl with your request to port 443. Apache coughed up a 400 error page over ssl in a way that caused the ruby standard library Net::HTTP to vomit exceptions all over your application.

This behavior was discovered on with Ruby 1.8.7 patch level 352. Judging by how it affected all the development system at my work, it affects all Ruby 1.8.7 patch levels equally. I have no ideal what happens with Ruby 1.9.

Filed under  //   documentation   ruby  

Capistrano: use git repository on the same server you deploy to

I ran across an interesting problem the other night. I finally convinced myself that I need to set up automated deployment for a personal website I was working on. The website is written in Ruby on Rails and I decided to use capistrano for deployment like ever other person I know that uses rails. The first problem I had was the capistrano docs are really really lacking in details. The tutorial covers the basics but that only works if you aren't doing something wacky.

 

I'm using git for version control which isn't all that odd. What is odd is that I don't have a remote repository. I don't really need to because I backup my laptop almost every night.  I don't want capistrano to use the copy method for deployment because I don't want to be trying to push several hundred kilobytes of data over a coffee shop's overloaded public wifi for every deployment.

 

I didn't want to use a git host like github or any one of a hundred others because I already rent a Virtual Private Server from slicehost.com. (256 slice to be specific) So I decided to just use git over ssh and store a copy of my repository on my server. It was pretty easy to create an empty repository on my server and push my local copy over. Capistrano however did not like this. My website needed to deploy to the same sever my remote repository was sitting on. There is no tutorial or examples for doing this. I googled for over an hour and read all the docs on capistrano.

 

The problem with capistrano is that by default the :repository variable only paths to urls or files on your local machine. I couldn't find a way to tell capistrano to look on the deployment server for the repository.

 

The solution:

set :repository, "file:///srv/git/repository.git"set :local_repository, "file://."

The above works because setting undocumented variable :local_repository tells capistrano that :repository is a location on the app server. Suddenly, I'm able to deploy using export instead of copy. I hope I saved someone else hours of searching to figure out how to do this.

 

Filed under  //   capistrano   documentation   ruby  

Using BlueCloth 2.0

This is a quick reference for how to use the BlueCloth 2.0 gem. I couldn't find anything anywhere on how to use this gem so I used irb and the C source of the plugin to figure it out.

Require the gem in a generic ruby project.

require 'rubygems'  #needed to load gems.
gem 'bluecloth', '>= 2.0.0'  #need to specify we want the 2.0 version, not the 1.0 version.
require 'bluecloth'

If you are using Ruby on Rails just add the line below to your environment file.

config.gem 'bluecloth', :version => '>= 2.0.0'

Once you have the gem included you can make a wrapper function

def markdown_parse(str)
  bc = BlueCloth.new(str)
  bc.to_html
end

or a one-liner

html = BlueCloth.new(str).to_html()

If you want to get more complicated and change the default options.

def markdown_parse(str, options={})
  options = {
    :escape_html => true,
    :strict_mode => false,
  }.update(options)
  bc = BlueCloth.new(str, options)
  bc.to_html
end

A full list of options supported by BlueCloth 2.0.5 is below. Be sure to read the notes, some of them are important. You need to be using an html sanitizer with BlueCloth (such as rgrove's sanitize) if you allow user-submitted html to be used with markdown.

 

:remove_links => false

Disable links in markdown. This will ignore the link syntax and break a tags in an unsafe manner. Recommend using a sanitizer instead.

WARNING test(This isn't displaying correctly because of a posterous bug) is converted to <p>&lt;a href="testing">test</a></p>which leaves a trailing </a> which is unsafe. It's not dangerous but it could break the html on your site. It might also be possible to slip a tag past this using unicode or other tricks which is why I recommend using a sanitizer instead of this option.

 

:remove_images => false

Disable images in markdown. This will ignore the link syntax and break image tags naively using the same method as above. Recommend using a sanitizer instead.

 

:smartypants => true

Enable smartypants with markdown

 

:pseudoprotocols => false

Allow url syntax to create named anchors using id: and styled span tags using class:. Unsafe, allows duplicate ids in h tags. Html spec requires ids to be unique.

[just as he said](id:foo) converts to just as he said 
[just as he said](class:foo) converts to <span class="foo">just as he said</span>


:pandoc_headers => false

Enable Discount extension named "pandoc_headers

This the unit test for this feature, I've never used it myself.

it "correctly applies the :pandoc_headers option" do
  input = "% title\n% author1, author2\n% date\n\nStuff."
  bc = BlueCloth.new( input, :pandoc_headers => true )
  bc.header.should == {
    :title => 'title',
    :author => 'author1, author2',
    :date => 'date'
  }
  bc.to_html.should == '<p>Stuff.</p>'
end

 

:header_labels => false

Auto generate ids for h tags. Unsafe, allows duplicate ids in h tags. Html spec requires ids to be unique.

# A header
Some stuff

## A header
More stuff.

is converted to

<h1 id="A+header">A header</h1>
<p>Some stuff</p>
<h2 id="A+header">A header</h2>
<p>More stuff.</p>

 

:escape_html => false

Escape any html the parser finds. Given how naive the html escaping is in :remove_links I would recommend using a sanitizer on top of this.

 

:strict_mode => true

When on, it follows the markdown spec strictly allowing the use of intraword emphasis. (T*hi*s converts to This) When off, intraword emphasis are disabled and a subscript notation is added. I recommend setting this to false because intraword emphasis is annoying.

 

:auto_links => false

Convert bare urls in text into clickable links.

 

:safe_links => false

Only allow links that have recognized protocols to be parsed. Turning this on prevents relative urls from working. I recommend leaving this off and using a sanitizer instead.

Filed under  //   coding   documentation