Attachment 'FreeMindFlashBrowser-p2.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, height, and
  13 		collapsedToLevel of the 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 and collapsedToLevel 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 		There is a default for the collapsedToLevel of the applet.
  24 
  25 	[[FreeMindFlashBrowser( urlOfMindmap, width, height, collapsedToLevel )]]
  26 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  27 		browser applet the width, height and collapsedToLevel are taken from the
  28 		argument list.
  29 
  30 	@copyright: 2007 by Bum-seok Lee <shinsuk@gwbs.net>
  31 	@license: GNU GPL, see COPYING for details.
  32 """
  33 
  34 """
  35 	Changes:
  36 
  37 		2007-12-14 siemster <gregory.siems@pca.state.mn.us>
  38 			- Moved mmcachedir to wikiconfig ( as freemind_cache_dir )
  39 			- Moved mmmcacheurl to wikiconfig ( as freemind_cache_url )
  40 			- Moved flashurl to wikiconfig  ( as freemind_flash_browser_url )
  41 			- Added CSSFile ( freemind_css_url in wikiconfig )
  42 			- Added collapsedToLevel
  43 			- Changed 'div id' of the applet script to a generated value
  44 				(for the purpose of allowing multiple instances within the
  45 				same wiki page)
  46 
  47 		2008-06-27 siemster <gregory.siems@pca.state.mn.us>
  48 			- Fix to work with Moin version 1.7
  49 
  50 """
  51 
  52 from MoinMoin.action import AttachFile
  53 import os
  54 import md5
  55 
  56 debug = False
  57 
  58 # FMBMacro class
  59 
  60 """
  61 	Class FMBMacro implements the execution of a FreeMindFlashBrowser macro call.
  62 	One instance must be used per execution.
  63 """
  64 class FMBMacro:
  65 
  66 	widthDefault = '100%'
  67 	heightDefault= '500'
  68 	collapsedLevelDefault = '2'
  69 
  70 	"""
  71 		Construct the FMBMacro with the execution arguments.
  72 	"""
  73 	def __init__(self, macro, args):
  74 		self.macro = macro
  75 		self.args = args
  76 
  77 		self.flashBaseUrl  = macro.request.cfg.freemind_flash_browser_url
  78 		self.cssUrl        = macro.request.cfg.freemind_css_url
  79 		self.mmCacheUrl    = macro.request.cfg.freemind_cache_url
  80 		self.mmCacheDir    = macro.request.cfg.freemind_cache_dir
  81 
  82 		# Check and set, if we have an HTML formatter
  83 		self.isHTML = '<br />\n' == self.macro.formatter.linebreak(0)
  84 
  85 		# Fix for Moin 1.7.x
  86 		if not self.isHTML:
  87 			self.isHTML = '<br>\n' == self.macro.formatter.linebreak(0)
  88 
  89 		self.isFatal = False
  90 		self.applet = [] # this is for the applet code
  91 		self.messages = [] # this is for extra messages
  92 
  93 
  94 	def execute( self ):
  95 		self.info( "Yippieh - FMBMacro is executing!" )
  96 		self.checkArguments()
  97 		self.computeResult()
  98 		return self.result
  99 
 100 
 101 	"""
 102 		Check the arguments given with the macro call.
 103 	"""
 104 	def checkArguments(self):
 105 
 106 		if not self.args:
 107 			self.fatal( "At least one argument, i.e. the URL for the mindmap is expected!" )
 108 			self.usage()
 109 		else:
 110 			argsList = self.args.split(',')
 111 			argsLen = len(argsList)
 112 
 113 		self.width = FMBMacro.widthDefault
 114 		self.height = FMBMacro.heightDefault
 115 		self.collapsedLevel = FMBMacro.collapsedLevelDefault
 116 
 117 		mindmap = argsList[0].strip()
 118 
 119 		if 2 <= argsLen:
 120 			temp = argsList[1].strip()
 121 			if temp != '':
 122 				self.width = temp
 123 
 124 		if 3 <= argsLen:
 125 			temp = argsList[2].strip()
 126 			if temp != '':
 127 				self.height = temp
 128 
 129 		if 4 <= argsLen:
 130 			temp = argsList[3].strip()
 131 			if temp != '':
 132 				self.collapsedLevel = temp
 133 
 134 		if 4 < argsLen:
 135 			self.warning( "Too many arguments!" )
 136 			self.usage()
 137 
 138 		self.mindmapUrl = self.getMindmapURL(mindmap)
 139 
 140 		if True:
 141 			self.info( "isHTML= '%s'" %(self.isHTML) )
 142 			self.info( "mindmap= '%s'" %(mindmap) )
 143 			self.info( "width= '%s'" %(self.width) )
 144 			self.info( "height= '%s'" %(self.height) )
 145 			self.info( "startCollapsedToLevel= '%s'" %(self.collapsedLevel) )
 146 			self.info( "mindmap-url= '%s'" %(self.mindmapUrl) )
 147 			self.info( "isFatal= '%s'" %(self.isFatal) )
 148 			self.usage()
 149 
 150 	"""
 151 		Compute the result of the macro call.
 152 	"""
 153 	def computeResult(self):
 154 		result = "".join( self.messages )
 155 
 156 		if not self.isFatal:
 157 			self.computeApplet()
 158 			result += "".join( self.applet )
 159 
 160 		if self.isHTML:
 161 			self.result = self.macro.formatter.rawHTML( result )
 162 		else:
 163 			self.result = result
 164 
 165 
 166 	"""
 167 		Compute the applet link or applet tag (depending on formatter)
 168 	"""
 169 	def computeApplet( self ):
 170 		if self.isHTML:
 171 			divId = md5.new(self.mindmapUrl).hexdigest()
 172 
 173 			applet = """
 174 <!--
 175  - code generated by FreeMindFlashBrowser flash macro -
 176  - see http://freemind.sourceforge.net -
 177 -->
 178 <script type="text/javascript" src="%s/flashobject.js"></script>
 179 
 180 <div id="%s">
 181 	Flash plugin or Javascript are turned off.
 182 	Activate both and reload to view the mindmap
 183 </div>
 184 
 185 <script type="text/javascript">
 186 	// <![CDATA[
 187 	var fo = new FlashObject("%s/visorFreemind.swf", "visorFreeMind", "%s", "%s", 6, "#9999ff");
 188 	fo.addVariable("openUrl", "_self");
 189 	fo.addVariable("initLoadFile", "%s");
 190 	fo.addVariable("startCollapsedToLevel", "%s");
 191 	fo.addVariable("CSSFile", "%s");
 192 	fo.write("%s");
 193 	// ]]>
 194 </script>
 195 """ %( self.flashBaseUrl, divId, self.flashBaseUrl, self.width, self.height,
 196 			self.mindmapUrl, self.collapsedLevel, self.cssUrl, divId, )
 197 			self.applet.append( applet )
 198 
 199 		else:
 200 			self.applet.append( self.macro.formatter.url( 1, self.mindmapUrl ) )
 201 			self.applet.append( self.macro.formatter.url( 0 ) )
 202 
 203 
 204 	# message methods
 205 
 206 	def info( self, msg ):
 207 		if debug:
 208 		    self.msg( 'Info: ' + msg )
 209 
 210 	def warning( self, msg ):
 211 		self.msg( 'Warning: ' + msg )
 212 
 213 	def error( self, msg ):
 214 		self.msg( 'Error: ' + msg )
 215 
 216 	def fatal( self, msg ):
 217 		self.msg( 'Fatal: ' + msg )
 218 		self.isFatal = True
 219 
 220 	def msg( self, msg ):
 221 		msg = "FreeMindFlashBrowser-Macro:" + msg
 222 		print msg
 223 		if self.isHTML:
 224 			msg = '<h1 style="font-weight:bold;color:blue">' + msg.replace( '\n', '\n<br/>') + '</h1>'
 225 
 226 		self.messages.append(msg)
 227 
 228 
 229 	def usage( self ):
 230 		usage = """
 231 		Usage: [[FreeMindFlashBrowser( urlOfMindmap [,width [,height]] )]]
 232 			urlOfMindmap: an URL reaching the mindmap - allowed schemes are 'http:' and 'attachment:'.
 233 			width:		      (optional) - width of the applet; default: %s
 234 			height:		      (optional) - height of the applet; default: %s
 235 			startCollapsedToLevel (optional) - start collapsed to to node level; default: %s
 236 		""" %(self.widthDefault, self.heightDefault, self.collapsedLevelDefault)
 237 
 238 		self.info( usage )
 239 
 240 
 241 	"""
 242 		Take the given URL of the mindmap, validate it and return an URL that can be linked to from the applet.
 243 	"""
 244 	def getMindmapURL( self, url ):
 245 
 246 		if url.startswith( 'http:' ):
 247 			return url
 248 
 249 		elif url.startswith( 'attachment:'):
 250 			mmFileName = url[11:]
 251 			wikiPageName = self.macro.formatter.page.page_name
 252 			attachDir    = os.path.abspath(AttachFile.getAttachDir ( self.macro.request, wikiPageName ))
 253 			wikiDir      = os.getcwd().split('/')[-1]
 254 
 255 			# As there may be multiple wikis on the same server,
 256 			# ensure that each wiki has it's own subdir under the mm cache dir
 257 			dirName = os.path.join ( self.mmCacheDir, wikiDir )
 258 			if not os.path.isdir(dirName):
 259 				os.mkdir(dirName, 0755)
 260 
 261 			# Deal with slashes '/' in the wiki page name
 262 			for token in wikiPageName.split('/'):
 263 				dirName = os.path.join(dirName, token)
 264 				if not os.path.isdir(dirName):
 265 					os.mkdir(dirName, 0755)
 266 
 267 			cacheDir = os.path.join ( self.mmCacheDir, wikiDir, wikiPageName, 'attachments' )
 268 
 269 			try: os.symlink ( attachDir, cacheDir )
 270 			except OSError, e:
 271 				if e.errno == 17: pass
 272 				else: raise
 273 
 274 			return_url = os.path.join ( self.mmCacheUrl, wikiDir, wikiPageName, 'attachments', mmFileName )
 275 			return return_url
 276 
 277 		else:
 278 			self.warning( "Unrecognized scheme in mindmap URL! Url is '%s'!" %(url) )
 279 			self.usage()
 280 			return url
 281 
 282 
 283 # Macro execution interface
 284 
 285 def execute(macro, args):
 286 
 287 	fmb = FMBMacro( macro, args )
 288 	return fmb.execute()
 289 
 290 # 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.