# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Frame Parser

    This parser is used to align enclosed wiki markup.
        
    Syntax:
        {{{#!frame align=align,frame_thick=frame_thick,frame_style=frame_style,frame_color=frame_color,
        frame_background=frame_background,frame_position=frame_position,frame_width=frame_width,frame_padding=frame_padding,
        frame_margin=frame_margin
        wiki markup
        }}}
    
    Parameters:
        align:            one of ['left', 'right', 'center', 'justify', 'column:left', 'column:right','float:left','float:right']
                          default: left
        frame_thick:      one of ['thin', 'medium',' thick','1px','2px','5px','10px']
                          default: thin
        frame_style:      one of ['none','hidden', 'dotted', 'dashed', 'solid', 'double',
                                  'groove', 'ridge', 'inset',  'outset']
                          default: solid
        frame_color:      each which could be vrified by web.Color(str(name))
                          default: #9999FF
                       
        frame_background: each which could be vrified by web.Color(str(name))
                          default: transparent
                          
        frame_position:   one of ['static','absolute','relative','fixed','inherit']
                          default: relative
                          
        frame_width:      has to end with % is testet by str(float(number))
                          default: auto
                          
        frame_padding:    has to end with em is testet by str(float(number))
                          default: 0em
                          
        frame_margin:     has to end with em is testet by str(float(number))
                          default: 0em                 
        
        text_align:       one of ['left', 'right', 'center', 'justify']                              
                          default: left
                          
        wiki markup: could any wiki markup 

    Procedure:
        Please remove the version number.
        
        The units are limited for numbers. And only one value for frame_padding or frame_margin 

    Examples:
        {{{
        #!frame align=float:right
        attachment:moinmoin.png
        ||A||B||
        ||C||D||
        ||C||D||
        }}}
    
        {{{ 
#!frame align=column:left,frame_position=relative,frame_width=48%,frame_margin=0em,frame_thick=2px,frame_color=blue,frame_background=yellow
A WikiName is a word that uses capitalized words. WikiNames automagically become hyperlinks to the WikiName's page. What exactly is an uppercase or lowercase letter is determined by the configuration, the default configuration should work for UTF-8 characters (digits are treated like lowercase characters). 


When you click on the highlighted page title (i.e. WikiName on this page), you will see a list of all pages that link to the current page. This even works on pages that are not defined yet.
}}}{{{
#!frame align=column:right,frame_position=relative,frame_width=50%,frame_margin=0em,frame_thick=2px,frame_color=blue
When you click on the highlighted page title (i.e. WikiName on this page), you will see a list of all pages that link to the current page. This even works on pages that are not defined yet. 


A question mark before a link or a different rendering in grey means that the page is not yet defined: you can click the question mark or page name to offer a definition (e.g., ?NoSuchPageForReal). If you click on such a link, you will see a default page that you can edit; only after you save the page will it be created for real. A list of all pages that are not yet created but referred on another page is on WantedPages. 


To escape a WikiName, i.e. if you want to write the word WikiName without linking it, use an "empty" bold sequence (a sequence of six single quotes) like this: Wiki''''''Name. Alternatively, you can use the shorter sequence "``" (two backticks), i.e. Wiki``Name.
}}}
{{{ 
#!frame align=clear
}}}
        
        
    Modification History:
        @copyright: 2006 by Reimar Bauer
        @license: GNU GPL, see COPYING for details.
            
        
    
        
       initial version 1.6.0-1
