Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a84543977c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 52 lines (43 sloc) 1.314 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# Title: Google Form
# Authors: Arshad http://donothackcore.com
# Description: A filter to embed Google Forms in Octopress
#
# Syntax {% google_form formkey [message] %}
#
# Examples:
# {% google_form dGVfY3MwcklDcjVrZERGYlRoZWdJQnc6MQ Thank you. I'll get back to you shortly %}
#
require 'open-uri'
require 'nokogiri'

module Jekyll

  class GoogleForm < Liquid::Tag
    @url = ''
    @formkey = ''
    @message= ''
    @formhtml = ''

    def initialize(tag_name, markup, tokens)
      if markup =~ /([a-zA-Z0-9]*)?\s(.+)/
        @formkey = $1
        @message = $2
        
        #url of the Google Form
        @url = "https://docs.google.com/spreadsheet/embeddedform?formkey=#{@formkey}"
        
        #parse the HTML and get the form markup only
        doc = Nokogiri::HTML(open(@url))
        form = doc.xpath("//form").first.unlink
        
        @formhtml = form.to_html
      end
      super
    end

    def render(context)
      if @formhtml
        html = '<div class="google-form-wrapper">'
        html += "<p class='success-msg'>#{@message}</p>"
        html += @formhtml
        html += '</div>'
      else
        "Error processing input, expected syntax: {% google_form formkey [message] %}"
      end
    end
  end
end

Liquid::Template.register_tag('google_form', Jekyll::GoogleForm)
Something went wrong with that request. Please try again.