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

Paul Boddie paul at boddie.org.uk
Wed Jul 26 08:17:29 EDT 2017


On Wednesday 26. July 2017 13.56.53 Christoph Zimmermann wrote:
> 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".

I didn't immediately respond - not that I have any proper affiliation with the 
project - because you used the term "markdown" instead of markup, and I 
therefore suspected that you wanted to process Markdown format text, not Moin 
wiki format text.

I don't really use Markdown for anything, but here is more information in case 
you are curious about it:

https://en.wikipedia.org/wiki/Markdown

> 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.

Interesting that you iterate over the lines and strip leading and trailing 
whitespace. Is that to try and convert any DOS newlines? Note that stripping 
leading whitespace could break formatting because certain Moin format features 
rely on indentation.

> 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).

Yes, this is generally good advice. Some people seem to think that Python 2 
can't handle Unicode this conveniently, of course.

When doing similar things myself, I wrote some convenience functions to handle 
parsing and formatting because I don't find the Moin 1.x API particularly 
usable. See here for an example:

http://hgweb.boddie.org.uk/MoinSupport/file/791ab8b6dd9d/MoinSupport.py#l950

Paul


More information about the moin-devel mailing list