|
| 1 | +# Copyright: 2025 MoinMoin:UlrichB |
| 2 | +# License: GNU GPL v2 (or any later version), see LICENSE.txt for details. |
| 3 | + |
| 4 | +""" |
| 5 | +MoinMoin CLI - Migration of FullSearch macro (moin1.9) to new ItemList macro (moin2) if possible |
| 6 | +""" |
| 7 | + |
| 8 | +from moin.cli.migration.moin19 import macro_migration |
| 9 | +from moin.utils.tree import moin_page |
| 10 | + |
| 11 | +CONTENT_TYPE_MACRO_FORMATTER = "x-moin/macro;name={}" |
| 12 | +MACRO_NAME_FULLSEARCH = "FullSearch" |
| 13 | +MACRO_NAME_FULLSEARCH_CACHED = "FullSearchCached" |
| 14 | + |
| 15 | + |
| 16 | +def convert_fullsearch_macro_to_item_list(node): |
| 17 | + """Convert the given FullSearch macro node to an ItemList macro in-place |
| 18 | +
|
| 19 | + The moin1.9 FullSearch macro is used in various situations. One case |
| 20 | + is the listing of all pages for a specific category. In moin2, the |
| 21 | + categories are replaced by tags. Therefore, all macro calls related to |
| 22 | + categories are converted to the moin2 macro ItemList. |
| 23 | +
|
| 24 | + In all other cases, the FullSearch macro call is not changed. |
| 25 | + The same applies to the FullSearchCached macro. |
| 26 | +
|
| 27 | + Example conversions: |
| 28 | +
|
| 29 | + | PageList macro (moin1.9) | ItemList macro (moin2) | |
| 30 | + |-----------------------------------------|----------------------------------------------| |
| 31 | + | <<FullSearch(CategorySample)>> | <<ItemList(item="/", tag="CategorySample")>> | |
| 32 | + | <<FullSearch(category:CategorySample)>> | <<ItemList(item="/", tag="CategorySample")>> | |
| 33 | +
|
| 34 | + :param node: the DOM node matching the FullSearch macro content type |
| 35 | + :type node: emeraldtree.tree.Element |
| 36 | + """ |
| 37 | + |
| 38 | + # arguments |
| 39 | + args_before = None |
| 40 | + for elem in node.iter_elements(): |
| 41 | + if elem.tag.name == "arguments": |
| 42 | + args_before = elem.text |
| 43 | + if args_before and args_before.startswith("Category"): |
| 44 | + # argument is a category name, lets migrate to ItemList macro |
| 45 | + args_after = f'item="/", tag="{args_before}"' |
| 46 | + elif args_before and args_before.startswith("category:"): |
| 47 | + # argument is a category name, strip keyword and migrate to ItemList macro |
| 48 | + args_after = f'item="/", tag="{args_before[9:]}"' |
| 49 | + else: |
| 50 | + # argument is not a category or empty, we keep the old FullSearch macro |
| 51 | + return |
| 52 | + |
| 53 | + # content type |
| 54 | + new_content_type = CONTENT_TYPE_MACRO_FORMATTER.format("ItemList") |
| 55 | + node.set(moin_page.content_type, new_content_type) |
| 56 | + |
| 57 | + for elem in node.iter_elements(): |
| 58 | + if elem.tag.name == "arguments": |
| 59 | + elem.clear() |
| 60 | + elem.append(args_after) |
| 61 | + |
| 62 | + # 'alt' attribute |
| 63 | + new_alt = f"<<ItemList({args_after})>>" |
| 64 | + node.set(moin_page.alt, new_alt) |
| 65 | + |
| 66 | + |
| 67 | +macro_migration.register_macro_migration( |
| 68 | + CONTENT_TYPE_MACRO_FORMATTER.format(MACRO_NAME_FULLSEARCH), convert_fullsearch_macro_to_item_list |
| 69 | +) |
| 70 | + |
| 71 | +macro_migration.register_macro_migration( |
| 72 | + CONTENT_TYPE_MACRO_FORMATTER.format(MACRO_NAME_FULLSEARCH_CACHED), convert_fullsearch_macro_to_item_list |
| 73 | +) |
0 commit comments