Commits

David Chambers committed fe3dc81

replace google-code-prettify with Highlight.js

Comments (0)

Files changed (8)

+^concat[.]min[.]js$
 ^hashify[.]css$
+^highlight[.]js/
 ^marked/
 fs = require 'fs'
-path = require 'path'
 {exec} = require 'child_process'
 
 CoffeeScript = require 'coffee-script'
 
 
 files = [
-  'prettify.js'
   'base64/base64.js'
   'marked/lib/marked.js'
+  'highlight.js/build/highlight.pack.js'
   'hashify-editor/hashify-editor.js'
   'src/settings.coffee'
   'src/utils.coffee'
     data = fs.readFileSync filename, 'utf8'
     if /[.]coffee$/.test filename then CoffeeScript.compile data else data
 
-  {ast_mangle, ast_squeeze, gen_code} = uglify
-  scripts = (read f for f in files)
-  if key = options['ga-key']
-    scripts.push read('src/ga.coffee').replace /<KEY>/g, key
-  data = gen_code ast_squeeze ast_mangle parser.parse scripts.join ';'
+  # Include CoffeeScript lexer in addition to "common" lexers.
+  command = 'python highlight.js/tools/build.py :common coffeescript'
+  exec command, (err, stdout, stderr) ->
+    if err
+      console.error stderr.trim()
+      process.exit 1
 
-  if options.output? then fs.writeFileSync options.output, data, 'utf8'
-  else console.log data
+    {ast_mangle, ast_squeeze, gen_code} = uglify
+    scripts = (read f for f in files)
+    if key = options['ga-key']
+      scripts.push read('src/ga.coffee').replace /<KEY>/g, key
+    data = gen_code ast_squeeze ast_mangle parser.parse scripts.join ';'
+
+    if options.output? then fs.writeFileSync options.output, data, 'utf8'
+    else console.log data
 
 task 'build:styles', 'generate style sheet from Sass file', (options) ->
   args = ['--sass-dir .', '--css-dir .']
     args.push '--environment production'
 
   exec "compass compile #{ args.join ' ' }", (err, stdout) ->
-    console.log stdout.trim()
+    console.log output = stdout.trim()
+    # Strip colour codes before comparing output.
+    return if output.replace(/\u001b\[\d+m/g, '') is 'unchanged hashify.sass'
+
+    text = fs.readFileSync 'highlight.js/src/styles/github.css', 'utf8'
+    unless options.development
+      text = text
+        .replace(/\/\*[\s\S]+\*\//, '')   # strip header comment
+        .replace(/\n/g, '')               # strip line breaks
+        .replace(/[ ]+(?=\{)/g, '')       # strip spaces before "{"
+        .replace(/(\{|:|;)[ ]+/g, '$1')   # strip spaces after "{", ":" and ";"
+        .replace(/;(?=\})/g, '')          # strip semicolons before "}"
+    fs.appendFileSync 'hashify.css', text
 
 task 'server', 'start the development server', ->
   serve = (fn) -> (req, res) ->
       filename = [first, 'src', rest..., last].join '/'
 
     filename = __dirname + filename
-    path.exists filename, (exists) ->
-      if exists
-        if compile
-          res.contentType 'js'
-          res.send CoffeeScript.compile fs.readFileSync filename, 'utf8'
-        else
-          res.sendfile filename
+    if fs.existsSync filename
+      if compile
+        res.contentType 'js'
+        res.send CoffeeScript.compile fs.readFileSync filename, 'utf8'
       else
-        res.redirect '/' + Buffer('# 400 Bad Request').toString('base64')
+        res.sendfile filename
+    else
+      res.redirect '/' + Buffer('# 400 Bad Request').toString('base64')
 
   app = express.createServer()
   app.get /^\/[A-Za-z0-9+/=]*$/, serve -> '/index.html'
 
 # Map synonyms to canonical symbols.
 symbols = {
-  :docco    => ['-d', '--docco'],
-  :help     => ['-h', '--help'],
-  :mode     => ['-m', '--mode'],
-  :open     => ['-o', '--open'],
-  :prettify => ['-p', '--prettify'],
-  :raw      => ['-r', '--raw'],
+  :docco => ['-d', '--docco'],
+  :help  => ['-h', '--help'],
+  :mode  => ['-m', '--mode'],
+  :open  => ['-o', '--open'],
+  :raw   => ['-r', '--raw'],
 }
 
 # Generate a hash with all the valid flags as its keys.
 if args[:help]
   puts '''
     usage: hashify path/to/file [options]
-    
+
     options:
       -d, --docco               use docco.hashify.me rather than hashify.me
       -m, --mode MODE           editor is hidden in "presentation" mode
       -o, --open                open document in default web browser
-      -p, --prettify [yes|no]   google-code-prettify is invoked unless "no"
       -r, --raw                 generate URL for text/plain version
       -h, --help                show this overview
   '''.gsub /^[ ]{4}|\s+\Z/, ''
 # Ignore unqualified, invalid, or implicit `--mode`.
 params[:mode] = args[:mode] if modes.include? args[:mode]
 
-# Only include "prettify" parameter if `--prettify no`.
-params[:prettify] = :no if not args[:docco] and args[:prettify] == false
-
 # Hashify uses "k1:v1;k2:v2" rather than "k1=v1&k2=v2".
 search = params.map {|param, value| "#{param}:#{value}"}.join ';'
 search = "?#{search}" unless search.empty?
 
   # It's necessary to treat both `nil` and `""` as exit conditions.
   # Virtually always, `idx` will be larger than `contents.length`:
-  # 
+  #
   #     >> 'foo'[7..-1]
   #     => nil
-  # 
+  #
   # Should the two happen to be equal, though, the result is `""`:
-  # 
+  #
   #     >> 'qux bar'[7..-1]
   #     => ""
   until contents.nil? or contents.empty?
     # Three characters of binary text generally produce four characters
     # of ASCII text when Base64 encoded:
-    # 
+    #
     #     >> encode 'foo'
     #     => "Zm9v"
-    # 
+    #
     # Characters outside a defined range cannot be represented in ASCII
     # so succinctly:
-    # 
+    #
     #     >> encode '𝄞𝄞𝄞'
     #     => "8J2EnvCdhJ7wnYSe"
     #     >> '𝄞𝄞𝄞'.length
     #     => 3
-    # 
+    #
     # First we take the longest slice that *may* produce a string of an
     # acceptable length. We then Base64 encode the slice. If the length
     # of the resulting string exceeds `max`, we decrement `idx` and try
   padding: 1px
   white-space: pre-wrap
   > code
-    display: block
-    padding: 12px 18px
     font-size: 18px
     line-height: 24px
+    body &
+      padding: 12px 18px
 
 code
-  background: #eee
+  background: #f8f8ff
   padding: 0 0.25em
   line-height: 1
 
     color: #333
     outline: none
 
-// syntax highlighting
-
-@media screen
-  .kwd, .tag
-    color: #008
-  .atv, .str
-    color: #080
-  .com
-    color: #800
-  .clo, .opn, .pun
-    color: #660
-  .atn, .dec, .typ, .var
-    color: #606
-  .lit
-    color: #066
-  .fun
-    color: red
-
 @media print
   #sidebar
     display: none !important
           <li><a href=https://bitbucket.org/davidchambers/base64.js>Base64</a></li>
           <li><a href=https://bitbucket.org/davidchambers/hashify.me>Hashify</a></li>
           <li><a href=https://bitbucket.org/davidchambers/hashify-editor>Hashify Editor</a></li>
+          <li><a href=https://github.com/isagalaev/highlight.js>Highlight.js</a></li>
           <li><a href=https://github.com/chjj/marked>marked</a></li>
-          <li><a href=http://code.google.com/p/google-code-prettify/>Prettify</a></li>
         </ul>
         <p>Copyright 2011 <a href=http://twitter.com/davidchambers>David Chambers</a>.</p>
       </div>
         }
         if (location.hostname == '127.0.0.1') {
           files = [
-            '/prettify.js',
             '/base64/base64.js',
             '/marked/lib/marked.js',
+            '/highlight.js/build/highlight.pack.js',
             '/hashify-editor/hashify-editor.js',
             '/lib/settings.js',
             '/lib/utils.js',
     "coffee-script": "1.3.x",
     "express": "2.5.x",
     "uglify-js": "1.3.x"
+  },
+  "engines": {
+    "node": "0.8.x"
   }
 }

src/highlight.coffee

-{components} = Hashify.location
 {subscribe} = Hashify.utils
 
 # This is a live collection:
 nodeList = document.getElementsByTagName 'code'
 
 subscribe 'post:render', ->
-  # Apply syntax highlighting unless instructed otherwise.
-  return if components().query.contains 'prettify:no'
   for node in nodeList when /^lang-/.test node.className
-    node.className += ' prettyprint'
-  do prettyPrint
+    node.className = node.className.replace 'lang', 'language'
+    hljs.highlightBlock node

src/location.coffee

 directives = [
   # name,  in longest query
   ['mode:presentation', yes]
-  ['prettify:no',       yes]
   ['raw:yes',            no]
 ]