Attachment 'HighlighterList-1.9.0-2.py'

Download

   1 """
   2     HighlighterList Macro
   3 
   4     MoinMoin - A simple macro for displaying a table with list of available 
   5     Pygments lexers.
   6 
   7     Usage: <<HighlighterList([columns=<list of one of description, names,
   8              patterns, mimetypes, separated by pipe>,
   9              sort_column=(description|names|patterns|mimetypes),
  10              sort=(True|False)], filter_re=<regular expression>)>>
  11 
  12     @param columns: List of columns to display, separated by pipe character.
  13            Currently supported "description", "names", "patterns", "mimetypes".
  14            Unknown column names ignored. Spaces should be omitted. If empty,
  15            all columns are displayed.
  16     @param sort_column: Name of column by which list should be sorted. Column
  17            name can be one of "description", "names", "patterns", "mimetypes".
  18            If column is not "name", item values in this columns will be split.
  19            Has effect in any case (not only when sort is True). If sort_column
  20            is empty, description column is used.
  21     @param filter_re: Filtering regular expression which data in sort_column
  22            should match. If filter_re is empty, no filtering performed.
  23     @param sort: Boolean value (true values are strings "true", "1", "yes" in
  24            any case) which determine whether list should be sorted.
  25 
  26     @copyright: 2009 MoinMoin:EugeneSyromyatnikov
  27     @license: GNU GPL, see COPYING for details.
  28 """
  29 
  30 import re
  31 
  32 from MoinMoin.config import multiconfig
  33 from MoinMoin import wikiutil
  34 
  35 import pygments.lexers
  36 
  37 available_columns = ['description', 'names', 'patterns', 'mimetypes']
  38 
  39 def macro_HighlighterList(macro, columns='|'.join(available_columns),
  40         sort_column=tuple(available_columns),
  41         sort=True, filter_re=None, _kwargs=None):
  42     request = macro.request
  43     _ = request.getText
  44     f = request.formatter
  45 
  46     column_titles = [_('Lexer description'),
  47                      _('Lexer names'),
  48                      _('File patterns'),
  49                      _('Mimetypes'),
  50                     ]
  51 
  52     columns = columns and [available_columns.index(column)
  53                 for column
  54                 in columns.split('|')
  55                 if column in available_columns] or range(len(available_columns))
  56     sort_column = available_columns.index(sort_column) or 0
  57     do_filter = (filter_re not in (None, ""))
  58     filter_re = re.compile(filter_re or ".*")
  59 
  60     lexer_list = pygments.lexers.get_all_lexers()
  61     lexer_data = []
  62 
  63     #expanding tuples if sort_column is not name
  64     if sort_column != 0:
  65         for lexer in lexer_list:
  66             if len(lexer[sort_column]):
  67                 for i in lexer[sort_column]:
  68                     lexer_item = list(lexer)
  69                     lexer_item[sort_column] = i
  70                     lexer_data.append(lexer_item)
  71             else:
  72                 lexer_item = list(lexer)
  73                 lexer_item[sort_column] = ""
  74                 lexer_data.append(lexer_item)
  75     else:
  76         lexer_data.extend(lexer_list)
  77 
  78 
  79     #filtering
  80     if do_filter:
  81         lexer_data = [lexer for lexer in lexer_data
  82                        if filter_re.search(lexer[sort_column])]
  83 
  84     #sorting
  85     if sort:
  86         lexer_data.sort(cmp=lambda x, y:
  87           ((x != y)
  88           and cmp(x[sort_column].lower(), y[sort_column].lower())
  89           or cmp(x[0].lower(), y[0].lower())))
  90 
  91     #generating output
  92     ret = []
  93 
  94     #table header
  95     ret.extend([
  96         f.table(1),
  97         f.table_row(1),
  98         ])
  99     for col in columns:
 100         ret.extend([
 101                     f.table_cell(1),
 102                     f.strong(1),
 103                     f.text(column_titles[col]), f.strong(0), f.table_cell(0)
 104                   ])
 105     ret.append(f.table_row(0))
 106 
 107     #table data
 108     for parser in lexer_data:
 109         ret.append(f.table_row(1))
 110 
 111         for col in columns:
 112             ret.extend([f.table_cell(1), f.text(parser[col]), f.table_cell(0)])
 113 
 114         ret.append(f.table_row(0))
 115 
 116     ret.append(f.table(0))
 117 
 118     return ''.join(ret)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-11-16 08:26:02, 3.9 KB) [[attachment:HighlighterList-1.9.0-2.py]]
  • [get | view] (2009-11-16 06:22:12, 3.8 KB) [[attachment:PygmentsLexerList-1.9.0-1.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.