<div xmlns="http://www.w3.org/1999/xhtml"
     xmlns:py="http://genshi.edgewall.org/" py:strip="">

  <!--! 
     
     WARNING: this file and the corresponding <py:def>s are going to be phased
              out soon. Please DON'T include macros.html in your own templates.

              See http://trac.edgewall.org/ticket/6374
  -->

  <!--!  Display the plural or singular form of a noun depending on a given
  -      number.
  -
  -      'cnt'       corresponding quantity
  -      'noun'
  -      'keepzero'  If True, 0 gets pluralized, otherwise nothing gets generated.
  -
  -      We take care to not insert any extra space.
  -->
  <py:def function="plural(cnt,noun,keepzero=False)"><py:if
      test="cnt != 0 or keepzero"
      >${cnt == 1 and ('1 '+noun) or '%d %ss' % (cnt, noun)}</py:if></py:def>

  <!--!  Display size of a content in human friendly format (think -H)
  -
  -      We take care to not insert any extra space.
  -->
  <py:def function="sizeinfo(size)"><span title="$size bytes">${
      pretty_size(size)
  }</span></py:def>

  <!--!  Display author information, eventually obfuscating the e-mail address
  -
  -      We take care to not insert any extra space.
  -->
  <py:def function="authorinfo(author, email_map=None)"><py:choose><py:when test="author"><py:with
    vars="author = show_email_addresses and email_map and '@' not in author and email_map[author] or author">${
      author and format_author(author) or 'anonymous'
  }</py:with></py:when><py:otherwise>anonymous</py:otherwise></py:choose></py:def>

  <!--!  Display a sequence of path components.
  -
  -      Each component is a link to the corresponding location in the browser.
  -->
  <py:def function="browser_path_links(path_links,rev=None)">
    <py:for each="idx, part in enumerate(path_links)"><py:with 
        vars="first = idx == 0; last = idx == len(path_links) - 1"><a 
          class="${classes('pathentry', first=first)}"
          title="${first and 'Go to root directory' or 'View ' + part.name}"
          href="$part.href">$part.name</a><py:if 
        test="not last"><span class="pathentry sep">/</span></py:if></py:with></py:for>
    <py:if test="rev"><span class="pathentry sep">@</span>
      <a class="pathentry" href="${href.changeset(rev)}" title="View changeset $rev">$rev</a>
    </py:if>
    <br style="clear: both" />
  </py:def>

  <!--! Add Previous/Up/Next navigation links
  -
  -     `label` the label to use after the Previous/Next words
  -     `uplabel` the label to use for the Up link
  -
  -     Assume the 'chrome' datastructure to be available from the context.
  -->
  <ul py:def="prevnext_nav(label, uplabel=None)" py:with="links = chrome.links"
      py:if="'up' in chrome.links or
             'prev' in chrome.links or
             'next' in chrome.links">
    <li class="first" py:choose="">
      &larr; <a py:when="'prev' in links" py:with="link = links.prev[0]"
                class="prev" href="${link.href}"
                title="${link.title}">Previous $label</a>
      <span py:otherwise="" class="missing">Previous $label</span>
    </li>
    <li py:if="uplabel and 'up' in links">
      <a py:with="link = links.up[0]" href="${link.href}"
         title="${link.title}">$uplabel</a>
    </li>
    <li class="last" py:choose="">
      <a py:when="'next' in links"  py:with="link = links.next[0]"
         class="next" href="${link.href}"
         title="${link.title}">Next $label</a>
      <span py:otherwise="" class="missing">Next $label</span> &rarr;
    </li>
  </ul>

  <!--! Add diff option fields (to be used inside a form)
  -
  -     `diff` the datastructure which contains diff options
  -
  -->
  <py:def function="diff_options_fields(diff)">
    <label for="style">View differences</label>
    <select id="style" name="style">
      <option selected="${diff.style == 'inline' or None}"
              value="inline">inline</option>
      <option selected="${diff.style == 'sidebyside' or None}"
              value="sidebyside">side by side</option>
    </select>
    <div class="field">
      Show <input type="text" name="contextlines" id="contextlines" size="2"
                  maxlength="3" value="${diff.options.contextlines &lt; 0 and 'all' or diff.options.contextlines}" />
      <label for="contextlines">lines around each change</label>
    </div>
    <fieldset id="ignore" py:with="options = diff.options">
      <legend>Ignore:</legend>
      <div class="field">
        <input type="checkbox" id="ignoreblanklines" name="ignoreblanklines"
               checked="${options.ignoreblanklines or None}" />
        <label for="ignoreblanklines">Blank lines</label>
      </div>
      <div class="field">
        <input type="checkbox" id="ignorecase" name="ignorecase"
               checked="${options.ignorecase or None}" />
        <label for="ignorecase">Case changes</label>
      </div>
      <div class="field">
        <input type="checkbox" id="ignorewhitespace" name="ignorewhitespace"
               checked="${options.ignorewhitespace or None}" />
        <label for="ignorewhitespace">White space changes</label>
      </div>
    </fieldset>
    <div class="buttons">
      <input type="submit" name="update" value="Update" />
    </div>
  </py:def>

  <!--! Display a div for visualizing a preview of a file content
  -
  -     'preview' is usually obtained from calling Mimeview.preview_data()
  -
  -->
  <py:def function="preview_file(preview)">
    ${preview.rendered}
    <py:choose>
      <p py:when="preview.size == 0">
        <strong>(The file is empty)</strong>
      </p>
      <p py:when="not preview.rendered">
        <strong>HTML preview not available</strong>,
        <py:choose>
          <py:when test="preview.max_file_size_reached">
            since the file size exceeds $preview.max_file_size bytes.
          </py:when>
          <py:otherwise>
            since no preview renderer could handle it.
          </py:otherwise>
        </py:choose>
        Try <a href="$preview.raw_href">downloading</a> the file instead.
      </p>
      <py:when test="'errors' in preview">
        <p>
          <strong>HTML preview not available</strong>.
          To view, <a href="$preview.raw_href">download</a> the file.
        </p>
        <p>${plural(len(preview.errors), 'issue')} found:</p>
        <div py:for="renderer, err in preview.errors" class="system-message">
          <strong>${renderer.__class__.__name__}</strong>
          <pre>$err</pre>
        </div>
      </py:when>
    </py:choose>
  </py:def>

  <!--! Display a list of attachments
  -
  -     'alist' is an AttachmentList object (see attachment.py)
  -     'compact' is used to switch between a compact list of attachments (e.g. as used for
  -       the wiki view) and an expanded list (e.g. as used for the ticket view)
  -->
  <py:def function="list_of_attachments(alist, compact=False, add_button_title=None)">
    <py:def function="show_one_attachment(attachment)">
      <a href="${url_of(attachment.resource)}" title="View attachment">$attachment.filename</a>
      <a href="${url_of(attachment.resource, format='raw')}"
         title="Download" class="trac-rawlink"><img src="${chrome.htdocs_location}download.png" alt="Download"/></a>
      (${sizeinfo(attachment.size)}) - added by <em>${authorinfo(attachment.author)}</em>
      ${dateinfo(attachment.date)} ago.
    </py:def>
    <py:if test="alist.attachments or alist.can_create">
      <py:choose test="">
        <py:when test="compact and alist.attachments">
          <h3>Attachments</h3>
          <ul>
            <py:for each="attachment in alist.attachments">
              <li>
                ${show_one_attachment(attachment)}
                <q py:if="compact and attachment.description">${wiki_to_oneliner(context, attachment.description)}</q>
              </li>
            </py:for>
          </ul>
        </py:when>
        <py:when test="not compact">
          <h2>Attachments</h2>
          <div py:if="alist.attachments or alist.can_create" id="attachments">
            <dl py:if="alist.attachments" class="attachments">
              <py:for each="attachment in alist.attachments">
                <dt>${show_one_attachment(attachment)}</dt>
                <dd py:if="attachment.description">
                  ${wiki_to_oneliner(context, attachment.description)}
                </dd>
              </py:for>
            </dl>
            ${attach_file_form(alist, add_button_title)}
          </div>
        </py:when>
      </py:choose>
    </py:if>
  </py:def>

  <!--! Conditionally render an ''Attach File'' button
  -
  -     'alist' is an AttachmentList object (see attachment.py)
  -->
  <py:def function="attach_file_form(alist, add_button_title=None)">
    <form py:if="alist.can_create" method="get" action="${alist.attach_href}" id="attachfile">
      <div>
        <input type="hidden" name="action" value="new" />
        <input type="submit" name="attachfilebutton" value="${add_button_title or _('Attach file')}" />
      </div>
    </form>
  </py:def>

  <!--!  Display a generic "progress bar", for use in roadmap and milestone.
  -
  -      `stats` argument is a TicketGroupStats instance
  -       `interval_hrefs` are links to a query for each of the stats.intervals
  -->
  <py:def function="progress_bar(stats, interval_hrefs, percent=None, legend=True, style=None, stats_href=None)">
    <table class="progress" style="$style">
      <tr>
        <td py:for="idx, interval in enumerate(stats.intervals)" py:choose=""
            class="$interval.css_class" style="${interval.percent and 'width: %d%%' % interval.percent or 'display: none'}">
          <a py:when="interval_hrefs" href="${interval_hrefs[idx]}"
            title="${interval.count} of ${stats.count} ${stats.unit}${
            stats.count != 1 and 's' or ''} ${interval.title}"></a>
          <a py:otherwise=""
            title="${interval.count} of ${stats.count} ${stats.unit}${
            stats.count != 1 and 's' or ''} ${interval.title}"></a>
        </td>
      </tr>
    </table>
    <p class="percent">${percent is None and '%d%%' % stats.done_percent or percent}</p>
    <dl py:if="legend">
      <py:for each="idx, interval in enumerate(stats.intervals)">
        <dt py:choose="">
          <a py:when="interval_hrefs" href="${interval_hrefs[idx]}">${interval.title.capitalize()} ${stats.unit}s:</a>
          <a py:otherwise="">${interval.title.capitalize()} ${stats.unit}s:</a>
        </dt>
        <dd py:choose="">
          <a py:when="interval_hrefs" href="${interval_hrefs[idx]}">${interval.count}</a>
          <a py:otherwise="">${interval.count}</a>
        </dd>
      </py:for>
      <dt>/ <a href="${stats_href}">Total ${stats.unit}s:</a></dt>
      <dd><a href="${stats_href}">${sum([x.count for x in stats.intervals], 0)}</a></dd>
    </dl>
</py:def>

</div>
