{% macro automodule(name) %}
{% set m = autodoc.automodule(name) %}
{% if m.docs %}<h2>Introduction</h2><br>{{ m.docs|markdown }}<br><br>{% endif %}
{% for obj in m.members %}
	{% if obj.type=="function" %}
        {{ render_function(obj, name) }}
	{% elif obj.type=="class" %}
        {{ render_class(obj) }}
	{% endif %}
{% endfor %}
{% endmacro %}

{% macro render_class(obj) %}
	<h3 style="font-weight: normal;">Class <b>{{ obj.name }}</b></h3>
    {% if obj.bases %}
    <p style="padding-left: 30px;"><i>Inherits from {{ ", ".join(obj.bases) }}</i></h4>
    {% endif %}
    <div class="docs-attr-desc">{{ obj.docs|markdown }}</div>
    <div style="padding-left: 30px;">
        {% for func in obj.members %}
        {{ render_function(func) }}
        {% endfor %}
    </div>
    <hr>
{% endmacro %}

{% macro render_function(obj, module_name) %}
    {% set full_name = ((module_name + "." if module_name else "" ) + obj.name).replace(".__init__", "") %}
    {% if obj.whitelisted %}<p><span class="label label-info">Public API</span>
        <br><code>/api/method/{{ full_name }}</code>
    </p>{% endif %}
	<p class="docs-attr-name">
        <a name="{{ full_name }}" href="#{{ full_name }}" class="text-muted small">
            <i class="icon-link small" style="color: #ccc;"></i></a>
		{{ module_name.replace(".__init__", "") + "." if module_name else "" }}<b>{{ obj.name }}</b>
        <i class="text-muted">({{ print_args(obj.args) if obj.args else "" }})</i>
    </p>
	<div class="docs-attr-desc">{{ obj.docs|markdown }}</div>
	<br>
{% endmacro %}

{% macro print_args(args) -%}
{% for arg in args[0] -%}
	{%- set default_idx = args[3]|len - args[0]|len + (loop.index - 1) if args[3] else -1 -%}
	{{ arg }}{% if default_idx >= 0 %}={{ args[3][default_idx] }}{% endif %}{% if not loop.last %}, {% endif %}
	{%- endfor %}
{%- endmacro %}

{% macro version(name) %}
<a class="btn btn-default btn-sm" disabled style="margin-bottom: 10px;">
	Version {{ autodoc.get_version(name) }}</a>
{% endmacro %}

{% macro source_link(app, file_path, tree=False) %}
	<a class="btn btn-default btn-sm" href="{{ app.source_link }}/{{ "tree" if tree else "blob" }}/{{ app.branch }}/{{ file_path }}"
		target="_blank" style="margin-left: 10px; margin-bottom: 10px;"><i class="octicon octicon-mark-github"></i> Source</a>
{% endmacro %}

{% macro doctype_link(app, doctype) %}
{% set module = frappe.db.get_value("DocType", doctype, "module") %}
{% if doctype and module %}
<a href="{{ app.docs_base_url }}/{{ app.docs_version }}/models/{{
		scrub(module) }}/{{ scrub(doctype) }}">{{ doctype }}</a>
{% endif %}
{% endmacro %}
