#!/usr/bin/env python
# $Id: parsewikiup,v 1.8 2005/08/31 23:23:13 villate Exp $

""" Converts a text file with wikiup syntax into HTML, using a template.
"""

copyright = """
Copyright (C) 2003, 2004, 2005 Jaime Villate <villate@gnu.org>
 """
license = """
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifht Floor, Boston,
MA 02110-1301, USA.
"""

import getopt, sys, os
from time import strftime, gmtime
from wikiup import WikiTranslator, ApplyHtmlTemplate, LatexToPNG

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:t:e:s:z:",
                     ["help", "output=", "template=", "eqprefix=", "nopng",
                      "stylesheet=", "zoom="])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    output = None
    png = True
    template = None
    stylesheet = None
    zoom = None
    eqprefix = 'eq-'
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o == "--nopng":
            png = False
        if o in ("-o", "--output"):
            output = a
        if o in ("-t", "--template"):
            template = a
        if o in ("-s", "--stylesheet"):
            stylesheet = a
        if o in ("-e", "--eqprefix"):
            eqprefix = a
        if o in ("-z", "--zoom"):
            zoom = a

    if len(args) <> 1:
        usage()
    else:
        filename = args[0]
        file = open(filename)
        pagename = os.path.basename(filename)
        wt=WikiTranslator(file.read())
        wt.SetEqPrefix(eqprefix)
        wt.TranslateToHtmlLines()
        page=wt.page
        today=strftime('%Y-%m-%d')
        page['today']=today
        page['rightnow']=strftime("%Y-%m-%d %H:%M:%S +0000", gmtime())

        if 'html-title' not in page:
            if 'title' in page:
                page['html-title'] = page['title']
            else:
                page['html-title'] = filename
        if 'date' not in page:
            page['date'] = today
        if 'pagename' not in page:
            page['pagename'] = pagename
        if template:
            page['template'] = template
        else:
            if 'template' not in page:
                page['template'] = 'template.html'
        if zoom:
            page['zoom'] = zoom
        else:
            if 'zoom' not in page:
                page['zoom'] = 1
        if stylesheet:
            page['stylesheet'] = stylesheet
            
        if (png and 'equations' in page):
            num = 0
            resolution = int(round(90*float(page['zoom'])))
            for eq in page['equations']:
                num += 1
                LatexToPNG(eq,'%s%s.png' % (eqprefix, num), resolution)
        if output:
            f = open(output,'w')
            f.write(ApplyHtmlTemplate(page) + '\n')
        else:
            print ApplyHtmlTemplate(page)
    sys.exit()
    

def usage():
    print """
Usage: parsewikiup [options] filename

Options:
   -t FILE or --template=FILE    Use FILE as HTML template
   -s FILE or --stylesheet=FILE  Link HTML page to CSS stylesheet FILE
   -h or --help                  Print this help screen
   -o FILE or --output=FILE      Save output into FILE
   -e PREF or --eqprefix PREF    Use PREF as prefix for equation files
   -z NUM  or --zoom=NUM         Magnify the HTML page to NUM zoom factor
   --nopng                       Does not create PNG files for the equations
    """
    return

if __name__ == "__main__":
    main()



