Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.macro.HighlighterList Namespace Reference

Functions

def macro_HighlighterList

Variables

list available_columns = ['description', 'names', 'patterns', 'mimetypes']

Detailed Description

    MoinMoin - HighlighterList Macro

    A simple macro for displaying a table with list of available Pygments lexers.

    Usage: <<HighlighterList([columns=<list of one of description, names,
             patterns, mimetypes, separated by pipe>,
             sort_column=(description|names|patterns|mimetypes),
             sort=(True|False)], filter_re=<regular expression>)>>

    @param columns: List of columns to display, separated by pipe character.
           Currently supported "description", "names", "patterns", "mimetypes".
           Unknown column names ignored. Spaces should be omitted. If empty,
           all columns are displayed.
    @param sort_column: Name of column by which list should be sorted. Column
           name can be one of "description", "names", "patterns", "mimetypes".
           If column is not "description", item values in this column will be
           split. Has effect in any case (not only when sort is True). If
           sort_column is empty, description column is used.
    @param filter_re: Filtering regular expression which data in sort_column
           should match. If filter_re is empty, no filtering performed.
    @param sort: Boolean value (true values are strings "true", "1", "yes" in
           any case) which determine whether list should be sorted.

    @copyright: 2009 MoinMoin:EugeneSyromyatnikov
    @license: GNU GPL, see COPYING for details.

Function Documentation

def MoinMoin.macro.HighlighterList.macro_HighlighterList (   macro,
  columns = '|'.join(available_columns),
  sort_column = tuple(available_columns),
  sort = True,
  filter_re = None,
  _kwargs = None 
)

Definition at line 40 of file HighlighterList.py.

00040 
00041         sort=True, filter_re=None, _kwargs=None):
00042     request = macro.request
00043     _ = request.getText
00044     f = request.formatter
00045 
00046     column_titles = [_('Lexer description'),
00047                      _('Lexer names'),
00048                      _('File patterns'),
00049                      _('Mimetypes'),
00050                     ]
00051 
00052     columns = columns and [available_columns.index(column)
00053                 for column
00054                 in columns.split('|')
00055                 if column in available_columns] or range(len(available_columns))
00056     sort_column = available_columns.index(sort_column) or 0
00057     do_filter = (filter_re not in (None, ""))
00058     filter_re = re.compile(filter_re or ".*")
00059 
00060     lexer_list = pygments.lexers.get_all_lexers()
00061     lexer_data = []
00062 
00063     #expanding tuples if sort_column is not name
00064     if sort_column != 0:
00065         for lexer in lexer_list:
00066             if len(lexer[sort_column]):
00067                 for i in lexer[sort_column]:
00068                     lexer_item = list(lexer)
00069                     lexer_item[sort_column] = i
00070                     lexer_data.append(lexer_item)
00071             else:
00072                 lexer_item = list(lexer)
00073                 lexer_item[sort_column] = ""
00074                 lexer_data.append(lexer_item)
00075     else:
00076         lexer_data.extend(lexer_list)
00077 
00078 
00079     #filtering
00080     if do_filter:
00081         lexer_data = [lexer for lexer in lexer_data
00082                        if filter_re.search(lexer[sort_column])]
00083 
00084     #sorting
00085     if sort:
00086         lexer_data.sort(cmp=lambda x, y:
00087           ((x != y)
00088           and cmp(x[sort_column].lower(), y[sort_column].lower())
00089           or cmp(x[0].lower(), y[0].lower())))
00090 
00091     #generating output
00092     ret = []
00093 
00094     #table header
00095     ret.extend([
00096         f.table(1),
00097         f.table_row(1, style="background-color: #ffffcc"),
00098         ])
00099     for col in columns:
00100         ret.extend([
00101                     f.table_cell(1),
00102                     f.strong(1),
00103                     f.text(column_titles[col]), f.strong(0), f.table_cell(0)
00104                   ])
00105     ret.append(f.table_row(0))
00106 
00107     #table data
00108     for parser in lexer_data:
00109         ret.append(f.table_row(1))
00110 
00111         for col in columns:
00112             if col:
00113                 ret.extend([
00114                             f.table_cell(1),
00115                             f.code(1),
00116                             isinstance(parser[col], str) and f.text(parser[col])
00117                               or (f.code(0) + ', ' + f.code(1)).join([f.text(i) for i in parser[col]]),
00118                             f.code(0),
00119                             f.table_cell(0),
00120                           ])
00121             else:
00122                 ret.extend([f.table_cell(1), f.text(parser[col]), f.table_cell(0)])
00123 
00124         ret.append(f.table_row(0))
00125 
00126     ret.append(f.table(0))
00127 
00128     return ''.join(ret)
00129 

Variable Documentation

list MoinMoin.macro.HighlighterList.available_columns = ['description', 'names', 'patterns', 'mimetypes']

Definition at line 36 of file HighlighterList.py.