Attachment 'FreeMindFlashBrowser.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3 	MoinMoin - FreeMind Flash Browser macro
   4 
   5 	Thin macro based on FreeMind Browser (applet) macro
   6 
   7 	Inserts the FreeMind Flash Browser
   8 	See http://freemind.sourceforge.net/wiki/index.php/Flash
   9 
  10 	[[FreeMindFlashBrowser( urlOfMindmap )]]
  11 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  12 		browser applet there is a default for the width and height of the
  13 		applet
  14 
  15 	[[FreeMindFlashBrowser( urlOfMindmap, width )]]
  16 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  17 		browser applet the width is taken from the argument list there is a
  18 		default for the height of the applet
  19 
  20 	[[FreeMindFlashBrowser( urlOfMindmap, width, height )]]
  21 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  22 		browser applet the width and height are taken from the argument list
  23 
  24 
  25 	@copyright: 2007 by Bum-seok Lee <shinsuk@gwbs.net>
  26 	@license: GNU GPL, see COPYING for details.
  27 """
  28 
  29 from MoinMoin.action import AttachFile
  30 import os
  31 import shutil
  32 
  33 mmcachedir = "/var/www/cache"
  34 mmcacheurl = "http://gwbs.net/cache"
  35 flashurl = "http://gwbs.net/gwiki/freemind"
  36 
  37 debug = False
  38 
  39 # FMBMacro class
  40 
  41 """
  42 	Class FMBMacro implements the execution of a FreeMindFlashBrowser macro call.
  43 	One instance must be used per execution.
  44 """
  45 class FMBMacro:
  46 
  47 	widthDefault = '90%'
  48 	heightDefault= '500'
  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.isFatal = False
  61 		self.applet = [] # this is for the applet code
  62 		self.messages = [] # this is for extra messages
  63 
  64 
  65 	def execute( self ):
  66 		self.info( "Yippieh - FMBMacro is executing!" )
  67 		self.checkArguments()
  68 		self.computeResult()
  69 		return self.result
  70 
  71 
  72 	"""
  73 		Check the arguments given with the macro call.
  74 	"""
  75 	def checkArguments(self):
  76 
  77 		if not self.args:
  78 			self.fatal( "At least one argument, i.e. the URL for the mindmap is expected!" )
  79 			self.usage()
  80 		else:
  81 			argsList = self.args.split(',')
  82 			argsLen = len(argsList)
  83 
  84 		self.width = FMBMacro.widthDefault
  85 		self.height = FMBMacro.heightDefault
  86 
  87 		mindmap = argsList[0].strip()
  88 
  89 		if 2 <= argsLen:
  90 			self.width = argsList[1].strip()
  91 
  92 		if 3 <= argsLen:
  93 			self.height = argsList[2].strip()
  94 
  95 		if 3 < argsLen:
  96 			self.warning( "Too many arguments!" )
  97 			self.usage()
  98 
  99 		self.mindmapUrl = self.getMindmapURL(mindmap)
 100 
 101 		if True:
 102 			self.info( "isHTML= '%s'" %(self.isHTML) )
 103 			self.info( "mindmap= '%s'" %(mindmap) )
 104 			self.info( "width= '%s'" %(self.width) )
 105 			self.info( "height= '%s'" %(self.height) )
 106 			self.info( "mindmap-url= '%s'" %(self.mindmapUrl) )
 107 			self.info( "isFatal= '%s'" %(self.isFatal) )
 108 			self.usage()
 109 
 110 
 111 	"""
 112 		Compute the result of the macro call.
 113 	"""
 114 	def computeResult(self):
 115 		result = "".join( self.messages )
 116 		if not self.isFatal:
 117 			self.computeApplet()
 118 			result += "".join( self.applet )
 119 
 120 		if self.isHTML:
 121 			self.result = self.macro.formatter.rawHTML( result )
 122 		else:
 123 			self.result = result
 124 
 125 
 126 	"""
 127 		Compute the applet link or applet tag (depending on formatter)
 128 	"""
 129 	def computeApplet( self ):
 130 		if self.isHTML:
 131 			applet = """
 132 <!--
 133  - code generated by FreeMindFlashBrowser flash macro -
 134  - see http://freemind.sourceforge.net -
 135 -->
 136 <script type="text/javascript" src="%s/flashobject.js"></script>
 137 
 138 <div id="flashbrowser">
 139 	Flash plugin or Javascript are turned off.
 140 	Activate both and reload to view the mindmap
 141 </div>
 142 
 143 <script type="text/javascript">
 144 	// <![CDATA[
 145 	var fo = new FlashObject("%s/visorFreemind.swf", "visorFreeMind", "%s", "%s", 6, "#9999ff");
 146 	fo.addVariable("openUrl", "_self");
 147 	fo.addVariable("initLoadFile", "%s");
 148 	fo.addVariable("startCollapsedToLevel", "2");
 149 	fo.write("flashbrowser");
 150 	// ]]>
 151 </script>
 152 """ %( flashurl, flashurl, 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 	# message methods
 161 
 162 	def info( self, msg ):
 163 		if debug:
 164 			self.msg( 'Info: ' + msg )
 165 
 166 	def warning( self, msg ):
 167 		self.msg( 'Warning: ' + msg )
 168 
 169 	def error( self, msg ):
 170 		self.msg( 'Error: ' + msg )
 171 
 172 	def fatal( self, msg ):
 173 		self.msg( 'Fatal: ' + msg )
 174 		self.isFatal = True
 175 
 176 	def msg( self, msg ):
 177 		msg = "FreeMindFlashBrowser-Macro:" + msg
 178 		print msg
 179 		if self.isHTML:
 180 			msg = '<h1 style="font-weight:bold;color:blue">' + msg.replace( '\n', '\n<br/>') + '</h1>'
 181 
 182 		self.messages.append(msg)
 183 
 184 
 185 	def usage( self ):
 186 		usage = """
 187 		Usage: [[FreeMindFlashBrowser( urlOfMindmap [,width [,height]] )]]
 188 			urlOfMindmap: an URL reaching the mindmap - allowed schemes are 'http:' and 'attachment:'.
 189 			width:		  (optional) - width of the applet; default: %s
 190 			height:		  (optional) - height of the applet; default: %s
 191 		""" %(self.widthDefault, self.heightDefault)
 192 
 193 		self.info( usage )
 194 
 195 
 196 	"""
 197 		Take the given URL of the mindmap, validate it and return an URL that can be linked to from the applet.
 198 	"""
 199 	def getMindmapURL( self, url ):
 200 
 201 		if url.startswith( 'http:' ):
 202 			return url
 203 
 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 
 208 			# added following lines for flash browser.
 209 			fname = url[11:]
 210 
 211 			attachdir = AttachFile.getAttachDir ( self.macro.request, self.macro.formatter.page.page_name )
 212 			cachedir = os.path.join ( mmcachedir, self.macro.formatter.page.page_name )
 213 
 214 			try: os.symlink ( attachdir, cachedir )
 215 			except OSError, e:
 216 				if e.errno == 17: pass
 217 				else: raise
 218 
 219 			# shutil.copyfile ( origmm, destmm )
 220 			return os.path.join ( mmcacheurl, self.macro.formatter.page.page_name, fname )
 221 
 222 		else:
 223 			self.warning( "Unrecognized scheme in mindmap URL! Url is '%s'!" %(url) )
 224 			self.usage()
 225 			return url
 226 
 227 
 228 # Macro execution interface
 229 
 230 def execute(macro, args):
 231 
 232 	fmb = FMBMacro( macro, args )
 233 	return fmb.execute()
 234 
 235 # vim: ai si cin noet ts=4 sw=4

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-07-02 23:42:50, 8.0 KB) [[attachment:FreeMindFlashBrowser-p2.py]]
  • [get | view] (2007-01-23 16:02:24, 6.0 KB) [[attachment:FreeMindFlashBrowser.py]]
 All files | Selected Files: delete move to page copy to page

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