<commit_before>fs = require 'fs'
path = require 'path'
http = require 'http'
url = require 'url'
express = require 'express'
http_proxy = require 'http-proxy'
_ = require 'underscore'
handlers = exports.handlers = require 'preprocessor'
context = require './context'
listing = require './listing'
liveloader = require './liveloader'

# App

exports.VERSION = '0.5.0'

app = express.createServer()
liveloader.enable app
proxy = new http_proxy.RoutingProxy()

ROOT = process.argv[2]

load_source = (file, callback) ->
    path.exists file, (exists) ->
        if exists
            fs.readFile file, 'utf8', (err, content) ->
                callback new handlers.File
                    path: file
                    content: content
        else
            callback null

app.get '*', (req, res, next) ->
    file = ROOT + req.params[0]

    load_source file, (source) ->
        if source
            req.file = source
            next()
        else
            req.file = no
            # try built-in resources (like jquery and underscore)
            resource = path.join listing.here("resources"), req.params[0]
            path.exists resource, (exists) ->
                if exists
                    res.sendfile resource
                else
                    next()

# transforms a generic handler/compiler into an express.js view
register = (handler, app) ->
    for extension in handler.extensions
        match = new RegExp("^(.*\\.#{extension})$")
        app.get match, (req, res) ->
            if handler.mime.output is 'text/html'
                dispatch = 'live'
                variables = context.find_template_variables req.file.path
            else
                dispatch = 'send'
                variables = null
        
            res.contentType handler.mime.output
            # this executes the compiler and sends res[dispatch]
            # along as the callback, so we can easily support both
            # synchronous and asynchronous handlers
            handler.compiler req.file, variables, (output) ->
                res[dispatch] output

# this is where the magic happens and all the handlers 
# we got from `preprocessor` get loaded and registered
for name, handler of handlers.handlers
    register handler, app

# directory listing

app.get /^(.*)\/$/, listing.controller

# If the file loading routine that ran earlier found a file at this path, 
# send that one, or otherwise (since static file loading only happens 
# after we've checked all our custom filetype handlers and we are thus
# at the end of the line) return a 404.
filehandler = (req, res) ->
    if req.file
        res.sendfile req.file.path
    else
        res.send 404

# start server and proxy server<commit_msg>Added a ?raw querystring flag, if you don't want draughtsman to do any compilation on a file.<commit_after>fs = require 'fs'
path = require 'path'
http = require 'http'
url = require 'url'
express = require 'express'
http_proxy = require 'http-proxy'
_ = require 'underscore'
handlers = exports.handlers = require 'preprocessor'
context = require './context'
listing = require './listing'
liveloader = require './liveloader'

# App

exports.VERSION = '0.5.0'

app = express.createServer()
liveloader.enable app
proxy = new http_proxy.RoutingProxy()

ROOT = process.argv[2]

load_source = (file, callback) ->
    path.exists file, (exists) ->
        if exists
            fs.readFile file, 'utf8', (err, content) ->
                callback new handlers.File
                    path: file
                    content: content
        else
            callback null

app.get '*', (req, res, next) ->
    file = ROOT + req.params[0]

    load_source file, (source) ->
        if source
            req.file = source
            next()
        else
            req.file = no
            # try built-in resources (like jquery and underscore)
            resource = path.join listing.here("resources"), req.params[0]
            path.exists resource, (exists) ->
                if exists
                    res.sendfile resource
                else
                    next()

# transforms a generic handler/compiler into an express.js view
register = (handler, app) ->
    for extension in handler.extensions
        match = new RegExp("^(.*\\.#{extension})$")
        app.get match, (req, res) ->        
            if handler.mime.output is 'text/html'
                dispatch = 'live'
                variables = context.find_template_variables req.file.path
            else
                dispatch = 'send'
                variables = null

            if req.query.raw?
                res.contentType handler.mime.source
                res[dispatch] req.file.content
            else
                res.contentType handler.mime.output
                # this executes the compiler and sends res[dispatch]
                # along as the callback, so we can easily support both
                # synchronous and asynchronous handlers
                handler.compiler req.file, variables, (output) ->
                    res[dispatch] output

# this is where the magic happens and all the handlers 
# we got from `preprocessor` get loaded and registered
for name, handler of handlers.handlers
    register handler, app

# directory listing

app.get /^(.*)\/$/, listing.controller

# If the file loading routine that ran earlier found a file at this path, 
# send that one, or otherwise (since static file loading only happens 
# after we've checked all our custom filetype handlers and we are thus
# at the end of the line) return a 404.
filehandler = (req, res) ->
    if req.file
        res.sendfile req.file.path
    else
        res.send 404

# start server and proxy server
{'fraction_non_alphanumeric': 0.09084556254367575, 'fraction_numerical': 0.004542278127183787, 'mean_word_length': 2.0163329820864067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1828517', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1470, 'n_words': 546}