Attachment 'EmbedWikiPage-1.3.1-1.py'

Download

   1 """
   2     MoinMoin - EmbedWikiPage a macro to embed a wiki page from a different wiki
   3     @license: GNU GPL, see COPYING for details.
   4 
   5     PURPOSE:
   6         This macro is used to embed a wiki page into the current wikipage.
   7 	It is possible to embed local pages as well as external wiki pages.
   8 
   9 	if a WikiName is detected the name is translated into a link to the original page.
  10 	On some circumstances a WikiName is not detected, see RESTRICTIONS.
  11 
  12         The page is embedded temporary and you get at the end a link to edit the original page.
  13 
  14 	At this stage it could be used to embed single pages e.g. macro explanaitions from moinmoin
  15 
  16     CALLING SEQUENCE:
  17        [[EmbedWikiPage(site,name)]]
  18 
  19 
  20     INPUTS:
  21        site: the URL of the wiki where the page is on
  22        name: the wikiname of the page
  23 
  24     EXAMPLE:
  25        [[EmbedWikiPage(http://moinmoin.wikiwikiweb.de:8000/,ProcessorMarket/sctable)]]
  26 
  27     PROCEDURE:
  28       * Editing of an embedded page is always editing the original page.
  29       * wiki:, wiki:/ or [" "] page and normal !WikiName links are followed to their original locations
  30       * attachments are embedded
  31 
  32       Please remove the version number from the routine name!
  33 
  34     RESTRICTIONS:
  35       * at the moment it follows nearly all wiki links
  36 
  37       I have a simple mechanism to detect wiki:, wiki:/ or ["HOWTO"] and normal !WikiName pagelinks
  38 
  39       At the moment I don't find !WikiName which are inserted into tabulars and macros.
  40 
  41       Later on it would be fine to use the icon of the page (favicon)
  42       to show where are the links from.
  43 
  44     MODIFICATION:
  45        @copyright: 2004-09-26 by Reimar Bauer (R.Bauer@fz-juelich.de) EmbedWikiPage-1.2.3-1
  46        2004-09-28: RB 1.2.3-2 !WikiName is now supported (some cases are missing)
  47        2004-09-28: RB 1.2.3-3 !WikiName rules extended
  48        2004-10-16: RB 1.2.3-4 trailing space in site or name ignored
  49        2004-12-20: RB 1.3.1-1 adopted to new wiki syntax
  50 
  51     DISCUSSION:
  52 
  53 
  54 
  55 
  56 """
  57 import string, codecs, os, urllib
  58 from MoinMoin.parser import wiki
  59 from MoinMoin import wikiutil
  60 from MoinMoin import config
  61 
  62 def fetchfile(urlopener, url):
  63 #Copyright (c) 2002-2003  Gustavo Niemeyer <niemeyer@conectiva.com>
  64 # adopted by Reimar Bauer
  65     geturl = url+"?action=raw"
  66     filename, headers = urlopener.retrieve(geturl)
  67     return filename
  68 
  69 def get_urlopener(moinurl):
  70 #Copyright (c) 2002-2003  Gustavo Niemeyer <niemeyer@conectiva.com>
  71 # adopted by Reimar Bauer
  72     urlopener = urllib.URLopener()
  73     proxy = os.environ.get("http_proxy")
  74     if proxy:
  75         urlopener.proxies.update({"http": proxy})
  76     return urlopener
  77 
  78 
  79 def execute(macro, text):
  80 
  81     request=macro.request
  82     formatter=macro.formatter
  83 
  84     if text:
  85       args=text.split(',')
  86     else:
  87       args=[]
  88     number_args=len(args)
  89     if number_args < 2:
  90       return macro.formatter.sysmsg('Not enough arguments to EmbedWikiPage macro')
  91 
  92 
  93     site=wikiutil.escape(string.join(string.strip(args[0]),''), quote=1)
  94     name=wikiutil.escape(string.join(string.strip(args[1]),''), quote=1)
  95 
  96     url="%(site)s%(name)s" %{
  97           "site": site,
  98 	  "name": name
  99 	}
 100 
 101     if (name.find("/") > -1):
 102        wikiname=(name.split('/'))[0]
 103     else:
 104        wikiname=name
 105 
 106     urlopener = get_urlopener(url)
 107     moinfile = fetchfile(urlopener, url)
 108 
 109 
 110     file = codecs.open(moinfile, 'rb', config.charset)
 111     txt = file.read()
 112     file.close()
 113     words=txt.split(" ")
 114     i=0
 115     for item in words:
 116       if (item.find('attachment:') > -1):
 117           words[i]=string.replace(words[i],"attachment:",site+name+'?action=AttachFile&do=get&target=')
 118       if (item.find('/') == 0):
 119          words[i]=string.replace(words[i],string.replace(item,'.',''),' ['+site+name+string.replace(item,'.','')+' '+item+']')
 120 
 121       if (item.find('wiki:/') > -1):
 122           s=string.replace(item,'_','(5f)')
 123           words[i]=string.replace(words[i],item,s)
 124           words[i]=string.replace(words[i],"wiki:/",site+name+'/')
 125 
 126       if (item.find('wiki:') > -1):
 127           s=string.replace(item,'_','(5f)')
 128           words[i]=string.replace(words[i],item,s)
 129           words[i]=string.replace(words[i],"wiki:",site)
 130       if (item.find('[') > -1 ):
 131          if (item[0] == '['):
 132             if (item[1] == '"'):
 133                 w=item[2:]
 134                 pos=w.find('"')
 135                 w=w[0:pos]
 136                 pos=item.find(']')
 137                 w=w[0:pos]
 138 		if ((len(w)) < len(item)) :
 139 		    further=item[(pos+1):]
 140 		else:
 141 		    further=""
 142                 words[i]=string.replace(words[i],item,'['+site+wikiname+'/'+w+' '+w+']')+further
 143       test=item
 144       if (test.find('.') > -1):
 145           test=string.replace(test,'.','')
 146       if (test.find(',') > -1):
 147           test=string.replace(test,',','')
 148       if (test.find('!') > 0):
 149           test=string.replace(test,'!','')
 150       if (test.find(';') > -1):
 151           test=string.replace(test,';','')
 152 
 153       if wikiutil.isStrictWikiname(test):
 154 #         if (item.find('_') > -1):
 155 #             words[i]=string.replace(words[i],'_','(5f)')
 156          words[i]=string.replace(words[i],test,' ['+site+wikiname+'/'+test+' '+test+']')
 157 
 158 
 159 
 160 
 161       i=i+1
 162 
 163     txt=string.join(words," ")
 164 
 165     request.write('<HR width=50%>')
 166 
 167     wikiizer = wiki.Parser(string.join(txt,""),request)
 168     wikiizer.format(formatter)
 169 
 170     edit_url=url+'?action=edit'
 171 
 172     edit_icon=request.theme.make_icon("edit")
 173 
 174     request.write('<HR>')
 175     cmd="<a title=%(edit_url)s href=%(edit_url)s > %(edit_icon)s   %(txt)s</a>" % {
 176       "edit_url":'"'+edit_url+'"',
 177       "edit_icon":edit_icon,
 178       "txt":'Edit embedded page '+name+' on '+site
 179       }
 180 
 181 
 182     request.write(cmd)
 183     return ""

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] (2004-10-01 19:14:33, 5.5 KB) [[attachment:EmbedWikiPage-1.2.3-3.py]]
  • [get | view] (2004-12-20 23:04:18, 5.5 KB) [[attachment:EmbedWikiPage-1.3.1-1.py]]
 All files | Selected Files: delete move to page copy to page

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