Attachment 'FreeMindBrowser_for_1_9_2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - FreeMind browser Macro
   4 
   5     Inserts the FreeMindBrowser applet.
   6     See http://freemind.sourceforge.net/wiki/index.php/Main_Page
   7 
   8     [[FreeMindBrowser( urlOfMindmap )]]
   9         displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind browser applet
  10         there is a default for the width and height of the applet
  11 
  12     [[FreeMindBrowser( urlOfMindmap, width )]]
  13         displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind browser applet
  14         the width is taken from the argument list
  15         there is a default for the height of the applet
  16 
  17     [[FreeMindBrowser( urlOfMindmap, width, height )]]
  18         displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind browser applet
  19         the width and height are taken from the argument list
  20 
  21 
  22     @copyright: 2005 by Jürgen Lind <xxx@xxx>
  23     @license: GNU GPL, see COPYING for details.
  24 """
  25 
  26 from MoinMoin.action import AttachFile
  27 
  28 Dependencies = []
  29 
  30 debug = False
  31 
  32 # # # # # # # # # # # # # # #
  33 # FMBMacro class
  34 # # # # # # # # # # # # # # #
  35 """
  36     Class FMBMacro implements the execution of a FreeMindBrowser macro call.
  37     One instance must be used per execution.
  38 """
  39 class FMBMacro:
  40 
  41     appletClass = 'freemind.main.FreeMindApplet.class'
  42     widthDefault = '100%'
  43     heightDefault= '500'
  44 
  45 
  46     """
  47         Construct the FMBMacro with the execution arguments.
  48     """
  49     def __init__(self, macro, args):
  50         self.macro = macro
  51         self.args = args
  52 
  53         # Check and set, if we have an HTML formatter
  54         self.isHTML = '<br />\n' == self.macro.formatter.linebreak(0)
  55 
  56         # Following line works with an alias for '/applets' in apache httpd.conf (on windows).
  57         # appletArchive= "/applets/freemindbrowser.jar"
  58         appletArchive= "%s/applets/FreeMindBrowser/freemindbrowser.jar" % (self.macro.request.cfg.url_prefix_static)
  59         self.appletArchive = appletArchive
  60 
  61         self.isFatal = False
  62         self.applet = [] # this is for the applet code
  63         self.messages = [] # this is for extra messages
  64 
  65 
  66     def execute( self ):
  67         self.info( "Yippieh - FMBMacro is executing!" )
  68         self.checkArguments()
  69         self.computeResult()
  70         return self.result
  71 
  72 
  73     """
  74         Check the arguments given with the macro call.
  75     """
  76     def checkArguments(self):
  77 
  78         if not self.args:
  79             self.fatal( "At least one argument, i.e. the URL for the mindmap is expected!" )
  80             self.usage()
  81         else:
  82             argsList = self.args.split(',')
  83             argsLen = len(argsList)
  84 
  85         self.width = FMBMacro.widthDefault
  86         self.height = FMBMacro.heightDefault
  87 
  88         mindmap = argsList[0].strip()
  89 
  90         if 2 <= argsLen:
  91             self.width = argsList[1].strip()
  92 
  93         if 3 <= argsLen:
  94             self.height = argsList[2].strip()
  95 
  96         if 3 < argsLen:
  97             self.warning( "Too many arguments!" )
  98             self.usage()
  99 
 100         self.mindmapUrl = self.getMindmapURL(mindmap)
 101 
 102         # self.checkUrlAccess( self.mindmapUrl )
 103 
 104         if True:
 105             self.info( "isHTML= '%s'" %(self.isHTML) )
 106             self.info( "mindmap= '%s'" %(mindmap) )
 107             self.info( "width= '%s'" %(self.width) )
 108             self.info( "height= '%s'" %(self.height) )
 109             self.info( "mindmap-url= '%s'" %(self.mindmapUrl) )
 110             self.info( "isFatal= '%s'" %(self.isFatal) )
 111             self.usage()
 112 
 113 
 114     """
 115         Compute the result of the macro call.
 116     """
 117     def computeResult(self):
 118         result = "".join( self.messages )
 119         if not self.isFatal:
 120             self.computeApplet()
 121             result += "".join( self.applet )
 122             self.result = result
 123 
 124 
 125     """
 126         Compute the applet link or applet tag (depending on formatter)
 127     """
 128     def computeApplet( self ):
 129             applet = """
 130 <!--
 131  - code generated by FreeMindBrowser macro -
 132  - see http://freemind.sourceforge.net -
 133 -->
 134 <APPLET CODE="%s"
 135         ARCHIVE="%s"
 136         WIDTH="%s" HEIGHT="%s" >
 137   <PARAM NAME="type" VALUE="application/x.java-applet;version=1.4">
 138   <PARAM NAME="scriptable" VALUE="false">
 139   <PARAM NAME="modes" VALUE="freemind.modes.browsemode.BrowseMode">
 140   <PARAM NAME="browsemode_initial_map" VALUE="%s">
 141   <PARAM NAME="initial_mode" VALUE="Browse">
 142   <PARAM NAME="selection_method" VALUE="selection_method_direct">
 143   <PARAM NAME="X" VALUE="XX">
 144 </APPLET>
 145             """ %( FMBMacro.appletClass, self.appletArchive, self.width, self.height, self.mindmapUrl )
 146             self.applet.append( applet )
 147 
 148 
 149 
 150     # # # # # # # # # # # #
 151     # message methods
 152     # # # # # # # # # # # #
 153     def info( self, msg ):
 154         if debug:
 155             self.msg( 'Info: ' + msg )
 156 
 157     def warning( self, msg ):
 158         self.msg( 'Warning: ' + msg )
 159 
 160     def error( self, msg ):
 161         self.msg( 'Error: ' + msg )
 162 
 163     def fatal( self, msg ):
 164         self.msg( 'Fatal: ' + msg )
 165         self.isFatal = True
 166 
 167     def msg( self, msg ):
 168         msg = "FreeMindBrowser-Macro:" + msg
 169         print msg
 170         if self.isHTML:
 171             msg = '<h1 style="font-weight:bold;color:blue">' + msg.replace( '\n', '\n<br/>') + '</h1>'
 172 
 173         self.messages.append(msg)
 174 
 175 
 176     def usage( self ):
 177         usage = """
 178         Usage: [[FreeMindBrowser( urlOfMindmap [,width [,height]] )]]
 179             urlOfMindmap: an URL reaching the mindmap - allowed schemes are 'http:' and 'attachment:'.
 180             width:        (optional) - width of the applet; default: %s
 181             height:       (optional) - height of the applet; default: %s
 182         """ %(self.widthDefault, self.heightDefault)
 183 
 184         self.info( usage )
 185 
 186 
 187     """
 188         Take the given URL of the mindmap, validate it and return an URL that can be linked to from the applet.
 189     """
 190     def getMindmapURL( self, url ):
 191 
 192         if url.startswith( 'http:' ):
 193             return url
 194         elif url.startswith( 'attachment:'):
 195             relativeUrl = AttachFile.getAttachUrl( self.macro.formatter.page.page_name, url[11:], self.macro.request )
 196             qualifiedUrl = self.macro.request.getQualifiedURL( relativeUrl )
 197             return qualifiedUrl
 198         else:
 199             self.warning( "Unrecognized scheme in mindmap URL! Url is '%s'!" %(url) )
 200             self.usage()
 201             return url
 202 
 203 
 204 
 205     """
 206         Desktop edition returns 200 on non existing resource :-(
 207         is this correct behaviour?
 208     """
 209     def checkUrlAccess( self, url ):
 210         import urllib
 211 
 212         try:
 213             obj = urllib.urlopen( url )
 214             self.info( "urllib.urlopen( %s ) returns:" %(url) )
 215             self.info( str(obj) )
 216             self.info( "obj.info(): " )
 217             self.info( obj.info() )
 218             obj.close()
 219         except IOError:
 220             self.fatal( "Mindmap of specified url [%s] is not accessable." %(self.mindmapUrl) )
 221 
 222 
 223 # # # # # # # # # # # # # # #
 224 # Macro execution interface
 225 # # # # # # # # # # # # # # #
 226 
 227 def execute(macro, args):
 228 
 229     fmb = FMBMacro( macro, args )
 230     # fmb.execute()
 231     return fmb.execute()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-01-29 23:32:09, 7.3 KB) [[attachment:FreeMindBrowser.py]]
  • [get | view] (2010-03-30 01:01:00, 7.1 KB) [[attachment:FreeMindBrowser_for_1_9_2.py]]
  • [get | view] (2006-10-13 14:12:15, 7.3 KB) [[attachment:FreeMindBrowser_patched.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.