Changeset 943


Ignore:
Timestamp:
08/04/06 12:51:40 (7 years ago)
Author:
mglb1
Message:

Add syntax highlighting support to the configuration file display.
Enscript is used for formatting. Each template may specify that syntax
highlighting scheme that it wishes to use by adding an attribute named
highlightFormat to itself. This may take any valid value that enscript
will accept. If the highlightFormat attribute is not present then sh
formatting is used.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ccsd/trunk/crcnetd/_utils/ccsd_cfengine.py

    r942 r943  
    536536        start = time.time() 
    537537        log_debug("Started host generation thread for %s" % host) 
     538        session = getSessionE(networkData["session_id"]) 
    538539         
    539540        # Retrieve the list of templates to generate and flag the start  
     
    566567                files = t.writeTemplate(filename, template.multiFile) 
    567568                tend = time.time() 
     569                # Set properties 
     570                format = getattr(t, "highlightFormat", "") 
     571                if format != "": 
     572                    for file in files: 
     573                        session.revision.propset(file, "ccs:format", format) 
    568574                # Record how long the file took to generate 
    569575                _statsLock.acquire() 
     
    631637        log_debug("Started template generation thread for %s" % template_id) 
    632638        tstart = time.time() 
     639        session = getSessionE(networkData["session_id"]) 
    633640        # Instantiate a template 
    634641        template = _networkTemplates[template_id] 
     
    640647        files = t.writeTemplate(filename, template.multiFile) 
    641648        tend = time.time() 
     649        # Set properties 
     650        format = getattr(template, "highlightFormat", "") 
     651        if format != "": 
     652            for file in files: 
     653                session.revision.propset(file, "ccs:format", format) 
    642654        # Record how long the file took to generate 
    643655        _statsLock.acquire() 
     
    10471059        entries[0]["props"] = props 
    10481060        if doMarkup: 
    1049             entries[0]["contents"] = markup(file) 
    1050              
     1061            if "ccs:format" in props.keys(): 
     1062                entries[0]["contents"] = markup(file, props["ccs:format"]) 
     1063            else: 
     1064                entries[0]["contents"] = markup(file) 
     1065 
    10511066    return entries 
    10521067 
    1053 def markup(file): 
    1054     return file.split("\n") 
     1068def markup(file, format="sh"): 
     1069    """Passes the file through enscript for syntax highlighting""" 
     1070     
     1071    # Generate the command line to pass to enscript 
     1072    cmdline = config_get("cfengine", "enscript_path", "enscript") 
     1073    cmdline += ' --color -h -q --language=html -p - -E' + format 
     1074    log_debug("Enscript command line: %s" % cmdline) 
     1075     
     1076    # Run enscript 
     1077    (fdi, fdo) = os.popen2(cmdline) 
     1078    fdi.write(file) 
     1079    fdi.close() 
     1080    odata = fdo.read() 
     1081    rv = fdo.close() 
     1082    if rv is not None: 
     1083        log_warn("Could not run enscript to markup file!") 
     1084        log_debug(odata) 
     1085        return file.split("\n") 
     1086 
     1087    # Strip header and footer 
     1088    i = odata.find('<PRE>') 
     1089    beg = i > 0 and i + 6 
     1090    i = odata.rfind('</PRE>') 
     1091    end = i > 0 and i or len(odata) 
     1092    odata = EnscriptDeuglifier().format(odata[beg:end]) 
     1093    return odata.splitlines() 
     1094 
     1095# Enscript Deuglifier code from Trac 
     1096#  
     1097# Copyright (C) 2003-2006 Edgewall Software 
     1098# All rights reserved. 
     1099# 
     1100# Licensed using the Modified BSD license 
     1101class EnscriptDeuglifier(object): 
     1102    def __new__(cls): 
     1103        self = object.__new__(cls) 
     1104        if not hasattr(cls, '_compiled_rules'): 
     1105            cls._compiled_rules = re.compile('(?:'+'|'.join(cls.rules())+')') 
     1106        self._compiled_rules = cls._compiled_rules 
     1107        return self 
     1108     
     1109    def format(self, indata): 
     1110        return re.sub(self._compiled_rules, self.replace, indata) 
     1111 
     1112    def replace(self, fullmatch): 
     1113        for mtype, match in fullmatch.groupdict().items(): 
     1114            if match: 
     1115                if mtype == 'font': 
     1116                    return '<span>' 
     1117                elif mtype == 'endfont': 
     1118                    return '</span>' 
     1119                return '<span class="code-%s">' % mtype 
     1120     
     1121    def rules(cls): 
     1122        return [ 
     1123            r'(?P<comment><FONT COLOR="#B22222">)', 
     1124            r'(?P<keyword><FONT COLOR="#5F9EA0">)', 
     1125            r'(?P<type><FONT COLOR="#228B22">)', 
     1126            r'(?P<string><FONT COLOR="#BC8F8F">)', 
     1127            r'(?P<func><FONT COLOR="#0000FF">)', 
     1128            r'(?P<prep><FONT COLOR="#B8860B">)', 
     1129            r'(?P<lang><FONT COLOR="#A020F0">)', 
     1130            r'(?P<var><FONT COLOR="#DA70D6">)', 
     1131            r'(?P<font><FONT.*?>)', 
     1132            r'(?P<endfont></FONT>)' 
     1133        ] 
     1134    rules = classmethod(rules) 
    10551135 
    10561136##################################################################### 
Note: See TracChangeset for help on using the changeset viewer.