{% macro genurl(string, force=False) %}
  {% if (force or
        ( string.startswith('/') and not string.startswith('//') )) %}
    {% set url = '/'.join([SITEURL.rstrip('/'),string.lstrip('/')]) %}
    {{- url -}}
  {% else %}
    {% set url = '/'.join([SITEURL, string]) %}
    {{- url -}}
  {% endif %}
{% endmacro %}

{% macro newtab(string, force=False) %}
  {% if ( string.startswith('http://') or
          string.startswith('https://')) %}
    target="_blank"
  {% endif %}
{% endmacro %}

{% macro article_panel_list(parent='', id='', title_left='', title_right='', entry_list=[], show_date=False, title_left_link='') %}
  {% if not title_left_link %}
    {% set title_left_link = "#%s" % id %}
  {% endif %}

  <div class="panel panel-primary">
    <div class="panel-heading panel-title">
	<a id="{{ id }}" href="{{ genurl( title_left_link ) }}">
		{{- title_left -}}
	</a>
    </div>

    <div class="panel-body hidden"></div>

    <ul class="list-group">
      <div class="panel-collapse collapse in" id="{{ id }}collapse">
        {% for article in entry_list %}
        <a href="{{ genurl( article.url, force=True ) }}">
          <li class="list-group-item">
            {% if show_date %}
              {{- article.date|strftime("%m-%d") -}}
              <b> · </b>
            {% endif %}
            {{- article.title -}}
          </li>
        </a>
        {% endfor %}
      </div>
    </ul>

  </div>
{% endmacro %}

{% macro pagetitle(name) %}
  {% if name and name != '' %}
    <title>{{ name }} · {{ SITENAME }}</title>
  {% else %}
    <title>{{ SITENAME }}</title>
  {% endif %}
{% endmacro %}

{% macro pageheader(name, link) %}
  {% if name and name != '' %}
    <div class="page-header">
      {% if link %}
        <h1><a href="{{ genurl( link ) }}">{{ name }}</a></h1>
      {% else %}
        <h1>{{ name }}</h1>
      {% endif %}
    </div>
  {% endif %}
{% endmacro %}

{% macro navbar(link) %}

	<nav id="navbar" role="navigation">
		<div class="navbar-header">
			<div class="navbar-img">
				<a class="navbar-brand" href="{{ genurl('' ) }}">
						  <img src="{{ SITEURL }}/images/logo.png" alt="{{ SITENAME }}" id="logo" />
				</a>
			</div>
			{%if SHOW_PAGE_TITLE %}
			<div class="navbar-text">
				<h1 id="page-title">
					<a href="{{ genurl( SITEURL ) }}">
						{{ SITENAME }}
					</a>
				</h1>
			</div>
			{% endif %}
		</div>

    <input type="checkbox" id="menu-toggle" name="menu-toggle" checked="false"/>
    <label for="menu-toggle">hide/show menu</label> 

		<ul id="menuitem-list">
			{% for p in PAGES %}
				{% if p.url is string %}
					{% if p.title != 'Search' %}
						<li class="dropdown">
							<a class="menulink head {% if p.url == page %}active{% endif %}" href="{{ genurl( p.url ) }}" {{ newtab(p.url) }}>{{ p.title }}</a>
						</li>
					{% endif %}
				{% elif p.url is iterable %}
					<li class="dropdown">
						<!-- FIXME Selecting a submenu link does not set the head link active, need a custom jinja test for that //-->
						<a href="#" class="dropdown-toggle menulink head {% if p.url == page %}active{% endif %}" data-toggle="dropdown">
							{{- title }}
						</a>
						<ul class="dropdown-content" role="menu">
							{% for title,item_url in url %}
								<li>
									<a class="menulink" href="{{ genurl( item_url ) }}" {{ newtab(item_url) }}>
										{{ title }}
									</a>
								</li>
							{% endfor %}
						</ul>
					</li>
				{% endif %}
			{% endfor %}
		</ul>
	</nav>
{% endmacro %}

{% macro footer() %}
  <nav id="footer" class="navbar navbar-default">
    <div class="container">
        <p id="footer-text" class="text-center">
		Built with Pelican 
        </p>
      </div>
    </div>
  </nav>
{% endmacro %}

{% macro pagination() %}
  {% if DEFAULT_PAGINATION %}
    <ul class="pagination">

      {% if articles_page.has_previous() %}
        <li>
          {% if articles_page.previous_page_number() == 1 %}
            <a href="{{ genurl( page_name + '.html', force=True ) }}">&laquo;</a>
          {% else %}
            <a href="{{ genurl( page_name + articles_page.previous_page_number()|string + '.html' ) }}">&laquo;</a>
          {% endif %}
        </li>
      {% else %}
        <li class="disabled">
          <a>&laquo;</a>
        </li>
      {% endif %}

      {% for num in articles_paginator.page_range %}
      {% if articles_page.number != num %}
        <li>
          {% if num == 1 %}
            {% set page_link = page_name+'.html' %}
          {% else %}
            {% set page_link = page_name+num|string+'.html' %}
          {% endif %}
          <a href="{{ genurl( page_link, force=True ) }}">
            {{- num }}<span class="sr-only">(current)</span></a>
        </li>
      {% else %}
        <li class="active">
          <a>{{- num }}<span class="sr-only">(current)</span></a>
        </li>
      {% endif %}
      {% endfor %}

      {% if articles_page.has_next() %}
        <li>
          <a href="{{ genurl( page_name + articles_page.next_page_number()|string + '.html' ) }}">&raquo;</a>
        </li>
      {% else %}
        <li class="disabled">
          <a>&raquo;</a>
        </li>
      {% endif %}

    </ul>
  {% endif %}
{% endmacro %}

{% macro article_info(article) %}
  <span class="article_info">
    {% if article.tags %}
      <b> · </b>
      {% for tag in article.tags %}
        <a class="label label-primary"
          href="{{ genurl( '/tag/' + tag|string + '.html' ) }}">
          {{- tag -}}
        </a>
      {% endfor %}
    {% endif %}
    <!--<span class="glyphicon glyphicon-calendar"></span>-->
    {% if article.locale_date %}
      {% set year = article.date|strftime('%Y') %}
      {% set month = article.date|strftime('%m') %}
      {% set day = article.date|strftime('%d') %}
      <a href="{{ genurl( '/archives.html#Year' + year ) }}">{{ year }}</a>
      {{- '-' -}}
      <a href="{{ genurl( '/archives.html#Year' + year + 'Month' + month ) }}">{{ month }}</a>
      {{- '-' -}}
      <a href="{{ genurl( '/archives.html#Year' + year + 'Month' + month ) }}">{{ day }}</a>

    {% endif %}
    <!--<span class="glyphicon glyphicon-user"></span>-->
  </span>
{% endmacro %}

{% macro taglist(list,prefix='',suffix='') %}
  {% for tag, articles in list|sort %}
    <a class="label label-primary"
      href="{{prefix}}{{tag}}{{suffix}}">
      {{- tag }} {{ articles|count }}</a>
  {% endfor %}
{% endmacro %}
