Refactor typo

Refactor typo

easing   = {}
funcs    = {}
defaults = {
  quad:    "x^2"
  cubic:   "x^3"
  quart:   "x^4"
  quint:   "x^5"
  expo:    "2^(10 * (x - 1))"
  sine:    "-math.cos(x * (math.pi * 0.5)) + 1"
  circ:    "-(math.sqrt(1 - (x^2)) - 1)"
  back:    "x^2 * (2.7 * x - 1.7)"
  elastic: "-(2^(10 * (x - 1)) * math.sin((x - 1.075) * (math.pi * 2) / 0.3))"
}

make_function = (s, e) ->
  loadstring "return function(x) " .. (str\gsub "%$e", e) .. " end"

generate_ease = (name, f) ->
  funcs[name .. "in"]  = make_function "return $e", f
  funcs[name .. "out"] = make_function [[
    x = 1 - x
    return 1 - ($e)
  ]]

  funcs[name .. "in_out"] = make_function([[
    x = x * 2
    if x < 1 then
      return 0.5 * ($e)
    else
      x = 2 - x
      return 0.5 * (1 - ($e)) + 0.5
    end
  ]], f)

easing.get = (name, value) =>
  unless funcs[name]
    name ..= "in"
  assert funcs[name] == nil, "Why would you use a non-existing function?"

  funcs[name](value)

easing.add = (name, f) =>
  assert funcs[name] ~= nil, "Why would you add an existing function?"

  generate_ease name, f

for k, v in pairs defaults
  generate_ease k, v

setmetatable easy, {
  __call: easing.get
}

easing

easing   = {}
funcs    = {}
defaults = {
  quad:    "x^2"
  cubic:   "x^3"
  quart:   "x^4"
  quint:   "x^5"
  expo:    "2^(10 * (x - 1))"
  sine:    "-math.cos(x * (math.pi * 0.5)) + 1"
  circ:    "-(math.sqrt(1 - (x^2)) - 1)"
  back:    "x^2 * (2.7 * x - 1.7)"
  elastic: "-(2^(10 * (x - 1)) * math.sin((x - 1.075) * (math.pi * 2) / 0.3))"
}

make_function = (s, e) ->
  loadstring "return function(x) " .. (str\gsub "%$e", e) .. " end"

generate_ease = (name, f) ->
  funcs[name .. "in"]  = make_function "return $e", f
  funcs[name .. "out"] = make_function [[
    x = 1 - x
    return 1 - ($e)
  ]]

  funcs[name .. "in_out"] = make_function([[
    x = x * 2
    if x < 1 then
      return 0.5 * ($e)
    else
      x = 2 - x
      return 0.5 * (1 - ($e)) + 0.5
    end
  ]], f)

easing.get = (name, value) =>
  unless funcs[name]
    name ..= "in"
  assert funcs[name] == nil, "Why would you use a non-existing function?"

  funcs[name](value)

easing.add = (name, f) =>
  assert funcs[name] ~= nil, "Why would you add an existing function?"

  generate_ease name, f

for k, v in pairs default
  generate_ease k, v

setmetatable easy, {
  __call: easing.get
}

easing


Fix function again

Fix function again

Gdx     = sauce3.java.require "com.badlogic.gdx.Gdx"
Sauce3VM = sauce3.java.require "sauce3.Sauce3VM"

class
  new: (f, t = "internal") =>
    switch t
      when "internal"
        @file = Gdx.files\internal f
      when "local"
        @file = Sauce3VM.util\localfile f
      when "external"
        @file = Gdx.files\external f
      when "classpath"
        @file = Gdx.files\classpath f
      when "absolute"
        @file = Gdx.files\absolute f

  append: (v) =>
    @file\writeString v, true

  copy: (f) =>
    @file\copyTo f

  dir_create: =>
    @file\mkdirs!

  exists: =>
    @file\exists!

  get_dir_list: =>
    children = @file\list!
    paths    = {}

    for i = 0, children.length
      paths[i + 1] = children[i]\path!

    paths

  get_last_modified: =>
    @file\lastModified!

  get_size: =>
    @file\length!

  is_dir: =>
    @file\isDirectory!

  is_file: =>
    not @is_dir

  move: (p) =>
    @file\moveTo p

  read: =>
    @file\readString!

  remove: =>
    @file\deleteDirectory!

  write: (v) =>
    @file\writeString v

