// Generated by CoffeeScript 1.11.1
(function() {
  var description, inlineMarkup, ite, man, option, options, synopsis;

  inlineMarkup = function(s) {
    return s.replace(/\*\*([^*]+)\*\*/g, function(_, w) {
      return "\\f[B]" + w + "\\f[]";
    }).replace(/\*([^*]+)\*/g, function(_, w) {
      return "\\f[I]" + w + "\\f[]";
    }).replace(/`([^`]+)`/g, function(_, w) {
      return "\\f[B]" + w + "\\f[]";
    }).replace(/<([^>]+)\>/g, function(_, w) {
      return "\\f[I]" + w + "\\f[]";
    });
  };

  synopsis = function(readme) {
    var arg, s;
    return ["\\f[B]" + (readme.name.split('-')[0].trim()) + "\\f[]", "[\\f[I]option\\f[] …]"].concat((function() {
      var j, len, ref, results;
      ref = readme["arguments"];
      results = [];
      for (j = 0, len = ref.length; j < len; j++) {
        arg = ref[j];
        s = "\\f[I]" + arg.name + "\\f[]";
        if (arg.moreThanOnce) {
          s += " …";
        }
        if (arg.optional) {
          s = "[" + s + "]";
        }
        results.push(s);
      }
      return results;
    })()).join("\n");
  };

  description = function(readme) {
    return inlineMarkup(readme.description);
  };

  ite = function(i, t, e) {
    if (t == null) {
      t = i;
    }
    if (e == null) {
      e = "";
    }
    if (i != null) {
      return t;
    } else {
      return e;
    }
  };

  option = function(o) {
    var names;
    names = [ite(o.short, "\\f[B]\\-" + o.short + "\\f[]"), ite(o.long, "\\f[B]\\-\\-" + o.long + "\\f[]")].filter(function(s) {
      return s != null;
    }).join(", ");
    return ".TP\n.B " + names + (ite(o.argument, " \\f[I]" + o.argument + "\\f[]")) + "\n" + (inlineMarkup(o.description));
  };

  options = function(readme) {
    var d;
    return ((function() {
      var j, len, ref, results;
      ref = readme.options;
      results = [];
      for (j = 0, len = ref.length; j < len; j++) {
        d = ref[j];
        results.push(option(d));
      }
      return results;
    })()).join('\n');
  };

  module.exports = man = function(readme) {
    var name;
    name = readme.name.split(/\s*-\s*/)[0];
    return ".TH " + (name.toUpperCase()) + " " + readme.section + "\n.SH NAME\n.PP\n" + (readme.name.split('-').join(" \- ")) + "\n.SH SYNOPSIS\n.PP\n" + (synopsis(readme)) + "\n.SH DESCRIPTION\n.PP\n" + (description(readme)) + "\n.SH OPTIONS\n.PP\n" + (options(readme));
  };

}).call(this);
