From monochromec at gmail.com Fri Jul 21 12:58:39 2017 From: monochromec at gmail.com (Christoph) Date: Fri, 21 Jul 2017 18:58:39 +0200 Subject: [moin-devel] Simple (?) question: wiki mardown to HTML Message-ID: <56449112-a2e8-7aef-64a0-3f0a27d67e99@gmail.com> 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. From monochromec at gmail.com Wed Jul 26 07:56:53 2017 From: monochromec at gmail.com (Christoph Zimmermann) Date: Wed, 26 Jul 2017 13:56:53 +0200 Subject: [moin-devel] Simple (?) question: wiki mardown to HTML In-Reply-To: <56449112-a2e8-7aef-64a0-3f0a27d67e99@gmail.com> References: <56449112-a2e8-7aef-64a0-3f0a27d67e99@gmail.com> Message-ID: <67203942-c9ac-14a8-d746-21a7b364c866@gmail.com> 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: From paul at boddie.org.uk Wed Jul 26 08:17:29 2017 From: paul at boddie.org.uk (Paul Boddie) Date: Wed, 26 Jul 2017 14:17:29 +0200 Subject: [moin-devel] Simple (?) question: wiki mardown to HTML In-Reply-To: <67203942-c9ac-14a8-d746-21a7b364c866@gmail.com> References: <56449112-a2e8-7aef-64a0-3f0a27d67e99@gmail.com> <67203942-c9ac-14a8-d746-21a7b364c866@gmail.com> Message-ID: <201707261417.29642.paul@boddie.org.uk> 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 From jasper.olbrich at posteo.de Wed Jul 26 16:49:47 2017 From: jasper.olbrich at posteo.de (Jasper Olbrich) Date: Wed, 26 Jul 2017 22:49:47 +0200 Subject: [moin-devel] Extend pages via action as another user Message-ID: Hi, I'm trying to create a kind of group mechanism that allows any registered user who knows a password to join an existing group in my wiki. Setup: * TestGroup page, member list at the bottom of the page * LetMeIn page with a form field for the password (FormCreate macro) * custom action deriving from FormSubmit At first, I tried to edit the current revision of TestGroup on the file system level, but there seem to be some caching issues with regard to the permissions, or I screwed up the revisions or something else. It didn't work reliably. Then I found the Page and PageEditor classes and did the following: real_user = self.request.user # 'su' u = User(self.request, _getUserIdByKey(self.request, "name", "ol")) self.request.user = u p = Page(self.request, "TestGroup") rev = p.current_rev() pe = PageEditor(self.request, "TestGroup") pe.saveText(p.get_raw_body() + " * {}\n".format(real_user.name), rev) # 'un-su' self.request.user = real_user "ol" is a user that can write to TestGroup. This code works, but feels kind of wrong. Are there better ways to do what I want? -- Jasper From paul at boddie.org.uk Wed Jul 26 18:04:05 2017 From: paul at boddie.org.uk (Paul Boddie) Date: Thu, 27 Jul 2017 00:04:05 +0200 Subject: [moin-devel] Extend pages via action as another user In-Reply-To: References: Message-ID: <201707270004.05722.paul@boddie.org.uk> On Wednesday 26. July 2017 22.49.47 Jasper Olbrich wrote: > > I'm trying to create a kind of group mechanism that allows any > registered user who knows a password to join an existing group in my wiki. > > Setup: > * TestGroup page, member list at the bottom of the page > * LetMeIn page with a form field for the password (FormCreate macro) > * custom action deriving from FormSubmit > > At first, I tried to edit the current revision of TestGroup on the file > system level, but there seem to be some caching issues with regard to > the permissions, or I screwed up the revisions or something else. It > didn't work reliably. Although Moin 1.x has a fairly understandable filesystem layout, getting the permissions right can be a challenge. Furthermore, there is caching involved with pages that provide group details, which may be the problem you were experiencing. > Then I found the Page and PageEditor classes and did the following: > > real_user = self.request.user > # 'su' > u = User(self.request, _getUserIdByKey(self.request, "name", "ol")) > self.request.user = u > p = Page(self.request, "TestGroup") > rev = p.current_rev() > pe = PageEditor(self.request, "TestGroup") > pe.saveText(p.get_raw_body() + " * {}\n".format(real_user.name), rev) > # 'un-su' > self.request.user = real_user > > "ol" is a user that can write to TestGroup. > This code works, but feels kind of wrong. Are there better ways to do > what I want? Probably not! In my ApproveChanges plug-in for Moin, I did something rather similar. See the following resource for more details: http://hgweb.boddie.org.uk/ApproveChanges/file/5e947e093388/ApproveChangesSupport.py#l180 Ideally, it would be possible to edit the group dictionary and have the dictionary serialised back to the originating page, but this is not a straightforward exercise. (Moin 2 might use special, rigid resources for things like this but I haven't really investigated.) Paul From monochromec at gmail.com Thu Jul 27 08:16:23 2017 From: monochromec at gmail.com (Christoph) Date: Thu, 27 Jul 2017 14:16:23 +0200 Subject: [moin-devel] Simple (?) question: wiki mardown to HTML Message-ID: <35a5752e-2119-aa00-3665-3edfd9600812@gmail.com> Hi Paul, Thanks for the hint - I indeed meant markup and not markdown. Regarding the strip: the sample code that I posted was an interim snapshot - the intention was to remove trailing newlines which screwed up subsequent code not part of the snippet. But instead of calling strip() the following works just as well: for line in f: if line [-1] == '\n': l0 = line [:-1] else: l0 = line lines.append(l0) Thanks again for your input! -- 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: 455 bytes Desc: OpenPGP digital signature URL: From paul at boddie.org.uk Thu Jul 27 10:11:39 2017 From: paul at boddie.org.uk (Paul Boddie) Date: Thu, 27 Jul 2017 16:11:39 +0200 Subject: [moin-devel] Simple (?) question: wiki mardown to HTML In-Reply-To: <35a5752e-2119-aa00-3665-3edfd9600812@gmail.com> References: <35a5752e-2119-aa00-3665-3edfd9600812@gmail.com> Message-ID: <201707271611.39362.paul@boddie.org.uk> On Thursday 27. July 2017 14.16.23 Christoph wrote: > > Thanks for the hint - I indeed meant markup and not markdown. No problem! Markdown isn't exactly the most sensible name someone could have chosen for a text format. ;-) > Regarding the strip: the sample code that I posted was an interim snapshot > - the intention was to remove trailing newlines which screwed up > subsequent code not part of the snippet. > > But instead of calling strip() the following works just as well: > > for line in f: > if line [-1] == '\n': > l0 = line [:-1] > else: > l0 = line > lines.append(l0) > > > Thanks again for your input! You could use rstrip, potentially with "\r\n" as argument if you don't want to strip tabs and spaces. Paul