# Module gallery_html

import os.path

from gallery import *

# kludgy html template fragments

# % pageTitle
XML_START_TEMPLATE = """<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="../gallery.css" rel="STYLESHEET" type="text/css" />
    <title>%s</title>
  </head> """

# % (gallery_title, intro)
INDEX_BODY_START_TEMPLATE = """ 
  <body>
    <div class="index_title">
      %s
    </div>
    <div class="index_instructons">
      <a href = ".."> Choose another picture gallery</a> &nbsp;
      Click on a picture to see it bigger.
    </div>
    <div class= "index_intro">
      %s
    </div>
"""

# for each pic, call index n
# % (n, name, breaks, title) 
INDEX_PIC_TEMPLATE_MULT_LINES = """
    <div class="dir">
      <table width="1">
        <tr><td colspan = "2">
        <a href = "%s.html">
           <img src ="%s_thumb.jpg"/>
        </a> 
        </td></tr>
        <tr>
          <td>%s</td>
          <td>
            %s
          </td></tr>
      </table>
    </div>
"""

# % (n, name, title) 
INDEX_PIC_TEMPLATE_1_LINE = """
    <div class="dir">
      <table width="1">
        <tr><td>
        <a href = "%s.html">
           <img src ="%s_thumb.jpg"/>
        </a> 
        </td></tr>
        <tr>
          <td>
            %s
          </td>
        </tr>
      </table>
    </div>
"""

# % (n, name) 
INDEX_PIC_TEMPLATE_NO_TITLE = """
    <div class="dir">
        <a href = "%s.html">
           <img src ="%s_thumb.jpg"/>
        </a> 
    </div>
"""

XML_END = """
  </body>
</html>
"""

# pic page
# After XML_START_TEMPLATE using  % title
# % (prev, big_link, next, next, orig_name, title, text)
PIC_BODY_TEMPLATE = """
  <body>
    <div class = "link_block">
      <a href = "%s.html">Previous Picture</a>
      %s
      <a href = "%s.html">Next Picture (or click pic)</a>
      <a href = "index.html">Gallery Index</a>
    </div>
    <div class = "main_pic">
      <a href = "%s.html">
        <img src = "%s_web.jpg"</img>
      </a>
    </div>
    <div class = "pic_title">
       %s
    </div>
    <div class = "pic_text">
      <p>
        %s
      </p>
    </div>
  </body>
</html>  
"""

# % name
BIG_LINK_TEMPLATE = """<a href = "%s.jpg">Full size</a>"""


def make_html(gal, lines_in_thumb_label, big_pics_linked):
    make_index(gal, lines_in_thumb_label)
    for n in range(len(gal.docs)):
        make_pic_html(gal, n, big_pics_linked)

def make_index(gal, n_lines):
    """Create index.html."""
    # for each pic, call index n
    tot = len(gal.docs)
    if n_lines == 0:
        parts = [INDEX_PIC_TEMPLATE_NO_TITLE % (this(n, tot), pd.orig_name)
                          for (n, pd) in enumerate(gal.docs)]
    elif n_lines == 1:
        parts = [INDEX_PIC_TEMPLATE_1_LINE % \
                 (this(n, tot), pd.orig_name, pd.get_title()) \
                       for (n, pd) in enumerate(gal.docs)]
    else:
        skip = "<br/>"*n_lines
        parts = [INDEX_PIC_TEMPLATE_MULT_LINES % \
             (this(n, tot), pd.orig_name, skip, pd.get_title()) \
             for (n, pd) in enumerate(gal.docs)]
    
    str = (XML_START_TEMPLATE % gal.title) + \
          (INDEX_BODY_START_TEMPLATE % (gal.title, add_para(gal.intro)))\
            + "".join(parts) + XML_END
          
    str2file(str, os.path.join(gal.gal_dir, "index.html"))


# in prev, this, next, convert from 0 based to 1 based system
def prev(n, tot):
    return str((n + tot - 1) % tot + 1)

def this(n, tot):
    return str(n + 1)

def next(n, tot):
    return str((n + 1) % tot + 1)

def make_pic_html(gal, n, big_pics_linked):
    " make html page that is one pic plus notes and navigation."
    tot = len(gal.docs)
    pd = gal.docs[n]    
    if big_pics_linked:
        big_link = BIG_LINK_TEMPLATE % pd.orig_name
    else:
            big_link = ""
    s = (XML_START_TEMPLATE % pd.get_title()) + \
            (PIC_BODY_TEMPLATE %
             (prev(n,tot), big_link,
              next(n,tot), next(n,tot),
              pd.orig_name, pd.get_title(), add_para(pd.text) ))

    str2file(s, os.path.join(gal.gal_dir, this(n, tot) +".html"))

def add_para(s):
    return s.replace("\n\n","\n</p><p>\n")


