# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - HelpOn Action Macro

    PURPOSE:
        This action macro is used to get help on an other extensions

    SYNTAX:
        http://WikiServer/WikiName?action=HelpOn

    PROCEDURE:
       You get a pull down list of available extensions and after selection 
       the help from inside of the routine is shown
       
       The extensions below the MoinMoin path are indicated by the prefix MM- 
       While the others are indicated by a W- in the list

       Please remove the version number from the filename.

    MODIFICATION HISTORY:
        @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
        @license: GNU GPL, see COPYING for details.
        Version: 1.3.5-1
        
        1.3.5-2 bug fix if no routine desription is defined
                minor change from table output to preformatted text
                
        1.3.5-3 code refactored      
        1.3.5-4 PRE does not need an additional line break
        
        1.5.1-5 removed request.cfg.excluded_actions 
        1.5.4-6 2006-07-04 RB: some PEP8 changes
        1.6.0-7 RB code change for 1.6 release, string replaced
        1.6.0-8 RB code changed to get multiple language support for plugins (local plugins not implemented now)
"""
import  codecs
from MoinMoin import wikiutil, macro, action, parser
from MoinMoin.Page import Page
from MoinMoin.formatter.text_html import Formatter

def HelpOn_Extract_Help(cfg, module):
    if module.startswith('MoinMoin'):
        return __import__(module).__doc__
    
def HelpOn(request,pagename,module):
    request.formatter = Formatter(request)
    info = HelpOn_Extract_Help(request.cfg,module)   
    html = "<PRE>%(txt)s</PRE>" % {
               "txt" : ''.join(info)}
    
    request.http_headers()
    request.setContentLanguage(request.lang)
    request.theme.send_title(pagename)
    request.write(request.formatter.startContent("content"))    
    request.write(html)
    request.write(request.formatter.endContent())
    request.theme.send_footer(pagename)
 
    
def execute(pagename, request):
    _ = request.getText
    actname = __name__.split('.')[-1]
    thispage = Page(request,pagename)

    if request.form.has_key('button') and request.form.has_key('ticket'):
        # check whether this is a valid ticket request (make outside
        # attacks harder by requiring two full HTTP transactions)
        if not wikiutil.checkTicket(request.form['ticket'][0]) :
            return thispage.send_page(request, msg = _('Please use the interactive user interface for HelpOn extensions!'))
        module = request.form.get('helpon', [u''])[0]
        
        return (HelpOn(request, pagename, module))
            
    html = []    
    for name in action.extension_actions :
        html.append("<OPTION>MoinMoin.action.%(name)s</OPTION>" % { "name": name}) 
    """       
    for name in wikiutil.wikiPlugins('macro', request.cfg): #request)[1]: 
        html.append("<OPTION>Wiki.action.%(name)s</OPTION>" % { "name": name}) 
    """    
    for name in macro.extension_macros :
        html.append("<OPTION>MoinMoin.macro.%(name)s</OPTION>" % { "name": name})  
    """      
    for name in wikiutil.wikiPlugins('macro', request.cfg):
        html.append("<OPTION>Wiki.macro.%(name)s</OPTION>" % { "name": name})    
    """  
    for name in  parser.modules : 
        html.append("<OPTION>MoinMoin.parser.%(name)s</OPTION>" % { "name": name})  
    """         
    for name in wikiutil.wikiPlugins('parser', request.cfg):
        html.append("<OPTION>Wiki.parser.%(name)s</OPTION>" % { "name": name})      
    """
    formhtml = '''
<form method="post" action="">
<select name="helpon" size="1">
%(option)s
</select>
<input type="hidden" name="action" value="%(actname)s">
<input type="submit" name="button" value="%(button)s">
<input type="hidden" name="ticket" value="%(ticket)s">
<p>
</form>''' % {
    'actname': 'HelpOn',
    'ticket' : wikiutil.createTicket(),
    'option' : ''.join(html),
    'button' : 'Help'}

    return thispage.send_page(request, msg=formhtml)