"""
import StringIO
from random import randint
from MoinMoin.parser import text_moin_wiki
from MoinMoin import wikiutil
from MoinMoin.action import AttachFile
from MoinMoin.util import web

Dependencies = []

class Parser:

    extensions = ['*']
    Dependencies = Dependencies

    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        
        self.align = 'left'  # secured
        
        self.text_align = 'left' #secured
        self.frame_thick = 'thin' #secured
        self.frame_style = 'solid' #secured
        self.frame_color = '#9999FF' #secured but color code name error crashes
        self.frame_background = 'transparent' #secured but color code name error crashes
        self.frame_position = 'relative' #secured
        self.frame_width = 'auto' #needs a better idea to get it secure at the moment only % is allowed
        self.frame_padding = '0em' #needs a better idea to get it secure
        self.frame_margin = '0em'#needs a better idea to get it secure
        
        
        for arg in kw.get('format_args', '').split(','):

            if arg.find('=') > -1:
                key, value = arg.split('=')
                setattr(self, key, wikiutil.escape(value.strip(), quote=1)) 
                
    

    def format(self, formatter):
        raw = self.raw
        pagename = formatter.page.page_name

        out = StringIO.StringIO()
        self.request.redirect(out)
        wikiizer = text_moin_wiki.Parser(raw, self.request)
        wikiizer.format(formatter)
        result = out.getvalue()
        self.request.redirect()
        del out
        
        if self.frame_position in ['static','absolute','relative','fixed','inherit']:
           frame_position =  self.frame_position
        else:
           frame_position = 'relative'
        
        if self.frame_thick in ['thin', 'medium',' thick','1px','2px','5px','10px']:
           frame_thick = self.frame_thick
        else: 
           frame_thick = '0'
           
        if self.text_align in ['left', 'right', 'center', 'justify']:
           text_align = self.text_align
        else:
           text_align = 'left'   
           
        if self.frame_style in ['none','hidden', 'dotted', 'dashed', 'solid', 'double',
                                'groove', 'ridge', 'inset',  'outset']:
           frame_style = self.frame_style    
        else:
           frame_style = 'solid'   
                         
        if self.frame_color !=  'transparent':
            frame_color = web.Color(str(self.frame_color))
        else:
            frame_color = self.frame_color
            
        if self.frame_background !=  'transparent':
           frame_background = web.Color(str(self.frame_background))
        else:
           frame_background = self.frame_background
           
        if self.frame_width.endswith("%"):
            frame_width = str(float(self.frame_width[:-1]))+'%'
        else:
            frame_width = 'auto'   
            
        if self.frame_padding.endswith("em"):
            frame_padding = str(float(self.frame_padding[:-2]))+'em'
        else:
            frame_padding = '0em'       
            
        if self.frame_margin.endswith("em"):
            frame_margin = str(float(self.frame_margin[:-2]))+'em'
        else:
            frame_margin = '0em'         
           
        if self.align in ['left', 'right', 'center', 'justify']:
            div = '<div align="%(align)s" style="border-width:%(frame_thick)s; border-color:%(frame_color)s; border-style:%(frame_style)s; position:%(frame_position)s; padding:%(frame_padding)s; margin:%(frame_margin)s; background-color:%(frame_background)s" width="%(frame_width)s">' % { 
                    "frame_thick": frame_thick,
                    "frame_style": frame_style,
                    "frame_color": frame_color,
                    "frame_position": frame_position,
                    "frame_padding": frame_padding,
                    "frame_margin": frame_margin,
                    "frame_background": frame_background,
                    "frame_width": frame_width,
                    "text_align": text_align,
                    "align": self.align,
                    }
            self.request.write("%(div)s%(result)s</div>" % {
                 "div": div,
                 "result": result}) 
        
        if self.align in ['column:left', 'column:right','float:left','float:right']:         
            if self.align == 'column:left':
                align = 'float:left'
            if  self.align == 'column:right':
                align = 'float:right'
            if self.align == 'float:left':
                align = self.align
            if  self.align == 'float:right':
                align = self.align
                
            tab = '<table style="%(align)s;" position="%(frame_position)s" width="%(frame_width)s" border="%(frame_thick)s"; bgcolor="%(frame_background)s"><tbody><tr><td style="border-style:none;">' % {
                    "align": align,
                    "frame_position": frame_position,
                    "frame_width": frame_width,
                    "frame_thick": frame_thick,
                    "frame_background": frame_background,
                } 
            self.request.write("%(tab)s%(result)s</td></tr></tbody></table>" % {
                               "tab": tab,
                               "result": result})  
            
        if self.align == 'clear':
            self.request.write('<br clear="both">')                        
            
                 
            
            
        
        
        
        
        