Attachment 'PygmentsLexerList-1.9.0-1.py'

Download

   1 """
   2     PygmentsLexerList Macro
   3 
   4     A simple macro for displaying a table with list of available Pygments 
   5 	lexers.
   6 
   7     Usage: <<PygmentsLexerList([columns=<list of one of name, aliases, 
   8              patterns, mimetypes, separated by pipe>, 
   9              sort_column=(name|alises|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 "name", "aliases", "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 "name", "aliases", "patterns", "mimetypes". If 
  18            column is not "name", item values in this columns will be split. Has
  19            effect in any case (not only when sort is True). If sort_column is 
  20            empty, names 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.
  28 """
  29 
  30 from MoinMoin.config import multiconfig
  31 from MoinMoin import wikiutil
  32 
  33 import pygments.lexers as lexers
  34 
  35 import re
  36 
  37 def macro_PygmentsLexerList(macro, columns='name|aliases|patterns|mimetypes', sort_column='name', sort=True, filter_re=None, _kwargs=None):
  38     request = macro.request
  39     _ = request.getText
  40     f = macro.request.formatter
  41 
  42     available_columns = ['name', 'aliases', 'patterns', 'mimetypes']
  43     column_titles = [_('Lexer name'), _('Lexer alias'), _('File pattern'), _('Mimetype')]
  44 
  45     columns = columns and [available_columns.index(column) for column in columns.split('|') if column in available_columns] or range(len(available_columns))
  46     sort = isinstance(sort, bool) and sort or (isinstance(sort, (str, unicode)) and str(sort).lower() in ('true', '1', 'yes') or False)
  47     sort_column = (sort_column in available_columns) and available_columns.index(sort_column) or 0
  48     do_filter = (filter_re not in (None, ""))
  49     filter_re = re.compile(filter_re or ".*")
  50 
  51     lexer_list = lexers.get_all_lexers()
  52     lexer_data = []
  53 
  54     #expanding tuples if sort_column is not name
  55     if sort_column != 0:
  56         for lexer in lexer_list:
  57             if len(lexer[sort_column]):
  58                 for i in lexer[sort_column]:
  59                     lexer_item = list(lexer)
  60                     lexer_item[sort_column] = i
  61                     lexer_data.append(lexer_item)
  62             else:
  63                 lexer_item = list(lexer)
  64                 lexer_item[sort_column] = ""
  65                 lexer_data.append(lexer_item)
  66     else:
  67         lexer_data.extend( lexer_list)
  68 
  69 
  70     #filtering
  71     if do_filter:
  72         lexer_data = [lexer for lexer in lexer_data if filter_re.search(lexer[sort_column])]
  73 
  74     #sorting
  75     if sort:
  76         lexer_data.sort(cmp=lambda x, y: ((x != y) and cmp(x[sort_column].lower(), y[sort_column].lower()) or cmp(x[0].lower(), y[0].lower())))
  77 
  78     #generating output
  79     ret = []
  80 
  81     #table header
  82     ret.extend([
  83         f.table(1),
  84         f.table_row(1),
  85         ])
  86     for col in columns:
  87         ret.extend([f.table_cell(1, attrs={'bgcolor': '#ffffcc'}), f.strong(1), f.text(column_titles[col]), f.strong(0), f.table_cell(0)])
  88     ret.append(f.table_row(0))
  89 
  90     #table data
  91     for parser in lexer_data:
  92         ret.append(f.table_row(1))
  93 
  94         for col in columns:
  95             ret.extend([f.table_cell(1), f.text(parser[col]), f.table_cell(0)])
  96 
  97         ret.append(f.table_row(0))
  98 
  99     ret.append(f.table(0))
 100 
 101     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.