3 import yaml
, codecs
, sys
, os
.path
, optparse
7 def __init__(self
, header
=None, footer
=None,
8 tokens
=None, events
=None, replaces
=None):
11 self
.replaces
= replaces
12 self
.substitutions
= {}
13 for domain
, Class
in [(tokens
, 'Token'), (events
, 'Event')]:
17 name
= ''.join([part
.capitalize() for part
in key
.split('-')])
18 cls
= getattr(yaml
, '%s%s' % (name
, Class
))
22 start
= value
.get('start')
23 end
= value
.get('end')
25 self
.substitutions
[cls
, -1] = start
27 self
.substitutions
[cls
, +1] = end
29 def __setstate__(self
, state
):
30 self
.__init
__(**state
)
32 yaml
.add_path_resolver(u
'tag:yaml.org,2002:python/object:__main__.Style',
34 yaml
.add_path_resolver(u
'tag:yaml.org,2002:pairs',
35 [None, u
'replaces'], list)
39 def __init__(self
, options
):
40 config
= yaml
.load(file(options
.config
, 'rb').read())
41 self
.style
= config
[options
.style
]
43 self
.input = file(options
.input, 'rb')
45 self
.input = sys
.stdin
47 self
.output
= file(options
.output
, 'wb')
49 self
.output
= sys
.stdout
52 input = self
.input.read()
53 if input.startswith(codecs
.BOM_UTF16_LE
):
54 input = unicode(input, 'utf-16-le')
55 elif input.startswith(codecs
.BOM_UTF16_BE
):
56 input = unicode(input, 'utf-16-be')
58 input = unicode(input, 'utf-8')
59 substitutions
= self
.style
.substitutions
60 tokens
= yaml
.scan(input)
61 events
= yaml
.parse(input)
66 if token
.start_mark
.index
!= token
.end_mark
.index
:
68 if (cls
, -1) in substitutions
:
69 markers
.append([token
.start_mark
.index
, +2, number
, substitutions
[cls
, -1]])
70 if (cls
, +1) in substitutions
:
71 markers
.append([token
.end_mark
.index
, -2, number
, substitutions
[cls
, +1]])
76 if (cls
, -1) in substitutions
:
77 markers
.append([event
.start_mark
.index
, +1, number
, substitutions
[cls
, -1]])
78 if (cls
, +1) in substitutions
:
79 markers
.append([event
.end_mark
.index
, -1, number
, substitutions
[cls
, +1]])
84 for index
, weight1
, weight2
, substitution
in markers
:
86 chunk
= input[index
:position
]
87 for substring
, replacement
in self
.style
.replaces
:
88 chunk
= chunk
.replace(substring
, replacement
)
91 chunks
.append(substitution
)
93 result
= u
''.join(chunks
)
95 self
.output
.write(self
.style
.header
)
96 self
.output
.write(result
.encode('utf-8'))
98 self
.output
.write(self
.style
.footer
)
100 if __name__
== '__main__':
101 parser
= optparse
.OptionParser()
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',
105 default
=os
.path
.join(os
.path
.dirname(sys
.argv
[0]), 'yaml_hl.cfg'),
106 help="set an alternative configuration file", metavar
='CONFIG')
107 parser
.add_option('-i', '--input', dest
='input', default
=None,
108 help="set the input file (default: stdin)", metavar
='FILE')
109 parser
.add_option('-o', '--output', dest
='output', default
=None,
110 help="set the output file (default: stdout)", metavar
='FILE')
111 (options
, args
) = parser
.parse_args()
112 hl
= YAMLHighlight(options
)