From abf910d210ffa722ca4c4f69daa95aaedd8b396f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Thu, 2 Jan 2014 16:22:04 +0100
Subject: [PATCH] FEATURE: allow for custom HTML inside the <head> tags

---
 .../templates/site_content_edit.js.handlebars | 54 +++++++++----------
 .../stylesheets/common/admin/admin_base.scss  |  6 ++-
 app/models/site_content.rb                    |  6 +--
 app/models/site_content_type.rb               |  7 +--
 app/views/layouts/application.html.erb        |  2 +
 app/views/layouts/no_js.html.erb              |  1 +
 config/locales/server.en.yml                  |  5 +-
 7 files changed, 41 insertions(+), 40 deletions(-)

diff --git a/app/assets/javascripts/admin/templates/site_content_edit.js.handlebars b/app/assets/javascripts/admin/templates/site_content_edit.js.handlebars
index 491019ca6..5d3cdae71 100644
--- a/app/assets/javascripts/admin/templates/site_content_edit.js.handlebars
+++ b/app/assets/javascripts/admin/templates/site_content_edit.js.handlebars
@@ -1,32 +1,26 @@
+<h3>{{model.title}}</h3>
+<p class='description'>{{model.description}}</p>
 
-  <h3>{{model.title}}</h3>
-  <p class='description'>{{model.description}}</p>
+{{#if model.markdown}}
+  {{pagedown value=model.content}}
+{{/if}}
+{{#if model.plainText}}
+  {{textarea value=model.content class="plain"}}
+{{/if}}
+{{#if model.html}}
+  {{aceEditor content=model.content mode="html"}}
+{{/if}}
+{{#if model.css}}
+  {{aceEditor content=model.content mode="css"}}
+{{/if}}
 
-  {{#if model.markdown}}
-    {{pagedown value=model.content}}
-  {{/if}}
-
-  {{#if model.plainText}}
-    {{textarea value=model.content class="plain"}}
-  {{/if}}
-
-  {{#if model.html}}
-    {{aceEditor content=model.content mode="html"}}
-  {{/if}}
-
-  {{#if model.css}}
-    {{aceEditor content=model.content mode="css"}}
-  {{/if}}
-
-
-
-  <div class='controls'>
-    <button class='btn' {{action saveChanges}} {{bindAttr disabled="saveDisabled"}}>
-      {{#if saving}}
-        {{i18n saving}}
-      {{else}}
-        {{i18n save}}
-      {{/if}}
-    </button>
-    {{#if saved}}{{i18n saved}}{{/if}}
-  </div>
+<div class='controls'>
+  <button class='btn' {{action saveChanges}} {{bindAttr disabled="saveDisabled"}}>
+    {{#if saving}}
+      {{i18n saving}}
+    {{else}}
+      {{i18n save}}
+    {{/if}}
+  </button>
+  {{#if saved}}{{i18n saved}}{{/if}}
+</div>
diff --git a/app/assets/stylesheets/common/admin/admin_base.scss b/app/assets/stylesheets/common/admin/admin_base.scss
index 836a9e23e..27cc5f56d 100644
--- a/app/assets/stylesheets/common/admin/admin_base.scss
+++ b/app/assets/stylesheets/common/admin/admin_base.scss
@@ -32,6 +32,10 @@ td {border-bottom: 1px solid #ddd; border-top: 1px solid #ddd;}
     p {
       margin-left: 6px;
     }
+
+    .ace-wrapper {
+      height: 400px;
+    }
   }
 
   table.report {
@@ -957,4 +961,4 @@ table.api-keys {
   .modal-tab {
     width: 95%;
   }
-}
\ No newline at end of file
+}
diff --git a/app/models/site_content.rb b/app/models/site_content.rb
index e8aa741a4..71d899ea5 100644
--- a/app/models/site_content.rb
+++ b/app/models/site_content.rb
@@ -20,9 +20,9 @@ class SiteContent < ActiveRecord::Base
   add_content_type :login_required_welcome_message, default_18n_key: 'login_required.welcome_message'
   add_content_type :privacy_policy, allow_blank: true
   add_content_type :faq, allow_blank: true
-  add_content_type :top, allow_blank: true
-  add_content_type :bottom, allow_blank: true
-
+  add_content_type :top, allow_blank: true, format: :html
+  add_content_type :bottom, allow_blank: true, format: :html
+  add_content_type :head, allow_blank: true, format: :html
 
   def site_content_type
     @site_content_type ||= SiteContent.content_types.find {|t| t.content_type == content_type.to_sym}
diff --git a/app/models/site_content_type.rb b/app/models/site_content_type.rb
index 5ba91888c..1b825accc 100644
--- a/app/models/site_content_type.rb
+++ b/app/models/site_content_type.rb
@@ -21,10 +21,7 @@ class SiteContentType
   end
 
   def default_content
-    if @opts[:default_18n_key].present?
-      return I18n.t(@opts[:default_18n_key])
-    end
-    ""
+    @opts[:default_18n_key].present? ? I18n.t(@opts[:default_18n_key]) : ""
   end
 
-end
\ No newline at end of file
+end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 6568a400c..a2aeb8553 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -25,6 +25,8 @@
 
     <%= discourse_csrf_tags %>
 
+    <%= raw SiteContent.content_for(:head) %>
+
     <%= yield :head %>
   </head>
 
diff --git a/app/views/layouts/no_js.html.erb b/app/views/layouts/no_js.html.erb
index bd9da09fc..871230b50 100644
--- a/app/views/layouts/no_js.html.erb
+++ b/app/views/layouts/no_js.html.erb
@@ -10,6 +10,7 @@
     <%= render :partial => "common/special_font_face" %>
     <%= render :partial => "common/discourse_stylesheet" %>
     <%= discourse_csrf_tags %>
+    <%= raw SiteContent.content_for(:head) %>
   </head>
   <body>
     <%- unless customization_disabled? %>
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d4372debb..d87eba7b1 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -499,9 +499,12 @@ en:
     login_required:
       title: "Login Required: Homepage"
       description: "The text displayed for unauthorized users when login is required on the site."
+    head:
+      title: "HTML head"
+      description: "HTML that will be inserted inside the <head></head> tags"
     top:
       title: "Top of the pages"
-      description: "HTML that will be added at the top of every pages (after the header, before the navigation or the topic title)"
+      description: "HTML that will be added at the top of every pages (after the header, before the navigation or the topic title)."
     bottom:
       title: "Bottom of the pages"
       description: "HTML that will be added at the bottom of every pages"