Skip to content

Commit

Permalink
Move the codeFor function into versioned folders, so that v1 and v2…
Browse files Browse the repository at this point in the history
… docs can have different example code blocks/editors
  • Loading branch information
GeoffreyBooth committed Dec 13, 2016
1 parent ff1725b commit 85b3be9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
26 changes: 1 addition & 25 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,7 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
</h2>
"""

codeFor = ->
counter = 0
hljs = require 'highlight.js'
hljs.configure classPrefix: ''
(file, executable = false, showLoad = true) ->
counter++
cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8'
js = CoffeeScript.compile cs, bare: yes
js = js.replace /^\/\/ generated.*?\n/i, ''

cshtml = "<pre><code>#{hljs.highlight('coffeescript', cs).value}</code></pre>"
# Temporary fix until highlight.js adds support for newer CoffeeScript keywords
# Added in https://github.com/isagalaev/highlight.js/pull/1357, awaiting release
if file in ['generator_iteration', 'generators', 'modules']
cshtml = cshtml.replace /(yield|import|export|from|as|default) /g, '<span class="keyword">$1</span> '
jshtml = "<pre><code>#{hljs.highlight('javascript', js).value}</code></pre>"
append = if executable is yes then '' else "alert(#{executable});".replace /"/g, '&quot;'
if executable and executable isnt yes
cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}"
run = if executable is true then 'run' else "run: #{executable}"
name = "example#{counter}"
script = "<script>window.#{name} = #{JSON.stringify cs}</script>"
load = if showLoad then "<div class='minibutton load' onclick='javascript: loadConsole(#{name});'>load</div>" else ''
button = if executable then """<div class="minibutton ok" onclick="javascript: #{js.replace /"/g, '&quot;'};#{append}">#{run}</div>""" else ''
"<div class='code'>#{cshtml}#{jshtml}#{script}#{load}#{button}<br class='clear' /></div>"
codeFor = require "./documentation/v#{majorVersion}/code.coffee"

htmlFor = ->
marked = require 'marked'
Expand Down
29 changes: 29 additions & 0 deletions documentation/v1/code.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
fs = require 'fs'
CoffeeScript = require '../../lib/coffee-script'


module.exports = ->
counter = 0
hljs = require 'highlight.js'
hljs.configure classPrefix: ''
(file, executable = no, showLoad = yes) ->
counter++
cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8'
js = CoffeeScript.compile cs, bare: yes
js = js.replace /^\/\/ generated.*?\n/i, ''

cshtml = "<pre><code>#{hljs.highlight('coffeescript', cs).value}</code></pre>"
# Temporary fix until highlight.js adds support for newer CoffeeScript keywords
# Added in https://github.com/isagalaev/highlight.js/pull/1357, awaiting release
if file in ['generator_iteration', 'generators', 'modules']
cshtml = cshtml.replace /(yield|import|export|from|as|default) /g, '<span class="keyword">$1</span> '
jshtml = "<pre><code>#{hljs.highlight('javascript', js).value}</code></pre>"
append = if executable is yes then '' else "alert(#{executable});".replace /"/g, '&quot;'
if executable and executable isnt yes
cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}"
run = if executable is yes then 'run' else "run: #{executable}"
name = "example#{counter}"
script = "<script>window.#{name} = #{JSON.stringify cs}</script>"
load = if showLoad then "<div class='minibutton load' onclick='javascript: loadConsole(#{name});'>load</div>" else ''
button = if executable then """<div class="minibutton ok" onclick="javascript: #{js.replace /"/g, '&quot;'};#{append}">#{run}</div>""" else ''
"<div class='code'>#{cshtml}#{jshtml}#{script}#{load}#{button}<br class='clear' /></div>"

0 comments on commit 85b3be9

Please sign in to comment.