Attachment 'ImportHtml-1.2.py'
Download 1 """
2 MoinMoin - ImportHtml action
3
4 """
5
6 import mimetypes, string, sys, time, HTMLParser
7 from MoinMoin import config, user, util, wikiutil, webapi
8 from MoinMoin.Page import Page
9 from MoinMoin.PageEditor import PageEditor
10 from MoinMoin.util import MoinMoinNoFooter
11
12 from MoinMoin.HTML2MoinMoin import HTML2MoinMoin
13 import urllib, cStringIO
14
15 #############################################################################
16 ### Create parts of the Web interface
17 #############################################################################
18
19 def show_form(pagename, request):
20 request.http_headers()
21 wikiutil.send_title(request, "Import HTML as Markup")
22 request.write(
23 """
24 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-d\ata">
25 <input type="hidden" name="action" value="ImportHtml">
26 <input type="radio" name="do" value="markup">Show markup<br>
27 <input type="radio" name="do" value="wiki">Show as wiki page<br>
28 <input type="radio" name="do" value="import">Append to page<br>
29 URL: <input type="text" name="url" size="50">
30 <input type="submit" value="Get">
31 </form>
32 """ % {
33 'baseurl': request.getBaseURL(),
34 'pagename': wikiutil.quoteWikiname(pagename),
35 })
36
37 #wikiutil.send_footer(request, pagename, showpage=1)
38
39
40 def get_content(request):
41 if request.form.has_key("url"):
42 try:
43 return urllib.urlopen(request.form["url"][0]).read()
44 except IOError:
45 return ""
46 else:
47 return ""
48
49 def get_parsed(request):
50 p = HTML2MoinMoin()
51 p.output = cStringIO.StringIO()
52 try:
53 p.feed(get_content(request))
54 p.close()
55 except:
56 import traceback
57 traceback.print_exc(None, p.output)
58 return p.output.getvalue()
59
60 def show_markup(pagename, request):
61 request.http_headers(["Content-type: text/plain"])
62 request.write(get_parsed(request))
63 raise MoinMoinNoFooter
64
65 def show_as_wiki_page(pagename, request):
66 page = Page(pagename)
67 page.set_raw_body(get_parsed(request), 1)
68 page.send_page(request)
69
70 def append_to_page(pagename, request):
71 page = PageEditor(pagename, request)
72 page.set_raw_body(page.get_raw_body() + get_parsed(request))
73 page.sendEditor()
74
75 def error_msg(pagename, request, msg):
76 Page(pagename).send_page(request, msg=msg)
77
78
79 def execute(pagename, request):
80 """ Main dispatcher for the 'ImportHtml' action.
81 """
82 _ = request.getText
83
84 msg = None
85 if not request.form.has_key('do'):
86 show_form(pagename, request)
87 elif request.form["do"][0] == "markup":
88 show_markup(pagename, request)
89 elif request.form["do"][0] == "wiki":
90 show_as_wiki_page(pagename, request)
91 elif request.form["do"][0] == "import":
92 append_to_page(pagename, request)
93 else:
94 msg = _('<b>Unsupported action: %s</b>') % (request.form['do'][0],)
95
96 if msg:
97 error_msg(pagename, request, msg)
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.You are not allowed to attach a file to this page.