Attachment 'FreeMindBrowser.py'

Download

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