Simple (?) question: wiki mardown to HTML
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.
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.
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
participants (3)
-
Christoph -
Christoph Zimmermann -
Paul Boddie