changeset 5856:8e5559a0a08b

PageList, FullSearch macros: ability to suppress highlighting in result list and item links added alongside with appropriate configuration options. PageList macros is now implemented via FullSearch macros for code deduplication.
author Eugene Syromyatnikov <evgsyr@gmail.com>
date Mon, 21 May 2012 03:23:27 +0400
parents 3315eb3e4012
children a89531afebe8
files MoinMoin/config/multiconfig.py MoinMoin/macro/FullSearch.py MoinMoin/macro/PageList.py
diffstat 3 files changed, 78 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Mon May 21 03:23:18 2012 +0400
+++ b/MoinMoin/config/multiconfig.py	Mon May 21 03:23:27 2012 +0400
@@ -1322,6 +1322,20 @@
       ('show_page_history_link', True, "Add link to page change history "
                                        "RSS feed in theme."),
     )),
+    'search_macro': ('Search macro settings',
+        'Settings related to behaviour of search macros (such as FullSearch, '
+        'FullSearchCached, PageList)',
+    (
+      ('parse_args', False, "Do search macro parameter parsing. In previous "
+                            "versions of MoinMoin, whole search macro "
+                            "parameter string had been interpreted as needle. "
+                            "Now, to provide ability to pass additional "
+                            "parameters, this behaviour should be changed."),
+      ('highlight_titles', 1, "Perform title matches highlighting by default "
+                              "in search results generated by macro."),
+      ('highlight_pages', 1, "Add highlight parameter to links in search "
+                             "results generated by search macros by default."),
+    )),
 }
 
 def _add_options_to_defconfig(opts, addgroup=True):
--- a/MoinMoin/macro/FullSearch.py	Mon May 21 03:23:18 2012 +0400
+++ b/MoinMoin/macro/FullSearch.py	Mon May 21 03:23:27 2012 +0400
@@ -80,8 +80,7 @@
     html = u'\n'.join(html)
     return macro.formatter.rawHTML(html)
 
-
-def execute(macro, needle):
+def execute(macro, needle, titlesearch=False, case=False):
     request = macro.request
     _ = request.getText
 
@@ -89,22 +88,76 @@
     if needle is None:
         return search_box("fullsearch", macro)
 
+    highlight_titles = getattr(request.cfg, "search_macro_highlight_titles", 1)
+    highlight_pages = getattr(request.cfg, "search_macro_highlight_pages", 1)
+
+    err = None
+    # It is needed because otherwise macro instances like
+    # <<FullSearch(..., highlight=1)>> (which found occurrences of "...," and
+    # "highlight=1" before the change) begin behaving differently.
+    if getattr(request.cfg, "search_macro_parse_args", False):
+        needle_found = False
+
+        # parse_quoted_separated() is used instead of rsplit() and such for
+        # proper parsing cases like FullSearch(",") and so.
+        args = wikiutil.parse_quoted_separated_ext(needle,
+                                                   separator=",",
+                                                   name_value_separator="=")
+
+        # First non-tuple item in resulting list to be needle
+        for arg in args:
+            if isinstance(arg, tuple):
+                val = arg[1].lower() in [u'1', u'true', u'y']
+                if arg[0] == u"highlight_pages":
+                    highlight_pages = val
+                elif arg[0] == u"highlight_titles":
+                    highlight_titles = val
+                else:
+                    err = _(u"Unknown macro parameter: %s.") % arg[0]
+            elif isinstance(arg, basestring):
+                if not needle_found:
+                    needle_found = True
+                    needle = arg
+                else:
+                    err = _(u"More than one needle with "
+                             "search_macro_parse_args config option enabled "
+                             "('%(needle)s' found already, '%(arg)s' occured)"
+                             ) % {'needle': wikiutil.escape(needle),
+                                  'arg': wikiutil.escape(arg)}
+
+        if not needle_found:
+            needle = ''
+
     # With empty arguments, simulate title click (backlinks to page)
-    elif needle == '':
-        needle = '"%s"' % macro.formatter.page.page_name
+    if needle == '' and not titlesearch:
+        needle = u'"%s"' % macro.formatter.page.page_name
 
     # With whitespace argument, show error message like the one used in the search box
     # TODO: search should implement those errors message for clients
-    elif needle.isspace():
-        err = _('Please use a more selective search term instead of '
+    elif not needle.strip():
+        err = _(u'Please use a more selective search term instead of '
                 '{{{"%s"}}}', wiki=True) % needle
-        return '<span class="error">%s</span>' % err
+
+    if err:
+        return u'<span class="error">%s</span>' % err
 
     needle = needle.strip()
 
     # Search the pages and return the results
-    results = search.searchPages(request, needle, sort='page_name')
+    try:
+        results = search.searchPages(request, needle, titlesearch=titlesearch,
+                                     case=case, sort='page_name')
 
-    return results.pageList(request, macro.formatter, paging=False)
+        ret = results.pageList(request, macro.formatter, paging=False,
+            highlight_titles=highlight_titles, highlight_pages=highlight_pages)
 
+    except ValueError:
+        # same error as in MoinMoin/action/fullsearch.py, keep it that way!
+        ret = ''.join([macro.formatter.text(u'<<%s(' % macro.name),
+                      _(u'Your search query {{{"%s"}}} is invalid. Please refer '
+                        'to HelpOnSearching for more information.', wiki=True,
+                        percent=True) % wikiutil.escape(needle),
+                      macro.formatter.text(u')>>')])
 
+    return ret
+
--- a/MoinMoin/macro/PageList.py	Mon May 21 03:23:18 2012 +0400
+++ b/MoinMoin/macro/PageList.py	Mon May 21 03:23:27 2012 +0400
@@ -12,6 +12,7 @@
 
 Dependencies = ["namespace"]
 from MoinMoin import search, wikiutil
+from MoinMoin.macro.FullSearch import execute as fs_execute
 
 def execute(macro, args):
     _ = macro._
@@ -20,22 +21,5 @@
     # If called with empty or no argument, default to regex search for .+, the full page list.
     needle = wikiutil.get_unicode(macro.request, args, 'needle', u'regex:.+')
 
-    # With whitespace argument, return same error message as FullSearch
-    if not needle.strip():
-        err = _('Please use a more selective search term instead of {{{"%s"}}}', wiki=True) % needle
-        return '<span class="error">%s</span>' % err
+    return fs_execute(macro, needle, titlesearch=True, case=case)
 
-    # Return a title search for needle, sorted by name.
-    try:
-        results = search.searchPages(macro.request, needle,
-                                     titlesearch=1, case=case,
-                                     sort='page_name')
-        ret = results.pageList(macro.request, macro.formatter, paging=False)
-    except ValueError:
-        # same error as in MoinMoin/action/fullsearch.py, keep it that way!
-        ret = ''.join([macro.formatter.text('<<PageList('),
-                      _('Your search query {{{"%s"}}} is invalid. Please refer to '
-                        'HelpOnSearching for more information.', wiki=True,
-                        percent=True) % wikiutil.escape(needle),
-                      macro.formatter.text(')>>')])
-    return ret