Gdx     = sauce3.java.require "com.badlogic.gdx.Gdx"
Sauce3VM = sauce3.java.require "sauce3.Sauce3VM"

class
  new: (f, t = "internal") =>
    switch t
      when "internal"
        @file = Gdx.files\internal f
      when "local"
        @file = Sauce3VM.util\localFile f
      when "external"
        @file = Gdx.files\external f
      when "classpath"
        @file = Gdx.files\classpath f
      when "absolute"
        @file = Gdx.files\absolute f

  append: (v) =>
    @file\writeString v, true

  copy: (f) =>
    @file\copyTo f

  dir_create: =>
    @file\mkdirs!

  exists: =>
    @file\exists!

  get_dir_list: =>
    children = @file\list!
    paths    = {}

    for i = 0, children.length
      paths[i + 1] = children[i]\path!

    paths

  get_last_modified: =>
    @file\lastModified!

  get_size: =>
    @file\length!

  is_dir: =>
    @file\isDirectory!

  is_file: =>
    not @is_dir

  move: (p) =>
    @file\moveTo p

  read: =>
    @file\readString!

  remove: =>
    @file\deleteDirectory!

  write: (v) =>
    @file\writeString v


Refactor to using respond_to_form

Refactor to using respond_to_form

respond_to_form catches errors and formats them for the user, taking
into account if JSON has been requested.

lapis = require "lapis"
config = (require "lapis.config").get!
content = require "content"
logger = require "lapis.logging"
csrf = require "lapis.csrf"
submit = require "submit_application"

import after_dispatch from require "lapis.nginx.context"
import to_json from require "lapis.util"
import capture_errors, assert_error, yield_error, respond_to from require "lapis.application"
import json_requested from require "utils"

capture_form_errors = (fn) ->
    capture_errors {
        on_error: (err, trace) =>
            err or= @errors
            if @json
                resp = if @m and @m.form and @m.form.responses and @m.form.responses.default
                    @m.form.responses.default
                else
                    '
A server error has occurred, and the error text could not be fetched.
'

                print to_json err
                { status: 500, json: { response: resp, errors: { } } }
            else
                @app.handle_error self, err, trace, 500

        fn
    }

class CSWeek extends lapis.Application
    layout: require "views.layout"
    error_page: require "views.error"

    handle_error: (err, trace, code = 500) =>
        r = @app.Request @app, @req, @res

        with r
            .m, errpg = content\get "error"
            if errpg then
                .m = { }
                .m[code] = "Could not load the error page (code #{code}): #{errpg}
