[moin-devel] Simple (?) question: wiki mardown to HTML

Christoph Zimmermann monochromec at gmail.com
Wed Jul 26 07:56:53 EDT 2017


After the overwhelming response to my initial question (is the project still alive? :-)
I took another look and figured out a solution:

html = wikiutil.renderText(LugVor.request, text_moin_wiki.Parser, text)

is the essential invocation of the HTML generation from standard MoinMoin markdown in the
string "text". 

So in the shape of a macro this would look like the following (for version 1.9.9, using the deprecated
invocation syntax):

from codecs import open
from MoinMoin import wikiutil
from MoinMoin.parser import text_moin_wiki
import os

def execute(macro, path):
        html = ''

        if os.access(path, os.R_OK):
            try:
                lines = []
                 with open(path, 'rt', encoding='utf-8') as f:
                      for line in f:
                          lines.append(line.strip())
                 html = wikiutil.renderText(macro.request, text_moin_wiki.Parser, '\n'.join(lines))
            except Exception as e:
	        pass # Or similar :-)

         return html

In a nutshell, this macro reads a text file containing valid markup and passes this to the 
renderText method for processing.

Caveat: if your markup contains unicode characters, ensure using a suitable file handler for proper
translation (as above with the utf-8 encoding in the open statement from the codecs package).

On 21 Jul 2017 18:58, Christoph wrote:
> I have the requirement to translate some dynamically generated wiki markdown 
> to HTML as part of a macro invocation on a page. The idea is to invoke a macro
> which then retrieves text from an external source which may include markdown
> already and then to convert this to valid HTML which can be display on the 
> page containing the macro.
> 
> After spending several hours w/o success on the actual approach, my suspicion
> is that I need a wiki markdown parser and a text / HTML formatter but I'm 
> lacking the exact Python code snippet for the macro as the existing documentation
> is far less than conclusive on this matter.
> 
> Any help is appreciated - thanks in advance!
> 

-- 

This email account is monitored seven days a week. 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/moin-devel/attachments/20170726/933c5797/attachment.sig>


More information about the moin-devel mailing list