<%! import os, re %>

<%def name="image(path, file, caption='', label='',width=100)">

    <%
        image_path = os.path.join(path, file)
        
        if not os.path.isabs(image_path):
            image_label, ext = os.path.splitext(image_path)
            image_path = os.path.join(base_path, image_path)
        else:
            image_label, ext = os.path.splitext(file)
        
        image_path = image_path.replace('\\','/')
        if not os.path.exists(image_path):
            image_path = '../image.jpg'
            
	if len(label)>0:
	    image_label = label
	else:
            image_label = image_label.replace('/','_').replace('\\','_')
            image_label = re.sub('_+', '_', image_label)

    %>
    
    \begin{figure}[H]
        \centering
        \includegraphics[width=${width}mm]{${image_path}}
        \caption{${caption}}
        \label{fig:${image_label}}
    \end{figure}

</%def>

<%def name="image2(path_list, file, caption='', label='')">

    <%
        image_path0 = os.path.join(path_list[0], file)
        
        if not os.path.isabs(image_path0):
            image_label, ext = os.path.splitext(image_path0)
            image_path0 = os.path.join(base_path, image_path0)
        else:
            image_label, ext = os.path.splitext(file)
        
        image_path0 = image_path0.replace('\\','/')
        if not os.path.exists(image_path0):
            image_path0 = '../image.jpg'
         

        image_path1 = os.path.join(path_list[1], file)
        
        if not os.path.isabs(image_path1):
            image_label, ext = os.path.splitext(image_path1)
            image_path1 = os.path.join(base_path, image_path1)
        else:
            image_label, ext = os.path.splitext(file)
        
        image_path1 = image_path1.replace('\\','/')
        if not os.path.exists(image_path1):
            image_path1 = '../image.jpg'
            
	if len(label)>0:
	    image_label = label
	else:
            image_label = image_label.replace('/','_').replace('\\','_')
            image_label = re.sub('_+', '_', image_label)
    %>
    
    \begin{figure}[H]
    \begin{subfigure}[t]{.5\textwidth}
        \centering
        \includegraphics[width=.9\textwidth]{${image_path0}}
        \caption{${map1}}	
    \end{subfigure}		
    ~
    \begin{subfigure}[t]{.5\textwidth}	
        \centering
        \includegraphics[width=.9\textwidth]{${image_path1}}
        \caption{${map2}}	
    \end{subfigure}		
    \caption{${caption}}
    \label{fig:${image_label}}
    \end{figure}

</%def>


<%def name="table(path, file, caption='', label='', small=False)">

    \begin{table}[H]
        \centering
         
        % if small:
		\small
        % endif
        		
        <%
            table_path = os.path.join(path, file)
            
            if not os.path.isabs(table_path):
                table_label, ext = os.path.splitext(table_path)
                table_path = os.path.join(base_path, table_path)
	    else:
	        table_label, ext = os.path.splitext(file)
	    
	    tabular = ''
	    if os.path.exists(table_path):
                f = open(table_path, 'r')
                tabular = f.read()
                f.close()
                
            if len(label)>0:
                table_label = label
            else:
                table_label = table_label.replace('/','_').replace('\\','_')
                table_label = re.sub('_+', '_', table_label)
        %>
    
        % if tabular:           	
        
			% if caption=='':
				
				${tabular}
				
				
			% else:
			    
				${fix_caption(tabular, caption)}
				

		    % endif
        % else:
        
        \caption{${caption}}
        \begin{tabular}{c}
            \includegraphics[width=50mm]{../table.jpg} \\
        \end{tabular}
        
        % endif
        
        \label{tab:${table_label}}
    \end{table}
    
</%def>

<%def name="longtable(path, file, caption='', label='')">

        <%
            table_path = os.path.join(path, file)
            
            if not os.path.isabs(table_path):
                table_label, ext = os.path.splitext(table_path)
                table_path = os.path.join(base_path, table_path)
	    else:
	        table_label, ext = os.path.splitext(file)
	    
	    tabular = ''
	    if os.path.exists(table_path):
                f = open(table_path, 'r')
                tabular = f.read()
                f.close()
                
            if len(label)>0:
                table_label = label
            else:
                table_label = table_label.replace('/','_').replace('\\','_')
                table_label = re.sub('_+', '_', table_label)
        %>

        % if tabular:
        
        ${tabular}	
        
        % else:
        
        \caption{${caption}}
        \begin{tabular}{c}
            \includegraphics[width=50mm]{../table.jpg} \\
        \end{tabular}
        
        % endif
    
</%def>

<%def name="table_info(info,runs)">
    \noindent\rule{\textwidth}{1pt}
    \begin{tabular}{cccc}
	    Run & Type & Configuration & Morphology \\ \hline
		% for run in runs:
            ${latex_escape(run) }& ${info[run]['type']}& ${info[run]['configuration']} & ${info[run]['morphology']}\\ \hline
		% endfor
    \end{tabular}
        

    
</%def>


<%def name="latex_escape(string)">

<%
    replacements = ('&', '\\$', '%', '_', '#', '\\{', '\\}');
    
    for s in replacements:
        string = re.sub(r'([^\\]?)'+s, r'\1\\'+s, string)
        
    replacements = {'^':'\\^{}', '~':'\\~{}', '|':'\\textbar ', '<':'\\textless ', '>':'\\textgreater '};
        
    for s, r in list(replacements.items()):
        string = string.replace(s, r)
            
    return string
%>
    
</%def>

<%def name="fix_caption(table,caption)">

<%

    string = table.replace('\caption{}','\caption{' + caption + '}')
            
    return string
%>
    
</%def>