When handling #{err}
#{trace}"

            .m[code] = .m[code] or .m.default or { }
            .page_id = "error"
            .status = code
            .error = err
            .traceback = trace
            .m.title = .m[code].title

            \write {
                status: code
                content_type: "text/html"
                render: "error"
            }
            \render!
            logger.request r
        r

    try_render: (template, context) =>
        context = context or self
        with context
            .nav = assert_error content\get "nav"
            .footer = assert_error content\get "footer"
        render: template

    respond_to_form: (err, model, page_id, template) =>
        resp = { }

        status = if not err
                200
            elseif err[1] == 'internal_error'
                500
            elseif err[1] == 'duplicate_application'
                409
            elseif err[1] == 'too_frequent'
                403
            elseif err[1] == 'file_too_big'
                419
            else
                400

        resp.response = model.form.responses[status] or model.form.responses.default if model.form.responses

        if status == 400
            resp.errors = { }
            for e in *err
                table.insert resp.errors, model.form.validation_errors[e] or 'unknown error'
        if @json
            { :status, json: resp }
        else
            @resp = resp
            @page_id = page_id
            ret = @app\try_render template, self
            ret.status = status
            ret

    @before_filter =>
        if #[n for n in *{'production-perftest', 'development-perftest'} when n == config._name] > 0
            after_dispatch ->
                print to_json(ngx.ctx.performance)

    [home: "/"]: =>
        @page_id = "home"
        @m = assert_error content\get "home"
        @lecturers = assert_error content\get "lecturers"
        @app\try_render "home", self

    [about: "/o-nedelji"]: =>
        @page_id = "about"
        @m = assert_error content\get "about"
        @app\try_render "about", self

    [lecturers: "/predavaci"]: =>
        @page_id = "lecturers"
        @m = assert_error content\get "lecturers_page"
        @lecturers = assert_error content\get "lecturers"
        @app\try_render "lecturers", self

    [lecturer: "/predavaci/:name"]: =>
        @lecturers_page = assert_error content\get "lecturers_page"
        @page_id = "lecturer"
        lecturers = assert_error content\get "lecturers"
        results = [l for l in *lecturers when l.id == @params.name]
        if #results > 1
            yield_error "Multiple lecturer id's found for id `#{@params.name}`"
        else if #results == 0
            @app.handle_error self, "No such lecturer: `#{@params.name}`", nil, 404
            return

        @m = results[1]
        @app\try_render "lecturer", self

    [apply: "/prijava"]: respond_to {
        GET: =>
            @page_id = "apply"
            @m = assert_error content\get "apply"
            @csrf_token = csrf.generate_token @
            @app\try_render "apply", self

        POST: capture_form_errors =>
                @json = json_requested @
                model = assert_error content\get "apply"
                @m = model

                this = self
                succ, ret, err = pcall ->
                    submit\submit this.res.req.params_post, model, (...) ->
                        this\build_url ...

                yield_error ret if not succ
                @app.respond_to_form self, err, model, 'apply', 'apply-result'
    }

    [apply_upload: "/prijava/upload/*"]: respond_to {
        GET: =>
            @page_id = "apply"
            @m = assert_error content\get "apply-upload"
            @a = assert_error content\get "apply"
            @csrf_token = csrf.generate_token @
            if @application = submit\get_application @params.splat
                @app\try_render "apply-upload", self
            else
                redirect_to: '/prijava'

        POST: capture_form_errors =>
            @json = json_requested @
            model = assert_error content\get 'apply-upload'
            apply_model = assert_error content\get 'apply'
            @m = model

            this = self
            succ, ret, err = pcall ->
                submit\upload this.res.req.params_post, @params.splat, model, apply_model.tasks

            yield_error ret if not succ

            @app.respond_to_form self, err, model, 'apply', 'apply-result'
    }

    handle_404: =>
        @app.handle_error self, "Route `#{self.req.parsed_url.path or 'unknown'}` not found", nil, 404

lapis = require "lapis"
config = (require "lapis.config").get!
content = require "content"
logger = require "lapis.logging"
csrf = require "lapis.csrf"
submit = require "submit_application"

import after_dispatch from require "lapis.nginx.context"
import to_json from require "lapis.util"
import capture_errors, assert_error, yield_error, respond_to from require "lapis.application"
import json_requested from require "utils"

capture_form_errors = (fn) ->
    capture_errors {
        on_error: (err, trace) =>
            err or= @errors
            if @json
                resp = if @m and @m.form and @m.form.responses and @m.form.responses.default
                    @m.form.responses.default
                else
                    '
A server error has occurred, and the error text could not be fetched.
'

                print to_json err
                { status: 500, json: { response: resp, errors: { } } }
            else
                @app.handle_error self, err, trace, 500

        fn
    }

