changeset 417:080dd141898c

Add support for explicit highlighter languages
author nanaya <me@myconan.net>
date Mon, 14 Sep 2015 14:50:35 +0900
parents c4c8214fb0b2
children a69dd2d37950
files app/assets/javascripts/application.coffee app/controllers/pastes_controller.rb app/views/pastes/new.html.erb app/views/pastes/show.html.erb config/locales/en.yml db/migrate/20150914054154_add_language_to_paste.rb db/schema.rb
diffstat 7 files changed, 44 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/app/assets/javascripts/application.coffee	Mon Sep 14 14:48:36 2015 +0900
+++ b/app/assets/javascripts/application.coffee	Mon Sep 14 14:50:35 2015 +0900
@@ -65,3 +65,11 @@
 
   format = window.location.hash.slice(1)
   $(".js-show-tab-#{format}").click()
+
+
+$(document).on "ready", ->
+  $languageSelectBox = $("#paste_language")
+  return unless $languageSelectBox.length
+
+  for language in hljs.listLanguages().sort()
+    $languageSelectBox.append $("<option />", value: language, text: language)
--- a/app/controllers/pastes_controller.rb	Mon Sep 14 14:48:36 2015 +0900
+++ b/app/controllers/pastes_controller.rb	Mon Sep 14 14:50:35 2015 +0900
@@ -68,6 +68,6 @@
   end
 
   def paste_params
-    params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key)
+    params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key, :language)
   end
 end
--- a/app/views/pastes/new.html.erb	Mon Sep 14 14:48:36 2015 +0900
+++ b/app/views/pastes/new.html.erb	Mon Sep 14 14:50:35 2015 +0900
@@ -14,33 +14,39 @@
       %>
   </div></div>
 
-  <div class="row">
-    <div class="form-group col-sm-3 <%= error_class f.object, :key %>">
+  <div class="row"><div class="col-sm-3">
+    <div class="form-group <%= error_class f.object, :key %>">
       <%= f.label :key, t(".deletion_key") %>
       <%= f.text_field :key, :class => "form-control" %>
     </div>
-  </div>
 
-  <div class="form-group">
-    <label><%= t(".privacy.label") %></label>
+    <div class="form-group">
+      <label><%= t(".privacy.label") %></label>
 
-    <div class="radio-container">
-      <% paste_privacy_options.each do |label, value| %>
-        <div class="radio">
-          <label>
-            <%= f.radio_button :is_private, value %>
-            <%= t(".privacy.#{label}") %>
-          </label>
-        </div>
-      <% end %>
+      <div class="radio-container">
+        <% paste_privacy_options.each do |label, value| %>
+          <div class="radio">
+            <label>
+              <%= f.radio_button :is_private, value %>
+              <%= t(".privacy.#{label}") %>
+            </label>
+          </div>
+        <% end %>
+      </div>
     </div>
-  </div>
+
+    <div class="form-group">
+      <%= f.label :language %>
+
+      <%= f.select :language, [], { :prompt => t(".language_none") }, :class => "form-control" %>
+    </div>
 
-  <div class="form-group">
-    <%= f.button :class => "btn btn-primary" %>
+    <div class="form-group">
+      <%= f.button :class => "btn btn-primary" %>
 
-    <%= link_to t(".clear"), "#", :class => "btn btn-danger js-paste-clear" %>
-  </div>
+      <%= link_to t(".clear"), "#", :class => "btn btn-danger js-paste-clear" %>
+    </div>
+  </div></div>
 <% end %>
 
 <div class="modal fade js-paste-preview-md-modal" tabindex="-1" role="dialog">
--- a/app/views/pastes/show.html.erb	Mon Sep 14 14:48:36 2015 +0900
+++ b/app/views/pastes/show.html.erb	Mon Sep 14 14:50:35 2015 +0900
@@ -26,7 +26,7 @@
     </div>
 
     <div role="tabpanel" class="tab-pane" id="hl">
-      <pre class="paste-show__content"><code class="js-paste-hl zp-hl"></code></pre>
+      <pre class="paste-show__content"><code class="js-paste-hl zp-hl lang-<%= @paste.language %>"></code></pre>
     </div>
 
     <div role="tabpanel" class="tab-pane" id="md">
--- a/config/locales/en.yml	Mon Sep 14 14:48:36 2015 +0900
+++ b/config/locales/en.yml	Mon Sep 14 14:50:35 2015 +0900
@@ -17,6 +17,7 @@
     new:
       clear: Clear
       deletion_key: Deletion Key
+      language_none: none (autodetect)
       preview_md: Markdown Preview
       privacy:
         label: Privacy
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db/migrate/20150914054154_add_language_to_paste.rb	Mon Sep 14 14:50:35 2015 +0900
@@ -0,0 +1,5 @@
+class AddLanguageToPaste < ActiveRecord::Migration
+  def change
+    add_column :pastes, :language, :string
+  end
+end
--- a/db/schema.rb	Mon Sep 14 14:48:36 2015 +0900
+++ b/db/schema.rb	Mon Sep 14 14:50:35 2015 +0900
@@ -11,9 +11,9 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20130512032335) do
+ActiveRecord::Schema.define(version: 20150914054154) do
 
-  create_table "pastes", force: true do |t|
+  create_table "pastes", force: :cascade do |t|
     t.string   "ip",         limit: 50,  null: false
     t.text     "paste",                  null: false
     t.string   "paste_hash", limit: 150, null: false
@@ -21,6 +21,7 @@
     t.datetime "updated_at",             null: false
     t.string   "key"
     t.string   "secret"
+    t.string   "language"
   end
 
   add_index "pastes", ["ip", "paste_hash"], name: "index_pastes_on_ip_and_hash", unique: true, using: :btree