Files
coffeescript/documentation/v2/code.coffee
Geoffrey Booth 63d3b699d7 [CS2] Fix CS2 docs mobile issues, improve performance (#4688)
* Get rid of offcanvas slide-in; fix sidebar

* We need to transpile docs.coffee, since Safari 9 throws an error on it and that’s too new a browser not to support; but this should also speed things up

* Fix some tabs

* Fix scrollbar

* Use Highlight.js to format placeholder code (during compilation, not rendering) before CodeMirror loads; match Highlight.js styles to CodeMirror styles as best we can

* Improve hash management

* Initialize CodeMirror on demand, on mouseover a particular code example, rather than all examples on startup

* Replace highlight.js with Prism for placeholder syntax highlighting

* Scratch Prism, use CodeMirror itself to do Node-based syntax highlighting, so that on CodeMirror initialization there’s no flash from mismatched highlighting

* Update packages; there’s a new version of Jison! (doesn’t change much)

* Fix mobile issues: use SVG play button icon, to avoid iOS blue square play button; make the code editor text 16px size explicit, to avoid unwanted mobile zoom; make the ‘code play’ buttons work even if the code examples are in placeholder (non-editable) mode, in case a user hasn’t moused over/tapped them

* Update docs output

* Whoops, CodeMirror is only a devDependency
2017-09-07 08:01:12 -07:00

48 lines
1.6 KiB
CoffeeScript

fs = require 'fs'
_ = require 'underscore'
# Use CodeMirror in Node for syntax highlighting, per
# https://github.com/codemirror/CodeMirror/blob/master/bin/source-highlight
CodeMirror = require 'codemirror/addon/runmode/runmode.node.js'
require 'codemirror/mode/coffeescript/coffeescript.js'
require 'codemirror/mode/javascript/javascript.js'
CoffeeScript = require '../../lib/coffeescript'
module.exports = ->
(file, run = no) ->
cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8'
js = CoffeeScript.compile cs, bare: yes # This is just the initial JavaScript output; it is replaced by dynamic compilation on changes of the CoffeeScript pane.
render = _.template fs.readFileSync('documentation/v2/code.html', 'utf-8')
include = (file) -> fs.readFileSync("documentation/v2/#{file}", 'utf-8')
highlight = (language, code) ->
# Adapted from https://github.com/codemirror/CodeMirror/blob/master/bin/source-highlight.
html = ''
curStyle = null
accum = ''
esc = (str) ->
str.replace /[<&]/g, (ch) ->
if ch is '&' then '&amp;' else '&lt;'
flush = ->
if curStyle
html += "<span class=\"#{curStyle.replace /(^|\s+)/g, '$1cm-'}\">#{esc accum}</span>"
else
html += esc accum
CodeMirror.runMode code, {name: language}, (text, style) ->
if style isnt curStyle
flush()
curStyle = style
accum = text
else
accum += text
flush()
html
output = render {file, cs, js, highlight, include, run}