Attachment 'FreemindSitemap_macro_for_moin_1.8.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3 	MoinMoin - Freemind sitemap Flash Browser macro (for MoinMoin V1.8.x)
   4 
   5 	Thin macro based on FreeMind Browser (applet) macro and
   6 	FreemindFlashBrowser macro.
   7 
   8 	Creates a sitemap of your wiki in the freemind format and inserts the
   9 	FreeMind Flash Browser.
  10 	
  11 	See http://freemind.sourceforge.net/wiki/index.php/Flash
  12 
  13 	<<FreeMindFlashBrowser()>>
  14 		displays the sitemap in the FreeMind browser applet with  the
  15 		default for the width, height, and collapsedToLevel of the applet.
  16 
  17 	<<FreeMindFlashBrowser( width )>>
  18 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  19 		browser applet the width is taken from the argument list there is a
  20 		default for the height and collapsedToLevel of the applet.
  21 
  22 	<<FreeMindFlashBrowser( width, height )>>
  23 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  24 		browser applet the width and height are taken from the argument list.
  25 		There is a default for the collapsedToLevel of the applet.
  26 
  27 	<<FreeMindFlashBrowser( width, height, collapsedToLevel )>>
  28 		displays the FreeMind mindmap specified by urlOfMindmap in the FreeMind
  29 		browser applet the width, height and collapsedToLevel are taken from the
  30 		argument list.
  31 
  32 	@copyright: 2007 by Bum-seok Lee <shinsuk@gwbs.net>
  33 	            2009 by Josef Meier <jo.meier@gmx.de>
  34 	@license: GNU GPL, see COPYING for details.
  35 """
  36 
  37 """
  38 	Changes:
  39 
  40 		2007-12-14 siemster <gregory.siems@pca.state.mn.us>
  41 			- Moved mmcachedir to wikiconfig ( as freemind_cache_dir )
  42 			- Moved mmmcacheurl to wikiconfig ( as freemind_cache_url )
  43 			- Moved flashurl to wikiconfig  ( as freemind_flash_browser_url )
  44 			- Added CSSFile ( freemind_css_url in wikiconfig )
  45 			- Added collapsedToLevel
  46 			- Changed 'div id' of the applet script to a generated value
  47 				(for the purpose of allowing multiple instances within the
  48 				same wiki page)
  49 
  50 		2008-06-27 siemster <gregory.siems@pca.state.mn.us>
  51 			- Fix to work with Moin version 1.7
  52 			
  53 		2009-09-10 Added code for automatic creation of freemind data
  54 		           out of your wikis structure. 
  55 
  56 """
  57 
  58 from MoinMoin import config, wikiutil, action
  59 from MoinMoin.action import AttachFile
  60 import os
  61 import md5
  62 import time
  63 import codecs
  64 
  65 debug = False
  66 
  67 # FMBMacro class
  68 
  69 """
  70 	Class FMBMacro implements the execution of a FreeMindFlashBrowser macro call.
  71 	One instance must be used per execution.
  72 """
  73 class FMBMacro:
  74 
  75 	widthDefault = '100%'
  76 	heightDefault= '500'
  77 	collapsedLevelDefault = '2'
  78 
  79 	"""
  80 		Construct the FMBMacro with the execution arguments.
  81 	"""
  82 	def __init__(self, macro, args):
  83 		self.macro = macro
  84 		self.args = args
  85 		self.request = macro.request
  86 
  87 		self.flashBaseUrl  = macro.request.cfg.freemind_flash_browser_url
  88 		self.cssUrl        = macro.request.cfg.freemind_css_url
  89 		self.mmCacheUrl    = macro.request.cfg.freemind_cache_url
  90 		self.mmCacheDir    = macro.request.cfg.freemind_cache_dir
  91 
  92 		# Check and set, if we have an HTML formatter
  93 		self.isHTML = '<br />\n' == self.macro.formatter.linebreak(0)
  94 
  95 		# Fix for Moin 1.7.x
  96 		if not self.isHTML:
  97 			self.isHTML = '<br>\n' == self.macro.formatter.linebreak(0)
  98 
  99 		self.isFatal = False
 100 		self.applet = [] # this is for the applet code
 101 		self.messages = [] # this is for extra messages
 102 
 103 
 104 	def execute( self ):
 105 		self.info( "Yippieh - FMBMacro is executing!" )
 106 		self.getFreemindSitemap()
 107 		self.checkArguments()
 108 		self.computeResult()
 109 		return self.result
 110 
 111 
 112 	"""
 113 		Get sitemap in freemind format by using the "FreemindSitemap" action.
 114 	"""
 115 	def getFreemindSitemap(self):
 116 		freemindSitemap = action.getHandler(self.request, 'FreemindSitemap', 'getSiteMapFreemind')
 117 		
 118 		if freemindSitemap:
 119 			freemindContent = freemindSitemap(self.request.page.page_name, self.request)
 120 		
 121 		if not os.path.isdir(self.mmCacheDir):
 122 			os.mkdir(self.mmCacheDir, 0755)
 123 
 124 		hashStart = time.asctime(time.gmtime()) + self.request.user.id	
 125 
 126 		filename = md5.new(hashStart).hexdigest() + ".mm"
 127 		completeName = os.path.join ( self.mmCacheDir, filename )
 128 
 129 		filehandle = open(completeName,"w")
 130 		filehandle.write( freemindContent.encode( "utf-8" ) )
 131 		filehandle.close()
 132 		self.mindmapUrl = self.mmCacheUrl + "/" + filename
 133 
 134 
 135 	"""
 136 		Check the arguments given with the macro call.
 137 	"""
 138 	def checkArguments(self):
 139 		if not self.args:
 140 			argsList = []
 141 			argsLen = 0
 142 		else:
 143 			argsList = self.args.split(',')
 144 			argsLen = len(argsList)
 145 
 146 		self.width = FMBMacro.widthDefault
 147 		self.height = FMBMacro.heightDefault
 148 		self.collapsedLevel = FMBMacro.collapsedLevelDefault
 149 
 150 		if 1 <= argsLen:
 151 			temp = argsList[0].strip()
 152 			if temp != '':
 153 				self.width = temp
 154 
 155 		if 2 <= argsLen:
 156 			temp = argsList[1].strip()
 157 			if temp != '':
 158 				self.height = temp
 159 
 160 		if 3 <= argsLen:
 161 			temp = argsList[2].strip()
 162 			if temp != '':
 163 				self.collapsedLevel = temp
 164 
 165 		if 3 < argsLen:
 166 			self.warning( "Too many arguments!" )
 167 			self.usage()
 168 
 169 		if True:
 170 			self.info( "isHTML= '%s'" %(self.isHTML) )
 171 			self.info( "width= '%s'" %(self.width) )
 172 			self.info( "height= '%s'" %(self.height) )
 173 			self.info( "startCollapsedToLevel= '%s'" %(self.collapsedLevel) )
 174 			self.info( "mindmap-url= '%s'" %(self.mindmapUrl) )
 175 			self.info( "isFatal= '%s'" %(self.isFatal) )
 176 			self.usage()
 177 
 178 	"""
 179 		Compute the result of the macro call.
 180 	"""
 181 	def computeResult(self):
 182 		result = "".join( self.messages )
 183 
 184 		if not self.isFatal:
 185 			self.computeApplet()
 186 			result += "".join( self.applet )
 187 
 188 		if self.isHTML:
 189 			self.result = self.macro.formatter.rawHTML( result )
 190 		else:
 191 			self.result = result
 192 
 193 
 194 	"""
 195 		Compute the applet link or applet tag (depending on formatter)
 196 	"""
 197 	def computeApplet( self ):
 198 		if self.isHTML:
 199 			divId = md5.new(self.mindmapUrl).hexdigest()
 200 
 201 			applet = """
 202 <!--
 203  - code generated by FreeMindFlashBrowser flash macro -
 204  - see http://freemind.sourceforge.net -
 205 -->
 206 <script type="text/javascript" src="%s/flashobject.js"></script>
 207 
 208 <div id="%s">
 209 	FreemindSitemap isn't working. Maybe you forgot to configure it in 'wikiconfig.py'.
 210 	Or maybe your browsers Flash plugin or Javascript are turned off.
 211 	Activate both and reload to view the mindmap
 212 </div>
 213 
 214 <script type="text/javascript">
 215 	// <![CDATA[
 216 	var fo = new FlashObject("%s/visorFreemind.swf", "visorFreeMind", "%s", "%s", 6, "#9999ff");
 217 	fo.addVariable("openUrl", "_self");
 218 	fo.addVariable("initLoadFile", "%s");
 219 	fo.addVariable("startCollapsedToLevel", "%s");
 220 	fo.addVariable("CSSFile", "%s");
 221 	fo.write("%s");
 222 	// ]]>
 223 </script>
 224 """ %( self.flashBaseUrl, divId, self.flashBaseUrl, self.width, self.height,
 225 			self.mindmapUrl, self.collapsedLevel, self.cssUrl, divId, )
 226 			self.applet.append( applet )
 227 
 228 		else:
 229 			self.applet.append( self.macro.formatter.url( 1, self.mindmapUrl ) )
 230 			self.applet.append( self.macro.formatter.url( 0 ) )
 231 
 232 
 233 	# message methods
 234 
 235 	def info( self, msg ):
 236 		if debug:
 237 		    self.msg( 'Info: ' + msg )
 238 
 239 	def warning( self, msg ):
 240 		self.msg( 'Warning: ' + msg )
 241 
 242 	def error( self, msg ):
 243 		self.msg( 'Error: ' + msg )
 244 
 245 	def fatal( self, msg ):
 246 		self.msg( 'Fatal: ' + msg )
 247 		self.isFatal = True
 248 
 249 	def msg( self, msg ):
 250 		msg = "FreemindSitemap-Macro:" + msg
 251 		print msg
 252 		if self.isHTML:
 253 			msg = '<h1 style="font-weight:bold;color:blue">' + msg.replace( '\n', '\n<br/>') + '</h1>'
 254 
 255 		self.messages.append(msg)
 256 
 257 
 258 	def usage( self ):
 259 		usage = """
 260 		Usage: <<FreemindSitemap( [,urlOfMindmap [,width [,height]] )]]
 261 			urlOfMindmap: an URL reaching the mindmap - allowed schemes are 'http:' and 'attachment:'.
 262 			width:		      (optional) - width of the applet; default: %s
 263 			height:		      (optional) - height of the applet; default: %s
 264 			startCollapsedToLevel (optional) - start collapsed to to node level; default: %s
 265 		""" %(self.widthDefault, self.heightDefault, self.collapsedLevelDefault)
 266 
 267 		self.info( usage )
 268 
 269 
 270 	"""
 271 		Take the given URL of the mindmap, validate it and return an URL that can be linked to from the applet.
 272 	"""
 273 	def getMindmapURL( self, url ):
 274 
 275 		if url.startswith( 'http:' ):
 276 			return url
 277 
 278 		elif url.startswith( 'attachment:'):
 279 			mmFileName = url[11:]
 280 			pagename, drawing = AttachFile.absoluteName(url, self.request.page.page_name)
 281 			mindmap_url = AttachFile.getAttachUrl(pagename, mmFileName, self.request)
 282 			return mindmap_url
 283 
 284 		else:
 285 			self.warning( "Unrecognized scheme in mindmap URL! Url is '%s'!" %(url) )
 286 			self.usage()
 287 			return url
 288 
 289 
 290 # Macro execution interface
 291 
 292 def execute(macro, args):
 293         fmb = FMBMacro( macro, args )
 294         return fmb.execute()
 295 
 296 # 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] (2009-09-30 13:09:31, 2.2 KB) [[attachment:FreemindSitemap_action_for_moin_1.8.py]]
  • [get | view] (2009-09-30 13:09:16, 2.2 KB) [[attachment:FreemindSitemap_action_for_moin_1.9.py]]
  • [get | view] (2009-09-30 13:19:54, 8.2 KB) [[attachment:FreemindSitemap_macro_for_moin_1.8.py]]
  • [get | view] (2009-09-30 13:19:21, 8.2 KB) [[attachment:FreemindSitemap_macro_for_moin_1.9.py]]
 All files | Selected Files: delete move to page copy to page

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