class CSWeek extends lapis.Application
    layout: require "views.layout"
    error_page: require "views.error"

    handle_error: (err, trace, code = 500) =>
        r = @app.Request @app, @req, @res

        with r
            .m, errpg = content\get "error"
            if errpg then
                .m = { }
                .m[code] = "Could not load the error page (code #{code}): #{errpg}
When handling #{err}
#{trace}"

            .m[code] = .m[code] or .m.default or { }
            .page_id = "error"
            .status = code
            .error = err
            .traceback = trace
            .m.title = .m[code].title

            \write {
                status: code
                content_type: "text/html"
                render: "error"
            }
            \render!
            logger.request r
        r

    try_render: (template, context) =>
        context = context or self
        with context
            .nav = assert_error content\get "nav"
            .footer = assert_error content\get "footer"
        render: template

    @before_filter =>
        if #[n for n in *{'production-perftest', 'development-perftest'} when n == config._name] > 0
            after_dispatch ->
                print to_json(ngx.ctx.performance)

    [home: "/"]: =>
        @page_id = "home"
        @m = assert_error content\get "home"
        @lecturers = assert_error content\get "lecturers"
        @app\try_render "home", self

    [about: "/o-nedelji"]: =>
        @page_id = "about"
        @m = assert_error content\get "about"
        @app\try_render "about", self

    [lecturers: "/predavaci"]: =>
        @page_id = "lecturers"
        @m = assert_error content\get "lecturers_page"
        @lecturers = assert_error content\get "lecturers"
        @app\try_render "lecturers", self

    [lecturer: "/predavaci/:name"]: =>
        @lecturers_page = assert_error content\get "lecturers_page"
        @page_id = "lecturer"
        lecturers = assert_error content\get "lecturers"
        results = [l for l in *lecturers when l.id == @params.name]
        if #results > 1
            yield_error "Multiple lecturer id's found for id `#{@params.name}`"
        else if #results == 0
            @app.handle_error self, "No such lecturer: `#{@params.name}`", nil, 404
            return

        @m = results[1]
        @app\try_render "lecturer", self

    [apply: "/prijava"]: respond_to {
        GET: =>
            @page_id = "apply"
            @m = assert_error content\get "apply"
            @csrf_token = csrf.generate_token @
            @app\try_render "apply", self

        POST: capture_form_errors =>
                @json = json_requested @
                model = assert_error content\get "apply"
                @m = model

                this = self
                succ, ret, err = pcall ->
                    submit\submit this.res.req.params_post, model, (...) ->
                        this\build_url ...

                yield_error ret if not succ

                resp = { }

                status = if ret
                        200
                    elseif err[1] == 'internal_error'
                        500
                    elseif err[1] == 'duplicate_application'
                        409
                    elseif err[1] == 'too_frequent'
                        403
                    else
                        400

                resp.response = model.form.responses[status] or model.form.responses.default
                if status == 400
                    resp.errors = { }
                    for e in *err
                        table.insert resp.errors, model.form.validation_errors[e] or 'unknown error'
                if @json
                    { :status, json: resp }
                else
                    @resp = resp
                    @page_id = 'apply'
                    ret = @app\try_render 'apply-result', self
                    ret.status = status
                    ret
    }

    [apply_upload: "/prijava/upload/*"]: respond_to {
        GET: =>
            @page_id = "apply"
            @m = assert_error content\get "apply-upload"
            @a = assert_error content\get "apply"
            @csrf_token = csrf.generate_token @
            if @application = submit\upload_form @params.splat
                @app\try_render "apply-upload", self
            else
                redirect_to: '/prijava'

        POST: capture_form_errors =>
            json = json_requested @
            model = assert_error content\get 'apply-upload'
            @m = model

            this = self
            succ, ret, err = pcall ->
                submit\upload this.res.req.params_post, model, (...) ->
                    this\build_url ...

            yield_error ret if not succ

            resp = { }

    }

    handle_404: =>
        @app.handle_error self, "Route `#{self.req.parsed_url.path or 'unknown'}` not found", nil, 404