| 5 | | #yaml.add_resolver(u'!Config', []) |
| 6 | | #yaml.add_resolver(u'!TokensConfig', [u'tokens']) |
| 7 | | #yaml.add_resolver(u'!EventsConfig', [u'events']) |
| 8 | | #yaml.add_resolver(u'!StartEndConfig', [u'tokens', None]) |
| 9 | | #yaml.add_resolver(u'!StartEndConfig', [u'events', None]) |
| | 5 | class Style: |
| | 6 | |
| | 7 | def __init__(self, header=None, footer=None, |
| | 8 | tokens=None, events=None, replaces=None): |
| | 9 | self.header = header |
| | 10 | self.footer = footer |
| | 11 | self.replaces = replaces |
| | 12 | self.substitutions = {} |
| | 13 | for domain, Class in [(tokens, 'Token'), (events, 'Event')]: |
| | 14 | if not domain: |
| | 15 | continue |
| | 16 | for key in domain: |
| | 17 | name = ''.join([part.capitalize() for part in key.split('-')]) |
| | 18 | cls = getattr(yaml, '%s%s' % (name, Class)) |
| | 19 | value = domain[key] |
| | 20 | if not value: |
| | 21 | continue |
| | 22 | start = value.get('start') |
| | 23 | end = value.get('end') |
| | 24 | if start: |
| | 25 | self.substitutions[cls, -1] = start |
| | 26 | if end: |
| | 27 | self.substitutions[cls, +1] = end |
| | 28 | |
| | 29 | def __setstate__(self, state): |
| | 30 | self.__init__(**state) |
| | 31 | |
| | 32 | yaml.add_path_resolver(u'tag:yaml.org,2002:python/object:__main__.Style', |
| | 33 | [None], dict) |
| | 34 | yaml.add_path_resolver(u'tag:yaml.org,2002:pairs', |
| | 35 | [None, u'replaces'], list) |
| 13 | | def __init__(self, config): |
| 14 | | parameters = yaml.load(config) |
| 15 | | self.replaces = parameters['replaces'] |
| 16 | | self.substitutions = {} |
| 17 | | for domain, items in [('Token', parameters['tokens']), |
| 18 | | ('Event', parameters['events'])]: |
| 19 | | for code in items: |
| 20 | | name = ''.join([part.capitalize() for part in code.split('-')]+[domain]) |
| 21 | | cls = getattr(yaml, name) |
| 22 | | value = items[code] |
| 23 | | if value: |
| 24 | | if 'start' in value: |
| 25 | | self.substitutions[cls, -1] = value['start'] |
| 26 | | if 'end' in value: |
| 27 | | self.substitutions[cls, +1] = value['end'] |
| | 39 | def __init__(self, options): |
| | 40 | config = yaml.load(file(options.config, 'rb').read()) |
| | 41 | self.style = config[options.style] |
| | 42 | if options.input: |
| | 43 | self.input = file(options.input, 'rb') |
| | 44 | else: |
| | 45 | self.input = sys.stdin |
| | 46 | if options.output: |
| | 47 | self.output = file(options.output, 'wb') |
| | 48 | else: |
| | 49 | self.output = sys.stdout |
| 29 | | def highlight(self, input): |
| 30 | | if isinstance(input, str): |
| 31 | | if input.startswith(codecs.BOM_UTF16_LE): |
| 32 | | input = unicode(input, 'utf-16-le') |
| 33 | | elif input.startswith(codecs.BOM_UTF16_BE): |
| 34 | | input = unicode(input, 'utf-16-be') |
| 35 | | else: |
| 36 | | input = unicode(input, 'utf-8') |
| | 51 | def highlight(self): |
| | 52 | if self.style.header: |
| | 53 | self.output.write(self.style.header) |
| | 54 | input = self.input.read() |
| | 55 | if input.startswith(codecs.BOM_UTF16_LE): |
| | 56 | input = unicode(input, 'utf-16-le') |
| | 57 | elif input.startswith(codecs.BOM_UTF16_BE): |
| | 58 | input = unicode(input, 'utf-16-be') |
| | 59 | else: |
| | 60 | input = unicode(input, 'utf-8') |
| | 61 | substitutions = self.style.substitutions |
| 45 | | if (cls, -1) in self.substitutions: |
| 46 | | markers.append([token.start_mark.index, +2, number, self.substitutions[cls, -1]]) |
| 47 | | if (cls, +1) in self.substitutions: |
| 48 | | markers.append([token.end_mark.index, -2, number, self.substitutions[cls, +1]]) |
| | 70 | if (cls, -1) in substitutions: |
| | 71 | markers.append([token.start_mark.index, +2, number, substitutions[cls, -1]]) |
| | 72 | if (cls, +1) in substitutions: |
| | 73 | markers.append([token.end_mark.index, -2, number, substitutions[cls, +1]]) |
| 53 | | if (cls, -1) in self.substitutions: |
| 54 | | markers.append([event.start_mark.index, +1, number, self.substitutions[cls, -1]]) |
| 55 | | if (cls, +1) in self.substitutions: |
| 56 | | markers.append([event.end_mark.index, -1, number, self.substitutions[cls, +1]]) |
| | 78 | if (cls, -1) in substitutions: |
| | 79 | markers.append([event.start_mark.index, +1, number, substitutions[cls, -1]]) |
| | 80 | if (cls, +1) in substitutions: |
| | 81 | markers.append([event.end_mark.index, -1, number, substitutions[cls, +1]]) |
| 75 | | parser.add_option('-c', '--config', dest='config', default='yaml_hl_ascii.cfg', metavar='CONFIG') |
| | 102 | parser.add_option('-s', '--style', dest='style', default='ascii', |
| | 103 | help="specify the highlighting style", metavar='STYLE') |
| | 104 | parser.add_option('-c', '--config', dest='config', default='yaml_hl.cfg', |
| | 105 | help="set an alternative configuration file", metavar='CONFIG') |
| | 106 | parser.add_option('-i', '--input', dest='input', default=None, |
| | 107 | help="set the input file (default: stdin)", metavar='FILE') |
| | 108 | parser.add_option('-o', '--output', dest='output', default=None, |
| | 109 | help="set the output file (default: stdout)", metavar='FILE') |