{# vim: set ts=2 et sts=2 sw=2: #}
{% extends "wiki/base.html" %}
{% block title %}{{ page_title(document.title) }}{% endblock %}
{% set classes = 'document' %}
{% block bodyclass %}document{% endblock %}
{% if document.parent %}
  {# If there is a parent doc, use it for translating. #}
  {% set translate_url = url('wiki.select_locale', document_path=document.parent.full_path) %}
{% else %}
  {% set translate_url = url('wiki.select_locale', document_path=document.full_path, locale=document.locale) %}
{% endif %}
{% if fallback_reason == 'no_translation' %}
  {% set help_link = url('wiki.translate', document_path=document.full_path, locale=document.locale)|urlparams(tolocale=request.locale) %}
{% elif fallback_reason == 'translation_not_approved' %}
  {% set help_link = url('wiki.translate', document_path=document.parent.full_path, locale=document.parent.locale)|urlparams(tolocale=request.locale) %}
{% endif %}

{% block extrahead %}
  <link rel="alternate" type="application/json" 
    href="{{ url('wiki.json_slug', document.full_path) }}">
{% endblock %}

{% block content %}

<!-- top toolbar -->
<section id="nav-toolbar"><div class="wrap">
  <!-- right floated navigation -->
  <nav id="tool-menus" role="navigation">
    <ul id="tools">
      <li class="menu">
        <a href="#page-tools" class="toggle">{{ _('This page') }}</a>
        <ul id="page-tools" class="sub-menu">
          <li class="page-print"> <a href="#" onclick="return window.print();"  title="{{ _('Print page') }}">{{ _('Print page') }}</a></li>
          {% if not document.is_template %}
            <li><a href="{{ url('wiki.new_document') }}?parent={{ document.id }}">{{ _('New sub-page') }}</a></li>
          {% endif %}
        </ul>
      </li>
      <li class="menu">
        <a href="#" class="toggle">{{ _('Languages') }}</a>
        <ul>
          {% for translation in document.other_translations %}
            <li><a rel="internal" href="{{ url('wiki.document', translation.full_path, locale=translation.locale) }}" title="{{ translation.title }}">{{ translation.language }}</a></li>
          {% endfor %}
          
          {% if document.is_localizable %}
            <li><a href="{{ translate_url }}">{{ _('Add translation') }}</a></li>
          {% endif %}
        </ul>
      </li>
    </ul>
  </nav>
  
  <!-- left crumb navigation -->
  <nav class="crumbs" role="navigation">
    <ol>
      {% for doc in document.parents %}
        <li class="crumb"><a href="{{ url('wiki.document', doc.full_path) }}">{{ doc.title }}</a></li>
      {% endfor %}
      <li class="crumb">{{ document.title }}</li>
    </ol>
  </nav>
  
</div></section>



<section id="content">
  <div class="wrap">
    <div id="content-main" class="full">
      <article class="article" role="main" 
          data-current-revision="{{ document.current_revision.id }}"
          data-refresh-message="{{ _('Your changes were merged. However, something else has been edited, so this page will be refreshed to reflect the changes.') }}"
          data-cancel-edit-message="{{ _('Abort editing in progress? Your unsaved changes will be discarded.') }}">
      <header id="article-head">
        <div class="title">
            <h1 class="page-title">{{ document.title }}</h1>
            {% if redirected_from %}
            <div class="redirected-from" id="redirected-from">
              <p>{{ _('Redirected from <a href="{href}">{title}</a>')|fe(href=redirected_from.get_absolute_url()|urlparams(redirect='no'), title=redirected_from.title) }}</p>
            </div>
          {% endif %}
        </div>
        <ul id="page-buttons">
            <li class="page-history"><a href="{{ url('wiki.document_revisions', document.full_path) }}">{{_('History')}}</a></li>
            {% if request.user.is_authenticated() %}
              <li class="page-watch"><a href="{{ url('wiki.document_watch', document.full_path) }}">{{_('Watch')}}</a></li>
            {% endif %}
            {% if document.allows_revision_by(request.user) %}
              {% if fallback_reason %}
                {% set edit_label = _('Edit English')  %}
                <li class="page-edit"><a href="{{ help_link }}">{{ _('Translate') }}</a></li>
              {% else %}
                {% set edit_label = _('Edit')  %}
              {% endif %}
              <li class="page-edit"><a href="{{ url('wiki.edit_document', document.full_path, locale=document.locale) }}">{{ edit_label }}</a></li>
            {% endif %}
        </ul>

        {% if request.user.is_authenticated() %}
          {% if render_raw_fallback %}
            <div id="doc-render-raw-fallback" class="warning">
            {% trans url=document.get_absolute_url() %}
              This document is being rendered for the first time by the system.
              Please <a href="{{ url }}">reload this page</a> in a few minutes
              to see full content.
            {% endtrans %}
            </div>
          {% elif document.is_rendering_in_progress %}
            <div id="doc-rendering-in-progress" class="warning">
            {% trans url=document.get_absolute_url(), started_at=document.render_started_at %}
              An update to this document
              <abbr title="{{ started_at }}">is currently in progress</abbr>.
              If the content below seems stale or if something is missing,
              please <a href="{{ url }}">reload this page</a> in a few minutes.
            {% endtrans %}
            </div>
          {% elif document.is_rendering_scheduled %}
            <div id="doc-rendering-scheduled" class="warning">
            {% trans url=document.get_absolute_url(), scheduled_at=document.render_scheduled_at %}
              An update to this document 
              <abbr title="{{ scheduled_at }}">has been scheduled</abbr>.
              If the content below seems stale or if something is missing,
              please <a href="{{ url }}">reload this page</a> in a few minutes.
            {% endtrans %}
            </div>
          {% endif %}
        {% endif %}

        {% if kumascript_errors %}
          {% include 'wiki/includes/kumascript_errors.html' %}
        {% endif %}
       </header>

       {#
    {% if document.is_majorly_outdated() %}
      <div class="warning-box">
        {% trans url=document.parent.get_absolute_url(), title=document.parent.title %}
          <p>This article may be out of date.</p>
          <p>
            An important change has been made to the English article on which
            this is based. Until this page is updated, you might find this
            helpful: <a href="{{ url }}">{{ title }}</a>
          </p>
        {% endtrans %}
      </div>
    {% endif %}
    #}
    
      {# There's no .has() method on review_tags; need to iterate for now #}
      {% if document.current_revision %}
        {% for tag in document.current_revision.review_tags.all() %}
          {% if 'technical' == tag.name %}
            <div class="warning review-technical">{% trans %}
              <p>This article is in need of a technical review.</p>
            {% endtrans %}</div>
          {% elif 'editorial' == tag.name %}
            <div class="warning review-editorial">{% trans %}
              <p>This article is in need of an editorial review.</p>
            {% endtrans %}</div>
          {% endif %}
        {% endfor %}
      {% endif %}
      {% if fallback_reason %}
        <div id="doc-pending-fallback" class="warning">
          {% trans help_link=help_link, locale=settings.LOCALES[request.locale].native %}
            Our volunteers haven't translated this article into {{ locale }} yet.
            <a href="{{ help_link }}">Join us and help get the job done!</a>
          {% endtrans %}
        </div>
      {% endif %}

      {% set tags = document.tags.all() %}
      {% set attachments = document.attachments.all() %}

      <div id="wikiArticle" class="page-content boxed">
        {% if toc_html %}
        <div id="article-nav">
          <div class="page-toc">
            <h2>Table of Contents</h2>
            <ol>
              {{ toc_html|safe }}
            </ol>
          </div>
          <ul class="page-anchors">
            <li class="anchor-tags">
              {% if tags|length %}
                <a href="#page-tags">Tags</a>
              {% else %}
                <span title="{{ _('This document has no tags') }}">Tags</span>
              {% endif %}
            </li>
            <li class="anchor-files">
              {% if attachments|length %}
                <a href="#page-file">Files</a>
              {% else %}
                <span title="{{ _('This document has no files') }}">Files</span>
              {% endif %}
            </li>
          </ul>
        </div>
        {% endif %}
      {% if not fallback_reason %}
        {% if document.is_template %}
          <pre class="brush: js">{{ document_html }}</pre>
        {% else %}
          {{ document_html|safe }}
        {% endif %}
      {% elif fallback_reason == 'no_translation' %}
        {{ document_html|safe }}
      {% elif fallback_reason == 'translation_not_approved' %}
        {{ document.parent.html|safe }}
      {% else %}
        {{ _("This article doesn't have approved content yet.") }}
      {% endif %}
    </div>
    <section class="page-meta">
      
      {% if tags | length %}
      <section id="page-tags">
        <h2>{{ _('Tags') }} ({{ tags | length}})</h2>
        <div id="deki-page-tags">
          <ul class="tags">
            <li>
              {% for tag in tags %}
                <a class="text" href="{{url('wiki.tag', tag.name)}}">{{ tag.name }}</a>
              {% endfor %}
            </li>
          </ul>
        </div>
      </section>
      {% endif %}

      {% if attachments | length %}
        <section id="page-attachments">
          <h2>{{ _('Attachments') }} ({{ attachments | length}})</h2>
          <table cellpadding="0" cellspacing="0">
            <thead>
              <th>{{ _('File') }}</th>
              <th>{{ _('Size') }}</th>
              <th>{{ _('Date') }}</th>
              <th>{{ _('Attached by') }}</th>
              <!-- <th>&nbsp;</th>  actions will go here -->
            </thead>
            <tbody>
              {% for attachment in attachments %}
                <tr>
                  <td class="attachment-name-cell">
                    <a href="{{ attachment.get_file_url() }}" title="{{ attachment.get_file_url() }}" target="_blank">{{ attachment.title | safe }}</a>
                    {% if attachment.current_revision.description %}
                      <div class="attachment-description">{{ attachment.current_revision.description }}</div>
                    {% endif %}
                  </td>
                  <td>{{ attachment.current_revision.file.size }} bytes</td>
                  <td>{{ datetimeformat(attachment.current_revision.created, format='datetime') }}</td>
                  <td><a href="{{ url('devmo.views.profile_view', username=attachment.current_revision.creator) }}">{{ attachment.current_revision.creator }}</a></td>
                  <!--<td>&nbsp;</td> actions will go here -->
                </tr>
              {% endfor %}
            </tbody>
          </table>
        </section>
      {% endif %}


      <section id="doc-contributors">
        {% trans contributors=user_list(contributors) %}
          Contributors to this page: {{ contributors }}
        {% endtrans %}

        {% set current_revision = document.current_revision %}
        {% if current_revision.creator %}
          <br />
          {{ _('Last updated by') }}:  
          <a href="{{ url('devmo.views.profile_view', username=current_revision.creator) }}">{{ current_revision.creator }}</a>, 
          {{ datetimeformat(current_revision.created, format='datetime') }}
          {% if current_revision.reviewer %}
            <br />
            {{ _('Last reviewed by') }}:  
            <a href="{{ url('devmo.views.profile_view', username=current_revision.reviewer) }}">{{ current_revision.reviewer }}</a>, 
            {{ datetimeformat(current_revision.reviewed, format='datetime') }}
          {% endif %}
        {% endif %}
      </section>
    </section>
  </article>
  <form id="wiki-page-edit" class="editing" method="post" action="{{ url('wiki.edit_document', document_path=document.full_path) }}">
      <input type="hidden" name="form" id="form" value="rev" />
      <input type="hidden" name="content" id="content" value="" />
  </form>
  {#
  {% include 'wiki/includes/helpful_form.html' %}
  {% include 'wiki/includes/more_help.html' %}
  #}
    </div>
   </div>
    {% if document.allows_revision_by(request.user) and request.user.is_authenticated() %}
      <div class="edited-section-ui template">
        <a class="btn-save" href="#">{{ _('Save') }}</a>
        <a class="btn-cancel" href="#">{{ _('Cancel') }}</a>
        <div class="src"></div>
      </div>
      {# This form gets filled out and used by the inline editor when a
         conflict is detected. It needs to be server side in order to get supplied
         with a CSRF token. #}
      <form class="conflict-bouncer template" method="POST" action="">
        <input type="hidden" name="form" value="rev">
        <input type="hidden" name="current_rev" value="">
        <input type="hidden" name="content" value="">
      </form>
    {% endif %}
</section>
{% endblock %}

{% block side %}
  {% include 'wiki/includes/support_for_selectors.html' %}
{% endblock %}