-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1789 from UlrichB22/randomquote
Add RandomQuote macro
- Loading branch information
Showing
7 changed files
with
153 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
= Fortune Cookies = | ||
|
||
This item is used to show the result of the RandomQuote macro. | ||
|
||
* I like '''MoinMoin''' Wiki | ||
* Try out MoinMoin version 2.0, see install docs at [[https://moin-20.readthedocs.io/en/latest/admin/install.html|moin-20.readthedocs.io]] | ||
* The '''RandomQuote''' macro uses the item '''FortuneCookies''' by default | ||
* This is a random quote generated by the '''RandomQuote''' macro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"action": "SAVE", | ||
"address": "127.0.0.1", | ||
"comment": "", | ||
"contenttype": "text/x.moin.wiki;charset=utf-8", | ||
"dataid": "1ae8372681474a6bafc162078dc6679d", | ||
"externallinks": [ | ||
"https://moin-20.readthedocs.io/en/latest/admin/install.html" | ||
], | ||
"itemid": "237f55f536a14767b5b0f4942a8bb1d8", | ||
"itemlinks": [], | ||
"itemtransclusions": [], | ||
"itemtype": "default", | ||
"language": "en", | ||
"mtime": 1730226097, | ||
"name": [ | ||
"FortuneCookies" | ||
], | ||
"name_old": [], | ||
"namespace": "help-en", | ||
"rev_number": 1, | ||
"revid": "2ee6b5b42d304ebcab3e7874df973aa7", | ||
"sha1": "8ceccfe52b6914aeb48eef5acef9163ac074ec6c", | ||
"size": 407, | ||
"summary": "", | ||
"tags": [], | ||
"wikiname": "MyMoinMoin" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright: 2002-2004 Juergen Hermann <[email protected]> | ||
# Copyright: 2002 MoinMoin:ThomasWaldmann | ||
# Copyright: 2024 MoinMoin:UlrichB | ||
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details. | ||
|
||
""" | ||
MoinMoin - RandomQuote Macro selects a random quote from FortuneCookies or a given moinwiki item. | ||
Usage: | ||
<<RandomQuote()>> | ||
<<RandomQuote(WikiTips)>> | ||
Comments: | ||
It will look for list delimiters on the moinwiki item in question. | ||
It will ignore anything that is not in an "*" list. | ||
""" | ||
|
||
import random | ||
|
||
from moin.constants.keys import NAME_EXACT | ||
from moin.items import Item | ||
from moin.i18n import _ | ||
from moin.constants.itemtypes import ITEMTYPE_NONEXISTENT | ||
from moin.converters._util import decode_data | ||
from moin.converters import default_registry as reg | ||
from moin.macros._base import MacroInlineBase, fail_message, valid_item_name | ||
from moin.utils.mime import Type, type_moin_document | ||
|
||
from moin.utils.interwiki import get_fqname, split_fqname | ||
|
||
random.seed() | ||
|
||
|
||
class Macro(MacroInlineBase): | ||
"""Return a random quote from FortuneCookies or a given wiki item""" | ||
|
||
def macro(self, content, arguments, page_url, alternative): | ||
item_name = arguments[0] if arguments else "FortuneCookies" | ||
if item_name[0] in ['"', "'"] and item_name[-1] in ['"', "'"]: # remove quotes | ||
item_name = item_name[1:-1] | ||
if not valid_item_name(item_name): | ||
err_msg = _("Invalid value given for item name: {0}").format(item_name) | ||
return fail_message(err_msg, alternative) | ||
|
||
# use same namespace as current item | ||
namespace = split_fqname(str(page_url.path)).namespace | ||
if not item_name.startswith(f"{namespace}/"): | ||
item_name = get_fqname(item_name, NAME_EXACT, namespace) | ||
|
||
# get the item with the list of quotes | ||
item = Item.create(item_name) | ||
if item.itemtype == ITEMTYPE_NONEXISTENT: | ||
err_msg = _("Item does not exist or read access blocked by ACLs: {0}").format(item_name) | ||
return fail_message(err_msg, alternative) | ||
data = decode_data(item.content.data, item.contenttype) | ||
|
||
# select lines looking like a list item | ||
quotes = data.splitlines() | ||
quotes = [quote.strip() for quote in quotes] | ||
quotes = [quote[2:] for quote in quotes if quote.startswith("* ")] | ||
if not quotes: | ||
err_msg = _("No quotes found in {0}").format(item_name) | ||
return fail_message(err_msg, alternative) | ||
|
||
result = random.choice(quotes) | ||
# quote may use some sort of markup, convert it to dom | ||
input_conv = reg.get(Type(item.contenttype), type_moin_document, includes="expandall") | ||
if not input_conv: | ||
raise TypeError(f"We cannot handle the conversion from {item.contenttype} to the DOM tree") | ||
return input_conv(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters