Back to index

moin  1.9.0~rc2
HighlighterList.py
Go to the documentation of this file.
00001 """
00002     MoinMoin - HighlighterList Macro
00003 
00004     A simple macro for displaying a table with list of available Pygments lexers.
00005 
00006     Usage: <<HighlighterList([columns=<list of one of description, names,
00007              patterns, mimetypes, separated by pipe>,
00008              sort_column=(description|names|patterns|mimetypes),
00009              sort=(True|False)], filter_re=<regular expression>)>>
00010 
00011     @param columns: List of columns to display, separated by pipe character.
00012            Currently supported "description", "names", "patterns", "mimetypes".
00013            Unknown column names ignored. Spaces should be omitted. If empty,
00014            all columns are displayed.
00015     @param sort_column: Name of column by which list should be sorted. Column
00016            name can be one of "description", "names", "patterns", "mimetypes".
00017            If column is not "description", item values in this column will be
00018            split. Has effect in any case (not only when sort is True). If
00019            sort_column is empty, description column is used.
00020     @param filter_re: Filtering regular expression which data in sort_column
00021            should match. If filter_re is empty, no filtering performed.
00022     @param sort: Boolean value (true values are strings "true", "1", "yes" in
00023            any case) which determine whether list should be sorted.
00024 
00025     @copyright: 2009 MoinMoin:EugeneSyromyatnikov
00026     @license: GNU GPL, see COPYING for details.
00027 """
00028 
00029 import re
00030 
00031 from MoinMoin.config import multiconfig
00032 from MoinMoin import wikiutil
00033 
00034 import pygments.lexers
00035 
00036 available_columns = ['description', 'names', 'patterns', 'mimetypes']
00037 
00038 def macro_HighlighterList(macro, columns='|'.join(available_columns),
00039         sort_column=tuple(available_columns),
00040         sort=True, filter_re=None, _kwargs=None):
00041     request = macro.request
00042     _ = request.getText
00043     f = request.formatter
00044 
00045     column_titles = [_('Lexer description'),
00046                      _('Lexer names'),
00047                      _('File patterns'),
00048                      _('Mimetypes'),
00049                     ]
00050 
00051     columns = columns and [available_columns.index(column)
00052                 for column
00053                 in columns.split('|')
00054                 if column in available_columns] or range(len(available_columns))
00055     sort_column = available_columns.index(sort_column) or 0
00056     do_filter = (filter_re not in (None, ""))
00057     filter_re = re.compile(filter_re or ".*")
00058 
00059     lexer_list = pygments.lexers.get_all_lexers()
00060     lexer_data = []
00061 
00062     #expanding tuples if sort_column is not name
00063     if sort_column != 0:
00064         for lexer in lexer_list:
00065             if len(lexer[sort_column]):
00066                 for i in lexer[sort_column]:
00067                     lexer_item = list(lexer)
00068                     lexer_item[sort_column] = i
00069                     lexer_data.append(lexer_item)
00070             else:
00071                 lexer_item = list(lexer)
00072                 lexer_item[sort_column] = ""
00073                 lexer_data.append(lexer_item)
00074     else:
00075         lexer_data.extend(lexer_list)
00076 
00077 
00078     #filtering
00079     if do_filter:
00080         lexer_data = [lexer for lexer in lexer_data
00081                        if filter_re.search(lexer[sort_column])]
00082 
00083     #sorting
00084     if sort:
00085         lexer_data.sort(cmp=lambda x, y:
00086           ((x != y)
00087           and cmp(x[sort_column].lower(), y[sort_column].lower())
00088           or cmp(x[0].lower(), y[0].lower())))
00089 
00090     #generating output
00091     ret = []
00092 
00093     #table header
00094     ret.extend([
00095         f.table(1),
00096         f.table_row(1, style="background-color: #ffffcc"),
00097         ])
00098     for col in columns:
00099         ret.extend([
00100                     f.table_cell(1),
00101                     f.strong(1),
00102                     f.text(column_titles[col]), f.strong(0), f.table_cell(0)
00103                   ])
00104     ret.append(f.table_row(0))
00105 
00106     #table data
00107     for parser in lexer_data:
00108         ret.append(f.table_row(1))
00109 
00110         for col in columns:
00111             if col:
00112                 ret.extend([
00113                             f.table_cell(1),
00114                             f.code(1),
00115                             isinstance(parser[col], str) and f.text(parser[col])
00116                               or (f.code(0) + ', ' + f.code(1)).join([f.text(i) for i in parser[col]]),
00117                             f.code(0),
00118                             f.table_cell(0),
00119                           ])
00120             else:
00121                 ret.extend([f.table_cell(1), f.text(parser[col]), f.table_cell(0)])
00122 
00123         ret.append(f.table_row(0))
00124 
00125     ret.append(f.table(0))
00126 
00127     return ''.join(ret)
00128