def parse(story): """ The main workhorse that does nothing but call MoinMoin to do its dirty laundry @param story: A text for conversion @type story: string @returns: formatted string @rtype: string """ s = StringIO() oldstdout = sys.stdout form = None page = Page(None) page.hilite_re = None request = Request() request.user = User() formatter = Formatter(request) formatter.setPage(page) sys.stdout = s Parser(story, request).format(formatter, form) sys.stdout = oldstdout result = s.getvalue() s.close() return result
def moin2doku(pagename, text): parser = Parser(text, request) # this needed for macros request.formatter = formatter p = Page(request, pagename) formatter.setPage(p) output = StringIO.StringIO() # wrap sys.stdout as RequestCLI has no interface to say where to output stdout = sys.stdout sys.stdout = output parser.format(formatter) sys.stdout = stdout return unicode(output.getvalue().decode('utf-8'))
def __init__(self, raw, request, **kw): XsltParser.__init__(self, raw, request) # relative path to docbook.xsl and compiled_xsl docbook_html_directory = request.cfg.docbook_html_dir self.db_xsl = os.path.join(docbook_html_directory, 'docbook.xsl') self.db_compiled_xsl = os.path.join(docbook_html_directory, 'db_compiled.dat') self.wikiParser = WikiParser(raw = self.raw, request = self.request, pretty_url=1) self.key = 'docbook'
def formatMarkup(request, text, currentStack = []): """ Formats the text passed according to wiki markup. This raises an exception if a text needs itself to be translated, this could possibly happen with macros. """ try: currentStack.index(text) raise Exception("Formatting a text that is being formatted?!") except ValueError: pass currentStack.append(text) from MoinMoin.Page import Page from MoinMoin.parser.wiki import Parser from MoinMoin.formatter.text_html import Formatter import StringIO origtext = text out = StringIO.StringIO() request.redirect(out) parser = Parser(text, request) formatter = Formatter(request, terse = True) reqformatter = None if hasattr(request, 'formatter'): reqformatter = request.formatter request.formatter = formatter p = Page(request, "$$$$i18n$$$$") formatter.setPage(p) parser.format(formatter) text = out.getvalue() if reqformatter == None: del request.formatter else: request.formatter = reqformatter request.redirect() del currentStack[-1] text = text.strip() return text
class Parser(XsltParser): """ Send XML file formatted via XSLT. """ caching = 1 Dependencies = Dependencies def __init__(self, raw, request, **kw): XsltParser.__init__(self, raw, request) # relative path to docbook.xsl and compiled_xsl docbook_html_directory = request.cfg.docbook_html_dir self.db_xsl = os.path.join(docbook_html_directory, 'docbook.xsl') self.db_compiled_xsl = os.path.join(docbook_html_directory, 'db_compiled.dat') self.wikiParser = WikiParser(raw = self.raw, request = self.request, pretty_url=1) self.key = 'docbook' def format(self, formatter): self.wikiParser.formatter = formatter XsltParser.format(self, formatter) def append_stylesheet(self): """" virtual function, for docbook parser """ abs_db_xsl = os.path.abspath(self.db_xsl) abs_db_compiled_xsl = os.path.abspath(self.db_compiled_xsl) # same as path.exists, but also test if it is a file if not os.path.isfile(abs_db_compiled_xsl): _compile_xsl(abs_db_xsl, abs_db_compiled_xsl) assert os.path.isfile(abs_db_compiled_xsl) self.processor.appendStylesheetInstance(cPickle.load(file(abs_db_compiled_xsl, 'rb'))) def parse_result(self, result): """ additional parsing to the resulting XSLT'ed result (resultString) before saving will do: BASIC CLEAN UP : remove unnecessary HTML tags RESOLVE IMG SRC : fix src to find attachment RESOLVE WikiNames: if a word is a valid wikiname & a valid wikipage, replace word with hyperlink """ # BASIC CLEAN UP # remove from beginning until end of body tag found = re.search('<body.*?>', result) if found: result = result[found.end():] # remove everything after & including </body> found = result.rfind('</body>') if found != -1: result = result[:found] # RESOLVE IMG SRC found = re.finditer('<img.*?>', result) if found: splitResult = _splitResult(found, result) for index in range(len(splitResult)): if splitResult[index].startswith('<img'): found = re.search('src="(?P<source>.*?)"', splitResult[index]) imageSrc = found.group('source') imageAlt = None # save alt found = re.search('alt="(?P<alt>.*?)"', splitResult[index]) if found: imageAlt = found.group('alt') splitResult[index] = self.wikiParser.attachment( ('attachment:' + imageSrc, "") ) if imageAlt: # restore alt splitResult[index] = re.sub('alt=".*?"', 'alt="%s"' % imageAlt, splitResult[index]) result = ''.join(splitResult) # RESOLVE WikiNames # if a word is a valid wikiname & a valid wikipage, # replace word with hyperlink found = re.finditer(self.wikiParser.word_rule, result) if found: splitResult = _splitResult(found, result) for index in range(len(splitResult)): if (re.match(self.wikiParser.word_rule, splitResult[index]) and Page.Page(self.request, splitResult[index]).exists()): splitResult[index] = self.wikiParser._word_repl(splitResult[index]) result = ''.join(splitResult) # remove stuff that fail HTML 4.01 Strict verification # remove unsupported attributes result = re.sub(' target=".*?"| type=".*?"', '', result) result = re.sub('<hr .*?>', '<hr>', result) # remove <p>...</p> inside <a>...</a> or <caption>...</caption> found = re.finditer('<a href=".*?</a>|<caption>.*?</caption>', result) # XXX re.DOTALL) if found: splitResult = _splitResult(found, result) for index in range(len(splitResult)): if (splitResult[index].startswith('<a href="') or splitResult[index].startswith('<caption>')): splitResult[index] = splitResult[index].replace('<p>', '').replace('</p>', '') result = ''.join(splitResult) return result
def execute(pagename, request): from MoinMoin import wikiutil from MoinMoin.Page import Page _ = request.getText thispage = Page(request, pagename) if request.user.valid: username = request.user.name else: username = '' if not username: return thispage.send_page(request, msg = _('Please log in first.')) userhomewiki = request.cfg.user_homewiki if userhomewiki != 'Self' and userhomewiki != request.cfg.interwikiname: interwiki = wikiutil.getInterwikiHomePage(request, username=username) wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_wiki(request, '%s:%s' % interwiki) wikiurl = wikiutil.mapURL(request, wikiurl) homepageurl = wikiutil.join_wiki(wikiurl, wikitail) request.http_redirect('%s?action=MyPages' % homepageurl) homepage = Page(request, username) if not homepage.exists(): return homepage.send_page(request, msg = _('Please first create a homepage before creating additional pages.')) pagecontent = _("""\ You can add some additional sub pages to your already existing homepage here. You can choose how open to other readers or writers those pages shall be, access is controlled by group membership of the corresponding group page. Just enter the sub page's name and click on the button to create a new page. Before creating access protected pages, make sure the corresponding group page exists and has the appropriate members in it. Use HomepageGroupsTemplate for creating the group pages. ||'''Add a new personal page:'''||'''Related access control list group:'''|| ||[[NewPage(HomepageReadWritePageTemplate,read-write page,%(username)s)]]||["%(username)s/ReadWriteGroup"]|| ||[[NewPage(HomepageReadPageTemplate,read-only page,%(username)s)]]||["%(username)s/ReadGroup"]|| ||[[NewPage(HomepagePrivatePageTemplate,private page,%(username)s)]]||%(username)s only|| """, formatted=False) pagecontent = pagecontent % locals() pagecontent = pagecontent.replace('\n', '\r\n') from MoinMoin.Page import Page from MoinMoin.parser.wiki import Parser from MoinMoin.formatter.text_html import Formatter pagename = username request.http_headers() # This action generate data using the user language request.setContentLanguage(request.lang) wikiutil.send_title(request, _('MyPages management', formatted=False), pagename=pagename) # Start content - IMPORTANT - without content div, there is no # direction support! request.write(request.formatter.startContent("content")) parser = Parser(pagecontent, request) formatter = Formatter(request) reqformatter = None if hasattr(request, 'formatter'): reqformatter = request.formatter request.formatter = formatter p = Page(request, "$$$") formatter.setPage(p) parser.format(formatter) if reqformatter == None: del request.formatter else: request.formatter = reqformatter # End content and send footer request.write(request.formatter.endContent()) wikiutil.send_footer(request, pagename)