<%
  require 'json'
  require 'ostruct'
  include ERB::Util
  archive_json = JSON.parse(File.open("../archive.json").read)
  recipe_json  = JSON.parse(File.open("../recipes.json").read)

  packages = (archive_json.keys & recipe_json.keys).sort.map do |pkgname|
    package = OpenStruct.new
    package.name = pkgname
    versions, deps, package.descr, package.pkgtype = archive_json[package.name]
    recipe = recipe_json[package.name]
    package.version = versions.join('.')
    package.url = "packages/#{package.name}-#{package.version}." + (package.pkgtype == "single" ? "el" : "tar")
    package.recipe_url = "https://github.com/milkypostman/melpa/blob/master/recipes/#{package.name}"
    package.source = 'unknown'
    if package.descr =~ /(.*?)(\s*-\*-.*?)?\s*\[source:\s*(\w+)\]\s*/
      package.descr, package.source = $1, $3
    end

    package.source_url =
      case package.source
      when 'github' then recipe['repo'].include?('/') ? "https://github.com/#{recipe['repo']}" : "https://gist.github.com/#{recipe['repo']}"
      when 'wiki'   then recipe.key?('files') ? nil : "http://www.emacswiki.org/emacs/#{package.name}.el"
      end
    package
  end
%><!DOCTYPE html>
<html lang="en-US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>MELPA</title>

  <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
  <link rel="stylesheet" href="styles/default.css" type="text/css" />
  <style type="text/css">
    body {
      padding-top: 60px;
    }
    h1 {
      color: #922793;
    }
    .dataTables_empty {
      background-color: #FCF8E3;
      color: #C09853;
    }
    pre code { /* Match highlight.js styles to bootstrap */
      padding: 0;
      background: transparent;
    }
    .navbar, .navbar .brand, .navbar a, .navbar .nav>li>a {
      color: #922793;
    }
    a[name] { /* Stop navbar from hiding anchors */
      padding-top: 60px;
      margin-top: -60px;
      display: inline-block; /* required for webkit browsers */
    }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
  <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/moment.min.js"></script>
  <script src="highlight.pack.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
  <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <a href="/" class="brand">MELPA</a>
        <ul class="nav">
          <li><a href="#packages">Packages</a></li>
          <li><a href="#installing">Installing</a></li>
          <li><a href="#known-issues">Known issues</a></li>
          <li><a href="#updating">Updating</a></li>
          <li><a href="#development">Development</a></li>
          <li><a href="https://github.com/milkypostman/melpa">Github</a></li>
          <li><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=UCKDWNVGK8MFA&lc=US&item_name=MELPA&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted">Donate</a></li>
        </ul>
      </div>
    </div>
  </div>
  <div class="container">
    <section class="page-header">
      <h1>MELPA <small>(Milkypostman’s Emacs Lisp Package Archive)</small></h1>
    </section>
    <div class="row">
      <div class="span8">
        <section class="hero-unit">
          <ul>
            <li><strong>Up-to-date packages built on our servers from upstream source</strong></li>
            <li><strong>Installable in any recent Emacs using "package.el"</strong> - no need to install svn/cvs/hg/bzr/git/darcs etc.</li>
            <li><strong>Curated</strong> - no obsolete, renamed, forked or randomly hacked packages</li>
            <li><strong>Comprehensive</strong> - more packages than any other archive</li>
            <li><strong>Automatic updates</strong> - new commits result in new packages</li>
            <li><strong>Extensible</strong> - contribute recipes via github, and we'll build the packages</li>
          </ul>
        </section>
      </div>
      <div class="span4">
        <div class="alert alert-success">
          <strong>Latest build:</strong> <em><script language="javascript">document.write(moment(new Date(<%= Time.now.to_i * 1000 %>)).fromNow());</script></em>
        </div>
        <a class="twitter-timeline" data-dnt="true" data-related="milkypostman,sanityinc" href="https://twitter.com/melpa_emacs" data-widget-id="311867756586864640">Tweets by @melpa_emacs</a>
      </div>
    </div>
    <section>
      <a name="packages"></a>
      <h2>Current List of <%=h packages.size %> Packages</h2>
      <table class="table table-bordered table-hover">
        <thead>
          <tr class="header">
            <th align="left">Package</th>
            <th align="left">Version</th>
            <th align="left">Description</th>
            <th align="left">Recipe</th>
            <th align="left">Source</th>
          </tr>
        </thead>
        <tbody>
          <% packages.each do |package| %>
            <tr>
              <td><a name="<%=h package.name %>"></a><a href="<%=h package.url %>"><%=h package.name %></a></td>
              <td><%=h package.version %></td>
              <td><%=h package.descr %></td>
              <td><a href="<%=h package.recipe_url %>">recipe</a></td>
              <td><% if package.source_url %>
                     <a href="<%=h package.source_url %>"><%=h package.source %></a>
                  <% else %>
                     <%=h package.source %>
                  <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </section>
    <section>
      <a name="installing"></a>
      <h2>Installing</h2>
      <p>To add the repository put this before the call to <code>package-initialize</code> in your <code>init.el</code> file.</p>
      <!-- <script src="https://gist.github.com/1679158.js"> </script> -->

      <pre><code>(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)</code></pre>

      <p>In Emacs &lt; 24, you'll also need to explicitly include the GNU
        ELPA archive, which provides important compatibility libraries like <code>cl-lib</code>:</p>
      <pre><code>(when (< emacs-major-version 24)
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))</code></pre>
      <p>
        Please read about <a href="#known-issues">known issues</a>
        below before attempting to install multiple packages at once.
      </p>
      <h3>Customizations</h3>
      <p>
        There is currently no way in <code>package.el</code> to
        exclude or include versions. So to remedy this there is
        a <code>melpa.el</code> package (available in MELPA) that will
        allow you to enable only certain packages or exclude certain
        packages. You can install the package manually by pasting this
        into your <code>*scratch*</code> buffer and evaluating it.
      </p>
      <pre><code>(progn
  (switch-to-buffer
    (url-retrieve-synchronously
      "https://raw.github.com/milkypostman/melpa/master/melpa.el"))
  (package-install-from-buffer  (package-buffer-info) 'single))</code></pre>
      <p>You can then customize two variables:</p>
      <dl>
        <dt><code>package-archive-enable-alist</code></dt>
        <dd>
          <p>
            Optional Alist of enabled packages used
            by <code>package-filter</code>. The format
            is <code>(ARCHIVE . PACKAGE ...)</code>,
            where <code>ARCHIVE</code> is a string matching an archive
            name in<code>package-archives</code>, <code>PACKAGE</code>
            is a symbol of a package in <code>ARCHIVE</code> to
            enable.
          </p>
          <p>
            If no <code>ARCHIVE</code> exists in the alist, all
            packages are enabled.
          </p>
        </dd>
        <dt><code>package-archive-exclude-alist</code></dt>
        <dd>
          <p>
            Alist of packages excluded by <code> package-filter
            </code>. The format is <code>(ARCHIVE . PACKAGE
            ...)</code>, where <code>ARCHIVE</code> is a string
            matching an archive name
            in</code>package-archives</code>, <code>PACKAGE</code> is
            a symbol of a package in that archive to exclude.</p>
          <p>
            Any specified package is excluded regardless of the value
            of <code>package-archive-enable-alist</code>
          </p>
        </dd>
      </dl>
    </section>
    <section>
      <a name="known-issues"></a>
      <h2>Known Issues</h2>
      <p>
        <strong>Note:</strong> <em>These fixes are included in
        the <code>melpa.el</code> package.</em>
      </p>
      <p>
        There is a small bug in Emacs24’s <code>package.el</code> such
        that the dependency order comes out backwards. The problem is
        patched by some <em>advice</em>.
      </p>

      <pre><code> (defadvice package-compute-transaction
  (before package-compute-transaction-reverse (package-list requirements) activate compile)
    "reverse the requirements"
    (setq requirements (reverse requirements))
    (print requirements))</code></pre>
    </section>
    <section>
      <a name="updating"></a>
      <h2>Updating Packages</h2>
      <p>
        <code>package.el</code> now includes a mechanism to upgrade
        packages. After running <code>package-list-packages</code>,
        type <em>U</em> (mark Upgradable packages) and then <em>x</em>
        (eXecute the installs and deletions). When it’s done
        installing all the packages it will ask if you want to delete
        the obsolete packages and so you can hit <em>y</em> (Yes).
      </p>
      <p>
        If you run into a problem installing or upgrading, you may
        need to go into your <code>~/.emacs.d/elpa/</code> directory
        and delete packages that are installed multiple times. This
        can happen when the install times out.
      </p>
      <a name="development"></a>
      <h2>Development</h2>
      <p><a href="https://github.com/milkypostman/melpa">https://github.com/milkypostman/melpa</a></p>
      <p>
        Contributions are welcome. Currently, the builder supports
        packages using git, subversion, mercurial, bzr, cvs, darcs and
        emacswiki.
      </p>
    </section>
  </div>
  <script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/jquery.dataTables.min.js" type="text/javascript"></script>
  <script language="javascript">
      $("table").dataTable({bPaginate: false, bLengthChange: false});
      $('div.dataTables_filter input').focus().attr("placeholder", "Enter filter terms");
  </script>
  <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-39278673-1']);
      _gaq.push(['_trackPageview']);
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>
</body>
</html>