#!/usr/bin/env python import xml.dom.minidom as dom import cgi default_styles = { 'Comment' : 'def:comment', 'String' : 'def:string', 'Preprocessor' : 'def:preprocessor', 'Keyword' : 'def:keyword', 'Data Type' : 'def:data-type', 'Decimal' : 'def:decimal', 'Specials' : 'def:specials', 'Function' : 'def:function', 'Base-N Integer' : 'def:base-n-integer', 'Floating Point' : 'def:floating-point', 'Floating point' : 'def:floating-point', 'Others' : None, 'Other' : None, 'Others 2' : None, 'Others 3' : None, } def escape_escape_char(ch): if ch == '\\': return '\\\\' elif ch in ['@']: return ch raise RuntimeError("don't know how to escape '%s'" % (ch,)) def escape_regex(s): new = '' for c in s: if c == '/': new += '\\/' else: new += c return cgi.escape(new) def normalize_id(id): if id == "C#": return "c-sharp" elif id == ".desktop": return "desktop" elif id == ".ini": return "ini" elif id == "C++ Line Comment": return "cpp-line-comment" elif id == "Markup (inline)": return "markup-inline" elif id == "Markup (block)": return "markup-block" else: return id.replace(', ', '-').replace('.', '-').replace('*', '-').replace(',', '-').replace(' ', '-').replace('/', '-').replace('#', '-').lower() class LangFile(object): def __init__(self, id, name, _name, section, _section, mimetypes, globs, filename): object.__init__(self) assert name or _name assert section or _section self.id = normalize_id(id or name or _name) self.name = name self._name = _name self.section = section self._section = _section self.mimetypes = mimetypes self.globs = globs self.filename = filename self.contexts = [] self.escape_char = None def set_esc_char(self, char): self.escape_char = char def add_context(self, ctx): self.contexts.append(ctx) def format_header(self, indent): string = '\n\n' if self.mimetypes: string += 2*indent + '%s\n' % (cgi.escape(self.mimetypes),) if self.globs: string += 2*indent + '%s\n' % (cgi.escape(self.globs),) string += indent + '\n\n' return string def format_footer(self, indent): return '\n' def format_styles(self, indent): string = indent + "\n" styles = {} for ctx in self.contexts: map_to = default_styles[ctx.style_name] styles[ctx.style] = [ctx.style_name, map_to] for s in styles: id = s name, map_to = styles[s] if map_to: string += indent*2 + '