From 4c258caba245fbd311b0e8f157a9d8a5fc1c54b7 Mon Sep 17 00:00:00 2001 From: Petrik Date: Sat, 11 Jul 2020 12:23:37 +0200 Subject: [PATCH] Add copy button for copying guide code examples [skip ci] Instead of carefully selecting the code in examples, use a copy button for copying the code. This uses https://clipboardjs.com/ for copying. For the bash code examples only line starting with $ prompt are copied. The $ prompt itself is not copied. --- guides/assets/javascripts/clipboard.js | 7 +++++++ guides/assets/javascripts/guides.js | 11 +++++++++++ guides/assets/stylesheets/main.css | 21 +++++++++++++++++++++ guides/assets/stylesheets/main.rtl.css | 21 +++++++++++++++++++++ guides/rails_guides/markdown/renderer.rb | 24 +++++++++++++++++++----- guides/source/layout.html.erb | 1 + 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 guides/assets/javascripts/clipboard.js diff --git a/guides/assets/javascripts/clipboard.js b/guides/assets/javascripts/clipboard.js new file mode 100644 index 00000000000..b9ed143b3e1 --- /dev/null +++ b/guides/assets/javascripts/clipboard.js @@ -0,0 +1,7 @@ +/*! + * clipboard.js v2.0.6 + * https://clipboardjs.com/ + * + * Licensed MIT © Zeno Rocha + */ +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ClipboardJS=e():t.ClipboardJS=e()}(this,function(){return o={},r.m=n=[function(t,e){t.exports=function(t){var e;if("SELECT"===t.nodeName)t.focus(),e=t.value;else if("INPUT"===t.nodeName||"TEXTAREA"===t.nodeName){var n=t.hasAttribute("readonly");n||t.setAttribute("readonly",""),t.select(),t.setSelectionRange(0,t.value.length),n||t.removeAttribute("readonly"),e=t.value}else{t.hasAttribute("contenteditable")&&t.focus();var o=window.getSelection(),r=document.createRange();r.selectNodeContents(t),o.removeAllRanges(),o.addRange(r),e=o.toString()}return e}},function(t,e){function n(){}n.prototype={on:function(t,e,n){var o=this.e||(this.e={});return(o[t]||(o[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){var o=this;function r(){o.off(t,r),e.apply(n,arguments)}return r._=e,this.on(t,r,n)},emit:function(t){for(var e=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),o=0,r=n.length;o -
#{code}
- + formatted_code = formatter.format(lexer.lex(code)) + clipboard_id = "clipboard-#{SecureRandom.hex(16)}" + <<~HTML +
+
#{formatted_code}
+ + +
HTML end @@ -79,6 +82,17 @@ module RailsGuides end end + def clipboard_content(code, language) + if language == "bash" + prompt_regexp = /^\$ / + code = code.split("\n"). + select { |line| line =~ prompt_regexp }. + map { |line| line.gsub(prompt_regexp, "") }. + join("\n") + end + ERB::Util.h(code) + end + def convert_notes(body) # The following regexp detects special labels followed by a # paragraph, perhaps at the end of the document. diff --git a/guides/source/layout.html.erb b/guides/source/layout.html.erb index 1b854edccc3..72fe9125870 100644 --- a/guides/source/layout.html.erb +++ b/guides/source/layout.html.erb @@ -9,6 +9,7 @@ +