
/**
 * http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
 * https://github.com/broofa/node-mime/blob/master/types/node.types
 *
 * Convert these text files to JSON for browser usage.
 */

var co = require('co')
var fs = require('fs')
var path = require('path')
var cogent = require('cogent')

function* get(url) {
  var res = yield* cogent(url, {
    string: true
  })

  if (res.statusCode !== 200)
    throw new Error('got status code ' + res.statusCode + ' from ' + url)

  var text = res.text
  var json = {}
  // http://en.wikipedia.org/wiki/Internet_media_type#Naming
  var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/
  text = text.split('\n')
  .filter(Boolean)
  .forEach(function (line) {
    line = line.trim()
    if (!line) return
    var match = re.exec(line)
    if (!match) return
    json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '').split(/\s+/).filter(Boolean)
  })
  fs.writeFileSync('lib/' + path.basename(url).split('.')[0] + '.json',
    JSON.stringify(json, null, 2))
}

co(function* () {
  yield [
    get('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'),
    get('https://raw.githubusercontent.com/broofa/node-mime/master/types/node.types')
  ]
})()

@@ -589,16 +589,502 @@
 #Naming%0A
+  /**%0A   * Mime types and associated extensions are stored in the form:%0A   *%0A   *   %3Ctype%3E %3Cext%3E %3Cext%3E %3Cext%3E%0A   *%0A   * And some are commented out with a leading %60#%60 because they have no associated extensions.%0A   * This regexp checks whether a single line matches this format, ignoring lines that are just comments.%0A   * We could also just remove all lines that start with %60#%60 if we want to make the JSON files smaller%0A   * and ignore all mime types without associated extensions.%0A   */%0A
   var re
@@ -1129,16 +1129,16 @@
 %5Cw+)*$/%0A
-
   text =
@@ -1297,24 +1297,89 @@
 tch) return%0A
+    // remove the leading # and %3Ctype%3E and return all the %3Cext%3Es%0A
     json%5Bmat
@@ -1435,16 +1435,23 @@
 +)/, '')
+%0A      
 .split(/
@@ -1455,16 +1455,23 @@
 t(/%5Cs+/)
+%0A      
 .filter(

comment on regexps